Jump to content

[1.7.10] event when item gets equiped/unequiped?


kauan99

Recommended Posts

Is there an event when an armor piece is equiped and unequiped? Also, when the held item is changed? I know minecraft sends a packet when the armorInventory gets changed (S04PacketEntityEquipment) and when the held item gets changed (C09PacketHeldItemChange and S09PacketHeldItemChange), Is there a way to react to those packets?

 

I want to be able to add PotionEffects to players while they wear/wield my items

 

My current approach is to override the ItemSword.onUpdate and the ItemArmor.onArmorTick and add all the effects to the player, but I feel like adding a bunch of potion effects to everyone that's wearing my items every tick is not a good design. I would also like to make check if a certain armor set is complete in order to give a special custom effect, but that would be too much overhead.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

You don't need events.

 

In your item, override:

//called every tick for all items in the inventory
public void onUpdate(ItemStack itemStack, World world, Entity entity, int slot, boolean held) {
}
//only makes sense for armor items, called every tick for equipped armors
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemStack) {
}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

yes that's what I've been using, but I don't think it's a good pattern.every tick I have to add several PotionEffects to each entity wearing my items (really a lot, I created a whole bunch of new potions). the fact that there are packets sent specifically for the 2 cases I need gives me hope of designing a better implementation

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

Both methods Draco mentions work great, with one caveat: they only tick while the item is in the inventory (for onUpdate) or equipped (for onArmorTickUpdate), meaning that if you want anything to happen when an item is no longer equipped / in the inventory, such as ending an effect, it won't work.

 

Since you are only adding potion effects at this time, you can get away with it by simply giving the potion effect a low duration, such as 4-5 ticks, and applying it every 4 ticks or so (e.g.  if (world.getWorldTime() % 4 == 0) { apply potion }).

 

If you want to be able to fly or some other effect that must end when the item(s) are no longer available to tick, you have to implement your own solution, generally using the PlayerTick event in combination with IExtendedEntityProperties for storing the last item(s) used. When the last item is not null but the current slot for it is null, you know to remove the effect.

 

EDIT: Adding potion effects / checking a few conditionals every tick doesn't really impact performance that much. Though it's good to think about these kinds of things it's really pre-optimization. Not to mention that even if you could intercept the packets, how would you remove the potion effect? What if the player had that potion effect from something else, too? Every option has pros / cons ;)

Link to comment
Share on other sites

You're right. It wouldn't work If I got those packets. And even if Forge had an event for it, it still wouldn't work, precisely because of what you said about the nature of PotionEffects being combined with each other when added. I loved world.getWorldTime(). I will use it to avoid adding the effects on every tick.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

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.