Jump to content

[SOLVED] [1.16] Help with understanding Capabilities


Recommended Posts

Posted (edited)

In my current endeavours to learn Forge Modding, I decided to code a simple backpack Item. nothing to special, just an item, that would act like a chest, and whenever used would open it's interface.

I had already done some test with Tile Entities and Containers, and I thought I could use a Container to do the job, and implement IContainerProvider in the Item. I had an ItemStackHandler in the BackpackItem class to hold the inventory but I ran into some problems when trying to store it in nbt, as in the Tile Entities they have their own methods I could override to deal with writing and reading nbt. So after some research I came accros Capabilities, I've been trying to understand them for some time now, but I cannot find any examples on how to use the IItemHandler to accomplish my goal.

I'd appreciate if someone could tell me how I should go about using capabilities to implement this simples backpack Item. Or just point me to a git repo that does something similiar.

EDIT: I have read the forge docs, they haven't helped much

 

EDIT 2: I read TheGreyGhost github repo on making an item that has an inventory and managed to accomplish it

the repo is: https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe32_inventory_item

Edited by kiou.23
Posted (edited)
2 hours ago, diesieben07 said:

You cannot just store it in your item class, because there is only one instance of that for all stacks of your item - they would all share the same inventory.

Yes, I am aware of this. It was when I realized this that I searched and found about Capabilities

2 hours ago, diesieben07 said:

To add capabilities to your item you need to override initCapabilities in your Item class. In there return a new instance of an ICapabilitySerializable implementation. This implementation exposes the capabilities of your item and also handles serialization to NBT.

So I need to create my own class that implements ICapabilitySerializable? and then I can have the ItemStackHandler stored in it?

 

EDIT: and then what? I do still need a Container and a ContainerScreen correct? how do I open the screen and pass the contents of the Capability to it? do I need to implement INamedContainerProvider on the capability class?

 

I'll share my code below

Edited by kiou.23
Posted
public class BackpackItem extends Item {

    public BackpackItem(Properties properties) {
        super(properties);
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context) {
        if (!context.getWorld().isRemote) {
            
        }

        return ActionResultType.SUCCESS;
    }

    @Override
    public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
        return new BackpackCapability(this.getClass());
    }
}

 

public class BackpackCapability extends CapabilityProvider implements ICapabilitySerializable<CompoundNBT> {

    public BackpackCapability(Class baseClass) {
        super(baseClass);
    }

    private static final String INVENTORY_TAG = "inventory";

    ItemStackHandler inventory = new ItemStackHandler(27) {
        @Override
        public boolean isItemValid(int slot, ItemStack stack) {
            return !(stack.getItem() instanceof BackpackItem);
        }
    };

    @Override
    public LazyOptional<ItemStackHandler> getCapability(Capability cap, Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
            return LazyOptional.of(() -> this.inventory);
        return super.getCapability(cap, side);
    }


    @Override
    public CompoundNBT serializeNBT() {

        CompoundNBT nbt = new CompoundNBT();
        nbt.put(INVENTORY_TAG, this.inventory.serializeNBT());

        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        this.inventory.deserializeNBT(nbt);
    }
}

 

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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