Updating item in inventory after changing its durability

Hello guys. I’m really kind of stuck with this problem. As the title says I’m trying to change an items durability, which according to the TransactionResult is successfull. But this doesn’t seem to change the item that the player is hold/ which is in his inventory.

This is the code I’m using to update the items durability:
if(item.supports(Keys.ITEM_DURABILITY)) { item.offer(Keys.ITEM_DURABILITY, item.get(Keys.ITEM_DURABILITY).get()-1); }

I’ve already tried offering the item to the players inventory, using player.getInventory().offer(ItemStack), but this just adds a new item with less durability.

Thank you, nylser.

How do you retrieve the ItemStack? Because in most cases you are just working with copies! That means you need to offer it back.

I use the item stack I get from a ChangeBlockEvent.Break, using player.getItemInHand().
How would I retrieve the stack correctly?

That’s correct but you need to use player.setItemInHand(item) after you changed the durability.

1 Like

That worked! Thank you! :slightly_smiling: I knew I was missing something.

One more question. When I set the durability of an item to 0 or -1 it doesn’t break. Do I have to do a custom check and fire an event for that? Currently, I’m just deleting the item by setting the itemInHand to null, but this of course doesn’t trigger the animation&sound.

I don’t think it’s possible because the animation is handled client side but you can send the Sound with
player.playSound(SoundTypes.ITEM_BREAK, player.getLocation().getPosition(), 1);
for only the Player.
Or with
player.getWorld().playSound(SoundTypes.ITEM_BREAK, player.getLocation().getPosition(), 1);
for every player nearby. The last argument is the volume between 0-2.

1 Like