From b8a0541ccfb75cbfa79b27bf160929008be40850 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Tue, 24 Dec 2024 10:58:58 +0100 Subject: [PATCH] Fix 0 parameter in Player#applyMending (#11802) Prevent division by zero in Player#applyMending by simply using zero instead if the divider itself is zero. This matches vanilla behaviour in the sense that ExperienceOrb#repairPlayerItems, which this logic is lifted from, does pass zero to the enchantment helper and runs its first iteration on repairing. Vanilla is not affected as the computation failing with zero is used to compute the leftover exp value, which is not needed if zero itself is passed in. As the paper impl however exposes said value to the initial event, our code needs to account for the zero division. --- .../main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 18100c2c7c..c9436e82dc 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -1896,7 +1896,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { handle.serverLevel(), itemstack, amount ); int i = Math.min(possibleDurabilityFromXp, itemstack.getDamageValue()); - final int consumedExperience = i * amount / possibleDurabilityFromXp; // Paper - taken from ExperienceOrb#repairPlayerItems + final int consumedExperience = i > 0 ? i * amount / possibleDurabilityFromXp : possibleDurabilityFromXp; // Paper - taken from ExperienceOrb#repairPlayerItems + prevent division by 0 org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i, consumedExperience); i = event.getRepairAmount(); orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);