The Bug
The minecraft:item_durability_changed
advancement trigger doesn't trigger when the mending enchantment repairs items.
Steps to Reproduce:
Download the attached data pack.
mc-127340.zip
Create a new world using this data pack.
Once you've entered the world, reload all data packs.
/reload
Give yourself a wooden shovel enchanted with mending that is slightly damaged.
/give @s minecraft:wooden_shovel{Damage:30,Enchantments:[{id:"mending",lvl:1}]}
Switch into survival mode and use the shovel.
→ Notice how the scoreboard increases, therefore indicating that the minecraft:item_durability_changed advancement trigger was triggered.
Make sure you're holding the shovel and summon an experience orb at your position so that the mending enchantment repairs it.
/summon minecraft:experience_orb ~ ~ ~ {Value:1}
→ Notice how the scoreboard doesn't increase, therefore indicating that the
minecraft:item_durability_changed
advancement trigger doesn't trigger when the mending enchantment repairs items.
Expected Behavior
The expected behavior would be that the minecraft:item_durability_changed
advancement trigger would trigger when the mending enchantment repairs items.
Code analysis and fix
Code analysis and fix by @unknown can be found in this comment.
Attachments
Comments 7
Can confirm in 21w37a. I've created and attached a data pack to make reproducing this issue much easier. Here are some additional details regarding this problem.
The Bug:
The minecraft:item_durability_changed advancement trigger doesn't trigger when the mending enchantment repairs items.
Steps to Reproduce:
Download the attached data pack.
Create a new world using this data pack.
Once you've entered the world, reload all data packs.
/reload
Give yourself a wooden shovel enchanted with mending that is slightly damaged.
/give @s minecraft:wooden_shovel{Damage:30,Enchantments:[{id:"mending",lvl:1}]}
Switch into survival mode and use the shovel.
→ ✔ Notice how the scoreboard increases, therefore indicating that the minecraft:item_durability_changed advancement trigger was triggered.
Make sure you're holding the shovel and summon an experience orb at your position so that the mending enchantment repairs it.
/summon minecraft:experience_orb ~ ~ ~ {Value:1}
→ ❌ Notice how the scoreboard doesn't increase, therefore indicating that the minecraft:item_durability_changed advancement trigger doesn't trigger when the mending enchantment repairs items.
Expected Behavior:
The expected behavior would be that the minecraft:item_durability_changed advancement trigger would trigger when the mending enchantment repairs items.
Here's a code analysis of this issue along with a potential fix regarding this issue.
Code Analysis:
The following is based on a decompiled version of Minecraft 1.18.1 using MCP-Reborn.
net.minecraft.world.entity.ExperienceOrb.java
public class ExperienceOrb extends Entity {
...
private int repairPlayerItems(Player $p, int $i) {
Entry<EquipmentSlot, ItemStack> entry = EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, $p, ItemStack::isDamaged);
if (entry != null) {
ItemStack itemstack = entry.getValue();
int i = Math.min(this.xpToDurability(this.value), itemstack.getDamageValue());
itemstack.setDamageValue(itemstack.getDamageValue() - i);
int j = $i - this.durabilityToXp(i);
return j > 0 ? this.repairPlayerItems($p, j) : 0;
} else {
return $i;
}
}
...
If we look at the above class, we can see that when experience orbs repair the player's items, the ITEM_DURABILITY_CHANGED
criteria trigger, is never triggered.
Potential Fix:
Simply adding a line of code in the appropriate place that triggers the ITEM_DURABILITY_CHANGED
advancement trigger, should resolve this problem. The following line of code could be used in order to fix this:
CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(...)
Still in 18w50a; but the
xp_orb
needs changing toexperience_orb