Jump to content

Continuously damaging item causes unequip


Recommended Posts

I am trying to implement cooldown for my items (CooldownItem extends Item), and it seems like ItemStack.itemDamage is the only place where I can store the cooldown time, so that it is correctly shared between client and server and is remembered when the itemStack is moved/dropped/etc.


Once the cooldown starts, I set itemDamage to the respective itemStack, and then on each tick in the CooldownItem.onUpdate(...) method I decrease this itemStack's itemDamage by 1.


Now the problem happens in ItemRenderer.updateEquippedItems(): probably because the itemStacks' damage is updated EVERY tick, it can get out of sync between different itemStacks that were copied. In the method I mentioned above the item that the player is currently holding in his hand gets out of sync with some other ItemStack reference to the same item because their itemDamage values are off by a small amount, and unequip animation starts. On some other tick they get in sync again and equip animation starts. So while my item is on cooldown and the player is holding it, it jerks up and down in a really annoying fashion.


Perhaps this could even be regarded as a bug in Minecraft, since ItemRenderer is located in package net.minecraft.client.renderer. But there are no items in Minecraft that get damaged constantly while no one uses them, like my CooldownItem.


I thought about storing cooldown in some other place, like make a Map<ItemStack, Integer> in CooldownItem, but then sharing the cooldown between copied itemStacks and between client and server seems like it's going to be a real pain to code. I can't subclass ItemStack, because it's declared final.


Anyone got ideas how to fix my rendering issue?

Link to comment
Share on other sites

Thanks for the reply, Kore!

I thought I had to do everything that is not rendering (and changing itemStack's damage surely is not) BOTH on the server and the client. But I tried your suggestion and added this check to CooldownItem.onUpdate(...), and the game still worked properly.


However, the jerking item issue was not solved. It even seemed like the item was spending more time in a "lowered" position than before. Although that looked a bit better than a "full" up-and-down swing :)

Link to comment
Share on other sites

Awesome, that was exactly what I needed! Thanks a lot, opssemnik.

There's only one (minor) issue: the item name stays on the display for the whole duration of my cooldown, for pretty much the same reason as described in the first post: because when item's NBT tag changes, Minecraft's GUI overlay thinks it's a different item that the player is holding now. But there's no more item unequip/equip problem!

Link to comment
Share on other sites

Unfortunately, item name showing up is not the only problem. If I change currently equipped item on cooldown to some other item and then back again to my item on cooldown, it starts unequip-equip animation again. So ultimately the problem wasn't solved.

However, NBT Tag is clearly much better than item stack damage, I just haven't read about its usage until now.

Link to comment
Share on other sites

  • 4 weeks later...

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.