Jump to content

Get item stack from an integer slot


ChromaKey81

Recommended Posts

What's the method for getting an item stack from the slot of either an entity or tile entity? I need to input a slot as an integer and return an ItemStack.

To get it from a tile entity, I've been using 

(IInventory) replace.getSource().getWorld().getTileEntity(sourcePos).getStackInSlot(sourceSlot)

which works properly, but someone else told me that IInventory won't work for anything outside of the vanilla game and that I should instead use the "IItemHandler wrapper." Not sure exactly what that is though.

 

However, I still haven't figured out how to do this with mobs, so I could use some help with that.

 

Edit: I suppose I should specify that this is being used for a command (the above code line is in ".executes((replace) -> {})")

Edited by ChromaKey81
not solved lol
Link to comment
Share on other sites

6 hours ago, diesieben07 said:

You first need to get the IItemHandler capability from the tile entity (getCapability with CapabilityItemHandler.ITEM_HANDLER_CAPABILITY). This will give you an IItemHandler if the TE has an inventory. You can then interact with that.

Thank you for the explanation on that. Does this work for entities? If not, what is the method for that?

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Entities that have an inventory (e.g. the player) also provide this capability, yes.

So it works with players, thanks. Are entities with armor/hand slots (e.g. zombies) included? I need to be able to pass any entity with slots as a source.

Edited by ChromaKey81
Link to comment
Share on other sites

Just now, diesieben07 said:

Yes, all living entities have equipment and expose it as the IItemHandler capability.

You can even pass in a Direction to only get the hands (Direction.UP or DOWN) or the armor (all other directions). When you pass in null you get a handler that wraps the whole inventory.

Thanks so much. I've been looking for an answer to this for a while

Link to comment
Share on other sites

7 hours ago, diesieben07 said:

You first need to get the IItemHandler capability from the tile entity (getCapability with CapabilityItemHandler.ITEM_HANDLER_CAPABILITY). This will give you an IItemHandler if the TE has an inventory. You can then interact with that.

Shoot, actually I'm still having a bit of trouble with that.

replace.getSource().getWorld().getTileEntity(BlockPosArgument.getLoadedBlockPos(replace, "sourcePos")).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)

 

This returns LazyOptional<IItemHandler>. How do I get from there to using the IItemHandler methods?

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.