From f548e7fbd533f110ba55a37751fb1da055520f3c Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Fri, 14 Jun 2024 16:01:00 +0200 Subject: [PATCH] Some work on the ItemMend events --- .../Ability-to-apply-mending-to-XP-API.patch | 9 ++-- .../server/Expand-PlayerItemMendEvent.patch | 44 +++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/patches/server/Ability-to-apply-mending-to-XP-API.patch b/patches/server/Ability-to-apply-mending-to-XP-API.patch index 286d4a6890..3ac3247762 100644 --- a/patches/server/Ability-to-apply-mending-to-XP-API.patch +++ b/patches/server/Ability-to-apply-mending-to-XP-API.patch @@ -36,12 +36,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM; + orb.setPosRaw(handle.getX(), handle.getY(), handle.getZ()); + -+ int i = Math.min(orb.xpToDurability(amount), itemstack.getDamageValue()); -+ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.getKey(), i); ++ final int possibleDurabilityFromXp = net.minecraft.world.item.enchantment.EnchantmentHelper.modifyDurabilityToRepairFromXp( ++ handle.serverLevel(), itemstack, amount ++ ); ++ int i = Math.min(possibleDurabilityFromXp, itemstack.getDamageValue()); ++ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i); + i = event.getRepairAmount(); + orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); + if (!event.isCancelled()) { -+ amount -= orb.durabilityToXp(i); ++ amount -= i * amount / possibleDurabilityFromXp; + itemstack.setDamageValue(itemstack.getDamageValue() - i); + } + } diff --git a/patches/server/Expand-PlayerItemMendEvent.patch b/patches/server/Expand-PlayerItemMendEvent.patch index 32f9780a51..f3afab364d 100644 --- a/patches/server/Expand-PlayerItemMendEvent.patch +++ b/patches/server/Expand-PlayerItemMendEvent.patch @@ -9,28 +9,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java +++ b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java @@ -0,0 +0,0 @@ public class ExperienceOrb extends Entity { - - if (optional.isPresent()) { - ItemStack itemstack = ((EnchantedItemInUse) optional.get()).itemStack(); -- int j = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemstack, amount); -- int k = Math.min(j, itemstack.getDamageValue()); -+ int j = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemstack, amount);; int xpTranslatedToDurability = j; // Paper - mending event - obfhelper -+ int k = Math.min(j, itemstack.getDamageValue()); int durabilityToAddBack = k; // Paper - mending event - obfhelper + int j = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemstack, amount); + int k = Math.min(j, itemstack.getDamageValue()); // CraftBukkit start - org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), k); -- k = event.getRepairAmount(); -+ org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), durabilityToAddBack, d -> d * amount / xpTranslatedToDurability); // Paper - Expand PlayerItemMendEvent -+ durabilityToAddBack = event.getRepairAmount(); // Paper - mending event - obfhelper ++ // Paper start - mending event ++ final int consumedExperience = k * amount / j; ++ org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), k, consumedExperience); ++ // Paper end - mending event + k = event.getRepairAmount(); if (event.isCancelled()) { return amount; - } - // CraftBukkit end +@@ -0,0 +0,0 @@ public class ExperienceOrb extends Entity { -- itemstack.setDamageValue(itemstack.getDamageValue() - k); -+ itemstack.setDamageValue(itemstack.getDamageValue() - durabilityToAddBack); // Paper - mending event - obfhelper + itemstack.setDamageValue(itemstack.getDamageValue() - k); if (k > 0) { - int l = amount - k * amount / j; -+ int l = amount - event.getDurabilityToXpOperation().applyAsInt(durabilityToAddBack); // Paper - mending event - obfhelper ++ int l = amount - k * amount / j; // Paper - diff on change - expand PlayerMendEvents if (l > 0) { // this.value = l; // CraftBukkit - update exp value of orb for PlayerItemMendEvent calls // Paper - the value field should not be mutated here because it doesn't take "count" into account @@ -39,16 +34,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -0,0 +0,0 @@ public class CraftPlayer extends CraftHumanEntity implements Player { - orb.setPosRaw(handle.getX(), handle.getY(), handle.getZ()); - - int i = Math.min(orb.xpToDurability(amount), itemstack.getDamageValue()); -- org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.getKey(), i); -+ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i, orb::durabilityToXp); // Paper - Expand PlayerItemMendEvent + handle.serverLevel(), itemstack, amount + ); + int i = Math.min(possibleDurabilityFromXp, itemstack.getDamageValue()); +- org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i); ++ final int consumedExperience = i * amount / possibleDurabilityFromXp; // Paper - taken from ExperienceOrb#repairPlayerItems ++ 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); if (!event.isCancelled()) { -- amount -= orb.durabilityToXp(i); -+ amount -= event.getDurabilityToXpOperation().applyAsInt(i); // Paper - Expand PlayerItemMendEvent +- amount -= i * amount / possibleDurabilityFromXp; ++ amount -= consumedExperience; // Use previously computed variable to reduce diff on change. itemstack.setDamageValue(itemstack.getDamageValue() - i); } } @@ -61,11 +57,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 } - public static PlayerItemMendEvent callPlayerItemMendEvent(net.minecraft.world.entity.player.Player entity, net.minecraft.world.entity.ExperienceOrb orb, net.minecraft.world.item.ItemStack nmsMendedItem, net.minecraft.world.entity.EquipmentSlot slot, int repairAmount) { -+ public static PlayerItemMendEvent callPlayerItemMendEvent(net.minecraft.world.entity.player.Player entity, net.minecraft.world.entity.ExperienceOrb orb, net.minecraft.world.item.ItemStack nmsMendedItem, net.minecraft.world.entity.EquipmentSlot slot, int repairAmount, java.util.function.IntUnaryOperator durabilityToXpOp) { // Paper - Expand PlayerItemMendEvent ++ public static PlayerItemMendEvent callPlayerItemMendEvent(net.minecraft.world.entity.player.Player entity, net.minecraft.world.entity.ExperienceOrb orb, net.minecraft.world.item.ItemStack nmsMendedItem, net.minecraft.world.entity.EquipmentSlot slot, int repairAmount, int consumedExperience) { // Paper - Expand PlayerItemMendEvent Player player = (Player) entity.getBukkitEntity(); org.bukkit.inventory.ItemStack bukkitStack = CraftItemStack.asCraftMirror(nmsMendedItem); - PlayerItemMendEvent event = new PlayerItemMendEvent(player, bukkitStack, CraftEquipmentSlot.getSlot(slot), (ExperienceOrb) orb.getBukkitEntity(), repairAmount); -+ PlayerItemMendEvent event = new PlayerItemMendEvent(player, bukkitStack, CraftEquipmentSlot.getSlot(slot), (ExperienceOrb) orb.getBukkitEntity(), repairAmount, durabilityToXpOp); // Paper - Expand PlayerItemMendEvent ++ PlayerItemMendEvent event = new PlayerItemMendEvent(player, bukkitStack, CraftEquipmentSlot.getSlot(slot), (ExperienceOrb) orb.getBukkitEntity(), repairAmount, consumedExperience); // Paper - Expand PlayerItemMendEvent Bukkit.getPluginManager().callEvent(event); return event; }