Jump to content

Detecting Item in player inventory and delete it in OnItemUseFirst.


MattZM

Recommended Posts

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 by vemerion
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.