Jump to content

[1.7.10] Detecting when player swaps held item


Recommended Posts

I have a feeling this is a really simple thing to do and probably a dumb question, but I have been searching for a while and not really found what I am looking for.


What I want to do is, detect when the player selects an item (scrolls to it, or presses the hotbar number). I have attempted to check the player's currently equipped item and compare it to the previously equipped item (as in the last held item), but I couldn't find a method for previously equipped items.


I did attempt a LivingUpdateEvent, to check and store the held item, but it didn't seem like the most practical way to do it, so I was wondering if anyone could advise me on a better way to detect when the player changes their held item.

Link to comment
Share on other sites

Comparing current to previous is the only option. Any other way would require either ASM or quite some replacements to vanilla inventory system.


As to storin previous values:

You need to implements IExtendedEntityProperties, use PlayerTickEvent (note: pick one event.phase, otherwise event is ran twice), make comprasion to previous stack and then refresh.


You will probably have to save two fields:

player.inventory.currentItem to check if "wheel" slot is same as before. - if it changed from prev pick, item changed.

player.inventory.getCurrentItem() compare if previous ItemStack is same as new one (from current tick).


I fortog you can just "==", no need to save currentItem.



IEEP tutorial:


1.7.10 is no longer supported by forge, you are on your own.

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.

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.