Jump to content

[1.12] Yet another struggle with a backpack implementation


RaijinKnight

Recommended Posts

 

Obligatory disclaimer... I have extremely little prior java experience. I've gone through several online guides and exercises, but by no means am I well versed in it, nor Eclipse (my IDE). Obviously this is also my first mod.

 

I have been trying to implement a basic backpack (craft and use it). Nothing fancy, but this is a key stepping stone for my mod. Successes thus far - Getting my mod info to show in MC, getting the item to appear, getting the model to appear (it has, however, disappeared in the mean time), implementing a crafting recipe for the backpack.

 

However... it doesn't do anything. I've scoured the internet, but I've only found examples for IInventory (Which should not be used anymore?) or half implementations. Stuck with the half implementations, a few 1.12 guides on making a chest... this is the basics of what I have. Is there any theoretical errors? Will post code, but I first want to know if my theory is wrong.

 

Item class, produced by crafting recipe. This contains an IItemStackHandler, and a custom capability that implements ICapabilityProvider. The onRightClick action result override calls a gui to open. It also overrides initCapabilities to provide the custom capability.

 

Custom capability contains an IItemHandler.

Spoiler

    public IItemHandler myItemHandler;
   
    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        } else {
        return false;
        }
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(myItemHandler);
        } else {
        return null;
        }
    }

I have a container, gui and gui proxy. The gui proxy gets called, but crashes when the container adds the slots for the item. Container's code:

Spoiler

private ItemStack pocketItemStack;

    public PocketContainer(IInventory playerInventory, ItemStack pocketItemStackIn) {
        this.pocketItemStack = pocketItemStackIn;

 
        addPlayerSlots(playerInventory);
        addOwnSlots();
    }

 

private void addOwnSlots() {
        IItemHandler itemHandler = this.pocketItemStack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        int x = 9;
        int y = 6;

        // Add our own slots
        int slotIndex = 0;
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            addSlotToContainer(new SlotItemHandler(itemHandler, slotIndex, x, y));
            slotIndex++;
            x += 18;
        }
    }

Any ideas? Hints? Directions to a 1.12 tutorial on implementing backpacks...? (I've been through multiple githubs, but the amount of extra's (honestly) confuses me so much I can't identify what the basic implementation is).

Link to comment
Share on other sites

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.