Minecraft Java Edition Minecraft Commands – How to Subtract Durability From an Item Held in Hand?

minecraft-commandsminecraft-java-edition

I'm in Java in 1.14.3 and I'm trying to create a custom mining tool using the stone hoe. Whenever the player mines with it, I want it to take durability/damage. I created a scoreboard scoreboard objectives add usedTool minecraft.mined:minecraft.stripped_spruce_log. Now, whenever the player mines, I check if they have the stone hoe in their hand: execute as @a[nbt={SelectedItem:{id:"minecraft:stone_hoe"}}] if entity @s[scores={usedTool=..1}] run [...] Is there a way I can subtract durability from the stone hoe after the player has mined with it?

Best Answer

You should use execute store result entity @s SelectedItem.tag.Damage int 1 run <some command that returns a value>. This will set the Damage of the tool (how much it has been used) to the value of whatever command you put after the run. The 1 after the int is a scale factor of 1.

You can make a system where after the item is used, you store the durability somewhere with execute store result score <entity> <objective> run data get entity @s SelectedItem.tag.Damage 1,then use scoreboard players add <entity> <objective> 1 to add one to the value (because you are adding damage, effectively removing durability), and then use the above execute store result entity @s SelectedItem.tag.Damage int 1 run scoreboard players get <entity> <objective> to store that modified value back into the Damage tag.