Minecraft – how would I refill an items durability over time with commands

minecraft-commandsminecraft-java-edition

I have a tool I want to give players but they can only use when its at full durability, I already know how to use the selector damage:0 and set the damage to something, but I would like to refill the item over time, kinda like a cool down timer for the item, how would I go about doing that?

oh also if possible I would like it to work from anywhere in their inventory,

I was thinking mending but it gives it a nasty glow, and wouldn't work if they had any other mending stuff on or if its not selected

replace item needs a defined slot, as I want this item to regenerate passively without having to hold it in main slot (cuz it would be useless if they constantly had to hold it) it wouldn't work,

I thought about data modify or data merge, but i'm too dumb to figure that one out

edit: It could even be a new item that takes its place, because its not really a dynamic item, you shouldn't need enchant or rename it, here is the give command for the full item

/give @s minecraft:carrot_on_a_stick{CustomModelData:1,display:{Name:"{\"text\":\"Killer Queen\",\"italic\":\"false\"}"}}

the only problem with clearing the item and giving it again with increasing durability is that the item may jump slots if you have any other slots empty, which would kind of break the illusion, though if it comes to that then I will

Best Answer

You cannot directly edit player NBT data. Something like in your case used to be possible by abusing this bug, but that was fixed. This means that you will now need to use /replaceitem. In theory /loot should also be able to be used to dynamically create items for various targets, including a specific slot in a player inventory, but this currently does not work due to this bug. That means that you will have to handle every possible slot and every possible damage value manually. Therefore you need 925 commands (instead of the 3 it would have taken with the fixed bug).

The principle is pretty easy: You can check the item's slot in player NBT and execute a /replaceitem command based on that:

/replaceitem entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] hotbar.0 carrot_on_a_stick{…}
/replaceitem entity @s[nbt={Inventory:[{Slot:1b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] hotbar.1 carrot_on_a_stick{…}
…
/replaceitem entity @s[nbt={Inventory:[{Slot:8b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] hotbar.8 carrot_on_a_stick{…}
/replaceitem entity @s[nbt={Inventory:[{Slot:9b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] inventory.0 carrot_on_a_stick{…}
…
/replaceitem entity @s[nbt={Inventory:[{Slot:35b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] inventory.26 carrot_on_a_stick{…}
/replaceitem entity @s[nbt={Inventory:[{Slot:-106b,id:"minecraft:carrot_on_a_stick",tag:{…}}]}] weapon.offhand carrot_on_a_stick{…}

Replace in the selector with the old data and in the to-be-given item with the new data. That includes the old and the new durability, ranging from 25 to 1 (it breaks at 0).