Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi everybody !

 

I want to create an item which, when the player right click with it in his main hand, open the Ender Chest container, like if he right click in an Ender Chest. 

@SubscribeEvent
public static void onBackpackUsing(PlayerInteractEvent.RightClickItem event) {
    PlayerEntity player = event.getPlayer();
    World world = event.getWorld();
    if (player.getHeldItemMainhand().getItem() == ModItems.ENDER_BACKPACK.get()) {
        world.setBlockState(player.func_233580_cy_().add(0,-1,0), Blocks.ENDER_CHEST.getDefaultState());
    } else if (player.getHeldItemMainhand().getItem() == ModItems.BACKPACK.get()) {
        world.setBlockState(player.func_233580_cy_().add(0,-1,0), Blocks.CHEST.getDefaultState());
    }
}

With this code, I can place an Ender Chest (or a Chest, depending on the item that the player use) under the player, but I don't know how to open the container, instead of placing the chest.

 

Thanks for you help !

Edited by Korantin

  • Author

Thanks for your answer !

I found how to do this with an ender chest !

Thanks very much !

Edited by Korantin

  • Author

I have another question...

How can I do the same thing, but with a normal chest ?

I want to create another item, a backpack, that when the player right click with it in the main hand, it open a container like if he was opening a chest. I also want to keep items that are in the container, to access to them in the future if the player right click with the same backpack.

I studied the chest code, but I still don't find how to do this.

  • Author
4 hours ago, diesieben07 said:

Make your items provide a IItemHandler capability. You can then use that in a Container / ContainerScreen as normal.

I'm not verry familiar with Capabilities, and I don't really understand the official documentation (I tried to understand this by myself before asking you some help), so could you explain me how can I do this please ?

Thanks a lot for helping me !

  • Author
18 minutes ago, diesieben07 said:

Make a class that implements ICapabiltySerializable.

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

public class ModCapabilities implements ICapabilitySerializable {

    @org.jetbrains.annotations.NotNull
    @Override
    public LazyOptional<T> getCapability(@org.jetbrains.annotations.NotNull Capability<T> cap, @org.jetbrains.annotations.Nullable Direction side) {
        return null;
    }

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }
}

I've done that, but my IDE underline in red the line where it is written "public LazyOptional<T> getCapability(@org.jetbrains.annotations.NotNull Capability<T> cap, @org.jetbrains.annotations.Nullable Direction side) {" and says me that there is an error.

 

And the word jetbrains is written in red in all the class.

 

What am I supposed to do to fix it ?

Edited by Korantin

  • Author

When I implemented ICapabilitySerializable, there was an error, and my IDE proposes me to implements methods, then it add these methods, with these annotations

  • Author

I've searched how to expose my ItemStackHandler as a capacity and still don't understand how to do this...

Sorry, but could you explain me how to do this please ?

 

My class :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    @CapabilityInject(ItemStackHandler.class)
    static Capability<ItemStackHandler> myCapability = null;

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

}

 

Edited by Korantin

  • Author

I've done that :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    @CapabilityInject(ItemStackHandler.class)
    static Capability<ItemStackHandler> ITEM_HANDLER_CAPABILITY = null;


    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

    private LazyOptional<IItemHandler> handler = LazyOptional.of(() -> ITEM_HANDLER_CAPABILITY).cast();

    @Override
    public boolean hasCapability(Capability<?> capability) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability);
    }

    @Override
    public <T> T getCapability(Capability<T> capability) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) inventory;
        }
        return super.getCapability(capability);
    }

}

But in the hasCapability methods, hasCapability is written in red, and the IDE says that "Cannot resolve method 'hasCapability' in 'Object'.

Please can you help me to fix it ? I don't really understand what goes wrong...

Thanks a lot for all the time you spend for me !

  • Author

Sorry, but I really don't understand anything about how to use LazyOptinal<IItemHandler>... Could you explain me please ?

  • Author
package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> myItemStackHandler);

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

}

Is it something like this for the LazyOptional<ItemHandler> ?

  • Author

I study Java in my studies, but we are at the very beginnings. I learn a bit more by myself, but just the basics.

I don't understand how to write and read data, like if the player was using a chest, when he right click with the item in his main hand.

I know how to detect when he right click with the item in his main hand, but no how to open the same gui as the chest gui, an how to write and read data.

  • Author

Okay, so, now I've done that :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;


public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    private LazyOptional<IItemHandler> handler = LazyOptional.of(() -> myItemStackHandler);

    @Override
    public INBT serializeNBT() {
        return myItemStackHandler.serializeNBT();
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        myItemStackHandler.deserializeNBT(myItemStackHandler.serializeNBT());
    }

    @Override
    public <IItemHandler> LazyOptional<IItemHandler> getCapability(Capability<IItemHandler> cap, Direction side) {
        return (LazyOptional<IItemHandler>) handler;
    }

}

But in my other class that manages events, how I open the GUI ? I just have to call the getCapability method, or I have to do something like in ChestBlock's class to open a chest ?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.