Detect item durability change

As the title said I’m trying to detect when an Item’s durability change. I tried using the ChangeDataHolderEvent.ValueChange event, but that seems not to be fired off. So I wonder if there is an event for such case, since I don’t want to handle all possible events where an Item can loose durability (since every Item, especially modded Items, loose durability in many different ways)

Sadly the actual even is the one you found ChangeDataHolderEvenr.ChangeValue however it isnt implemented for Key.ITEM_DURABILITY. Go to spongepowered GitHub and report an issue on it. It may get implemented then.

The work around would be to detect the event manually by storing all known items every tick and then the next server tick, comparing the durability. If one has changed then fire a fake event

I thought about this, but it may be really heavy to handle on some servers :confused: I guess i shoul cover most of the cases and then update the source when new events are found

Depends. Could put efficienties in place by making some assumptions, such as only getting data from items that are in loaded chunks. Only store the ItemStack and a double for durability (reduce the load on RAM). When checking Players inventory for items, only check items in either hand as well as armour just to name a few. The last one may not work well with mods that add extra slots to the player

I currently did this: a task that checks, for each player, the items in both hands and armor slots. And yes, this would not work with mods that adds extra armor slots, but i guess that until a proper hook is added this is fine (unless you check the whole inventory)

Thinking about it a little more you could just check every slot that isnt in the “Main Inventory” and crafting slots. That will get around the mod problem

1 Like

Not the cleanest way, since you won’t be able to see if that slot is actually an “equip” slot, like a new armor slot or a new hotbar slot, but i guess it could work

Depends on how you do it. If you get the parent inventory of the slot and just check if it is Main Inventory or a 2x2 grid inventory then continue to the next slot, if not then check for the item inside.