Paper/Spigot-Server-Patches/0194-Ability-to-apply-mending-to-XP-API.patch
Zach Brown 799f6af357
More patches for 1.14
Chunk related stuff will need to be revisted later
2019-04-27 19:24:00 -04:00

93 lines
4.0 KiB
Diff

From 773bc84cbbf0f96237d6f806536c5532b340efe1 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
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 6f64b8db3..b6a40e22a 100644
--- a/src/main/java/net/minecraft/server/EnchantmentManager.java
+++ b/src/main/java/net/minecraft/server/EnchantmentManager.java
@@ -241,6 +241,11 @@ public class EnchantmentManager {
return getEnchantmentLevel(Enchantments.CHANNELING, itemstack) > 0;
}
+ // Paper - OBFHELPER
+ public static @Nullable ItemStack getRandomEquippedItemWithEnchant(Enchantment enchantment, EntityLiving entityliving) {
+ Entry<EnumItemSlot, ItemStack> entry = b(enchantment, entityliving);
+ return entry != null ? entry.getValue() : null;
+ }
@Nullable
public static Entry<EnumItemSlot, ItemStack> b(Enchantment enchantment, EntityLiving entityliving) {
Map<EnumItemSlot, ItemStack> 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 704a48c8e..64d71a9a2 100644
--- a/src/main/java/net/minecraft/server/EntityExperienceOrb.java
+++ b/src/main/java/net/minecraft/server/EntityExperienceOrb.java
@@ -251,10 +251,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 bb50ae1ac..bea3ddf31 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1052,8 +1052,39 @@ 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 = new net.minecraft.server.EntityExperienceOrb(handle.world);
+ orb.value = amount;
+ orb.spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.CUSTOM;
+ orb.locX = handle.locX;
+ orb.locY = handle.locY;
+ orb.locZ = 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.21.0