Posted November 13, 20213 yr How can I detect a certain item and delete it? [1.16.5] It’s supposed to be done in onItemUseFirst I’ve tried: playerinventory.contains(new Itemstack(ITEM)) (ITEM is whatever a item) but it failed
November 16, 20213 yr On 11/13/2021 at 1:48 PM, MattZM said: How can I detect a certain item and delete it? [1.16.5] If you want to check for a certain item and delete all stacks of it from the player inventory, you could do something like this: IItemHandler inventory = new PlayerMainInvWrapper(player.inventory); for (int i = 0; i < inventory.getSlots(); i++) if (inventory.getStackInSlot(i).getItem() == <THE ITEM YOU WANT TO DELETE>) inventory.extractItem(i, inventory.getSlotLimit(i), false); The IItemHandler is a class provided by Forge that makes manipulation of inventories easier, which is why it is advantageous to use that instead of manipulating the player inventory directly. EDIT: Or do you want to consume the item you are using? The you could do stack.shrink(1) or similar on the item stack in question. Edited November 16, 20213 yr by vemerion
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.