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.
This commit is contained in:
Bjarne Koll 2024-12-24 10:58:58 +01:00 committed by GitHub
parent d0d0efee02
commit b8a0541ccf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1896,7 +1896,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
handle.serverLevel(), itemstack, amount handle.serverLevel(), itemstack, amount
); );
int i = Math.min(possibleDurabilityFromXp, itemstack.getDamageValue()); 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); org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i, consumedExperience);
i = event.getRepairAmount(); i = event.getRepairAmount();
orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);