ChromaKey81 Posted July 1, 2020 Posted July 1, 2020 (edited) 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 July 1, 2020 by ChromaKey81 not solved lol Quote
ChromaKey81 Posted July 1, 2020 Author Posted July 1, 2020 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? Quote
ChromaKey81 Posted July 1, 2020 Author Posted July 1, 2020 (edited) 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 July 1, 2020 by ChromaKey81 Quote
ChromaKey81 Posted July 1, 2020 Author Posted July 1, 2020 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 Quote
ChromaKey81 Posted July 1, 2020 Author Posted July 1, 2020 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? Quote
Recommended Posts
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.