Some work on the ItemMend events

This commit is contained in:
Bjarne Koll 2024-06-14 16:01:00 +02:00
parent be8b9314b4
commit f548e7fbd5
2 changed files with 26 additions and 27 deletions

View File

@ -36,12 +36,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM; + orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM;
+ orb.setPosRaw(handle.getX(), handle.getY(), handle.getZ()); + orb.setPosRaw(handle.getX(), handle.getY(), handle.getZ());
+ +
+ int i = Math.min(orb.xpToDurability(amount), itemstack.getDamageValue()); + final int possibleDurabilityFromXp = net.minecraft.world.item.enchantment.EnchantmentHelper.modifyDurabilityToRepairFromXp(
+ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.getKey(), i); + 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(); + i = event.getRepairAmount();
+ orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); + orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
+ if (!event.isCancelled()) { + if (!event.isCancelled()) {
+ amount -= orb.durabilityToXp(i); + amount -= i * amount / possibleDurabilityFromXp;
+ itemstack.setDamageValue(itemstack.getDamageValue() - i); + itemstack.setDamageValue(itemstack.getDamageValue() - i);
+ } + }
+ } + }

View File

@ -9,28 +9,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java --- a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
+++ b/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 { @@ -0,0 +0,0 @@ public class ExperienceOrb extends Entity {
int j = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemstack, amount);
if (optional.isPresent()) { int k = Math.min(j, itemstack.getDamageValue());
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
// CraftBukkit start // CraftBukkit start
- org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), k); - org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), k);
- k = event.getRepairAmount(); + // Paper start - mending event
+ org.bukkit.event.player.PlayerItemMendEvent event = CraftEventFactory.callPlayerItemMendEvent(player, this, itemstack, optional.get().inSlot(), durabilityToAddBack, d -> d * amount / xpTranslatedToDurability); // Paper - Expand PlayerItemMendEvent + final int consumedExperience = k * amount / j;
+ durabilityToAddBack = event.getRepairAmount(); // Paper - mending event - obfhelper + 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()) { if (event.isCancelled()) {
return amount; return amount;
} @@ -0,0 +0,0 @@ public class ExperienceOrb extends Entity {
// CraftBukkit end
- itemstack.setDamageValue(itemstack.getDamageValue() - k); itemstack.setDamageValue(itemstack.getDamageValue() - k);
+ itemstack.setDamageValue(itemstack.getDamageValue() - durabilityToAddBack); // Paper - mending event - obfhelper
if (k > 0) { if (k > 0) {
- int l = amount - k * amount / j; - 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) { 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 // 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 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/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 { @@ -0,0 +0,0 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
orb.setPosRaw(handle.getX(), handle.getY(), handle.getZ()); handle.serverLevel(), itemstack, amount
);
int i = Math.min(orb.xpToDurability(amount), itemstack.getDamageValue()); int i = Math.min(possibleDurabilityFromXp, 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);
+ org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, stackEntry.get().inSlot(), i, orb::durabilityToXp); // Paper - Expand PlayerItemMendEvent + 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(); i = event.getRepairAmount();
orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); orb.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
if (!event.isCancelled()) { if (!event.isCancelled()) {
- amount -= orb.durabilityToXp(i); - amount -= i * amount / possibleDurabilityFromXp;
+ amount -= event.getDurabilityToXpOperation().applyAsInt(i); // Paper - Expand PlayerItemMendEvent + amount -= consumedExperience; // Use previously computed variable to reduce diff on change.
itemstack.setDamageValue(itemstack.getDamageValue() - i); 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) {
+ 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(); Player player = (Player) entity.getBukkitEntity();
org.bukkit.inventory.ItemStack bukkitStack = CraftItemStack.asCraftMirror(nmsMendedItem); 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);
+ 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); Bukkit.getPluginManager().callEvent(event);
return event; return event;
} }