From 946f1ed01e757565fd7814a4f49b89d63962c69f Mon Sep 17 00:00:00 2001 From: Aikar Date: Wed, 20 Dec 2017 17:36:49 -0500 Subject: [PATCH] Ability to apply mending to XP API This allows plugins that give players the ability to apply the experience points to the Item Mending formula, which will repair an item instead of giving the player experience points. Both an API To standalone mend, and apply mending logic to .giveExp has been added. diff --git a/src/main/java/net/minecraft/server/EnchantmentManager.java b/src/main/java/net/minecraft/server/EnchantmentManager.java index 1822178e8..d96c03a1a 100644 --- a/src/main/java/net/minecraft/server/EnchantmentManager.java +++ b/src/main/java/net/minecraft/server/EnchantmentManager.java @@ -246,6 +246,11 @@ public class EnchantmentManager { return getEnchantmentLevel(Enchantments.CHANNELING, itemstack) > 0; } + // Paper - OBFHELPER + public static @javax.annotation.Nonnull ItemStack getRandomEquippedItemWithEnchant(Enchantment enchantment, EntityLiving entityliving) { + Entry entry = b(enchantment, entityliving); + return entry != null ? entry.getValue() : ItemStack.NULL_ITEM; + } @Nullable public static Entry b(Enchantment enchantment, EntityLiving entityliving) { Map map = enchantment.a(entityliving); diff --git a/src/main/java/net/minecraft/server/EntityExperienceOrb.java b/src/main/java/net/minecraft/server/EntityExperienceOrb.java index b3edb69a9..87c6b77ce 100644 --- a/src/main/java/net/minecraft/server/EntityExperienceOrb.java +++ b/src/main/java/net/minecraft/server/EntityExperienceOrb.java @@ -254,10 +254,12 @@ public class EntityExperienceOrb extends Entity { } } + public int durToXp(int i) { return b(i); } // Paper OBFHELPER private int b(int i) { return i / 2; } + public int xpToDur(int i) { return c(i); } // Paper OBFHELPER private int c(int i) { return i * 2; } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 5b5f2418e..6f47768c9 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -1053,8 +1053,37 @@ public class CraftPlayer extends CraftHumanEntity implements Player { return GameMode.getByValue(getHandle().playerInteractManager.getGameMode().getId()); } + // Paper start @Override - public void giveExp(int exp) { + public int applyMending(int amount) { + EntityPlayer handle = getHandle(); + // Logic copied from EntityExperienceOrb and remapped to unobfuscated methods/properties + net.minecraft.server.ItemStack itemstack = net.minecraft.server.EnchantmentManager.getRandomEquippedItemWithEnchant(net.minecraft.server.Enchantments.MENDING, handle); + if (!itemstack.isEmpty() && itemstack.getItem().usesDurability()) { + + net.minecraft.server.EntityExperienceOrb orb = net.minecraft.server.EntityTypes.EXPERIENCE_ORB.create(handle.world); + orb.value = amount; + orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM; + orb.setPositionRaw(handle.locX(), handle.locY(), handle.locZ()); + + int i = Math.min(orb.xpToDur(amount), itemstack.getDamage()); + org.bukkit.event.player.PlayerItemMendEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemMendEvent(handle, orb, itemstack, i); + i = event.getRepairAmount(); + orb.dead = true; + if (!event.isCancelled()) { + amount -= orb.durToXp(i); + itemstack.setDamage(itemstack.getDamage() - i); + } + } + return amount; + } + + @Override + public void giveExp(int exp, boolean applyMending) { + if (applyMending) { + exp = this.applyMending(exp); + } + // Paper end getHandle().giveExp(exp); } -- 2.16.1.windows.4