Jump to content

Inventory on Items


stepsword

Recommended Posts

I'm trying to attach a 3-slot inventory to an ItemStack for one of the Items in my mod. So far I've got it rendering properly, and hovering over the squares highlights them in the right places and items from the player's inventory show in the right spots.

 

But I can't pick up anything in the inventory - when I click something it gets picked up and placed back down immediately (and also, in the background, the "hitting" animation with the item fires). 

 

Here's the code for the Container:

 

Spoiler

public class MysticCodeInventory extends Container {
    ItemStackHandler codeInventory;
    IInventory playerInventory;

    public MysticCodeInventory(ItemStackHandler ci, IInventory pi, EntityPlayer player) {
        codeInventory = ci;
        playerInventory = pi;

        this.addSlotToContainer(new SlotItemHandler(codeInventory, 0, 44, 20)
        {
            public boolean isItemValid(ItemStack stack) {
                return stack.getItem() instanceof SpellScroll && !this.getHasStack();
            }
            @SideOnly(Side.CLIENT)
            public boolean isEnabled()
            {
                return true;
            }
        });
        this.addSlotToContainer(new SlotItemHandler(codeInventory, 1, 80, 20)
        {
            public boolean isItemValid(ItemStack stack) {
                return stack.getItem() instanceof SpellScroll && !this.getHasStack();
            }
            @SideOnly(Side.CLIENT)
            public boolean isEnabled()
            {
                return true;
            }
        });

        this.addSlotToContainer(new SlotItemHandler(codeInventory, 2, 116, 20)
        {
            public boolean isItemValid(ItemStack stack) {
                return stack.getItem() instanceof SpellScroll && !this.getHasStack();
            }
            @SideOnly(Side.CLIENT)
            public boolean isEnabled() {
                return true;
            }
        });


        for (int i1 = 0; i1 < 3; ++i1)
        {
            for (int k1 = 0; k1 < 9; ++k1)
            {
                this.addSlotToContainer(new Slot(playerInventory, k1 + i1 * 9 + 9, 8 + k1 * 18, i1 * 18 + 51));
            }
        }

        for (int j1 = 0; j1 < 9; ++j1)
        {
            this.addSlotToContainer(new Slot(playerInventory, j1, 8 + j1 * 18, 109));
        }

    }

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
        return playerIn != null && !playerIn.getHeldItemMainhand().isEmpty() && playerIn.getHeldItemMainhand().getItem() instanceof MysticCode;
    }


    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
    {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index < this.codeInventory.getSlots())
            {
                if (!this.mergeItemStack(itemstack1, this.codeInventory.getSlots(), this.inventorySlots.size(), true))
                {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 0, this.codeInventory.getSlots(), false))
            {
                return ItemStack.EMPTY;
            }

            if (itemstack1.isEmpty())
            {
                slot.putStack(ItemStack.EMPTY);
            }
            else
            {
                slot.onSlotChanged();
            }

        }
        return itemstack;
    }

}

Here's the relevant code from the item class:

 

Spoiler

@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
    return new MysticCodeInventoryProvider();
}

@Override
public int getItemStackLimit(ItemStack stack) {
    return 1;
}



@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    if (playerIn.isSneaking()) {
        if (worldIn.isRemote) {
            MahouTsukaiMod.proxy.openMysticCodeGUI(worldIn, playerIn.getHeldItem(handIn), true);
        }
    } else {
        playerIn.setActiveHand(handIn);
    }
    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, playerIn.getHeldItem(handIn));

}

@Override
public void readNBTShareTag(ItemStack stack, NBTTagCompound nbtt) {
    IItemHandler itemHandler = stack.getCapability(MysticCodeInventoryProvider.MYSTIC_CODE_INVENTORY, null);
    IItemHandler serverHandler = new ItemStackHandler(3);
    if (nbtt != null) {
        MysticCodeInventoryProvider.MYSTIC_CODE_INVENTORY.getStorage().readNBT(MysticCodeInventoryProvider.MYSTIC_CODE_INVENTORY, serverHandler, null, nbtt);
        if (itemHandler != null) {
            for (int i = 0; i < itemHandler.getSlots(); i++) {
                serverHandler.insertItem(i, itemHandler.getStackInSlot(i), false);
            }
        }
    }
}

I also register the capability in the attach capability event for ItemStacks. 

 

Is there something obvious I'm missing that would prevent me from picking items up? When I followed the "picking up" action in debug mode, it went through Container.slotClick() on both the client and server, and I could see the item being picked up on the client. But on the server it seemed to be picking up air.

 

Also, shift clicking the item does successfully put move the item from the hotbar to the inventory, and then it disappears after I close the inventory, but that's probably a bug for after whatever is stopping me from picking things up in the first place.

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.



×
×
  • Create New...

Important Information

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