Paper/Spigot-Server-Patches/0185-Ability-to-apply-mending-to-XP-API.patch
Aikar b3f265d1b1
Updated Upstream (Bukkit/CraftBukkit/Spigot)
Upstream has released updates that appears to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
0ca45a21 #503: Add PlayerHarvestBlockEvent
dfa80a52 SPIGOT-5930: Add PlayerRespawnEvent#isAnchorSpawn

CraftBukkit Changes:
145921e2 #676: Add PlayerHarvestBlockEvent
47abffa2 SPIGOT-5929: Angered zombified piglins do not inherit killed_by_player status
7f6b4f58 SPIGOT-5930: Add PlayerRespawnEvent#isAnchorSpawn
94eff632 SPIGOT-5867, MC-193339: NPE during shutdown when rcon enabled with no password
068618eb SPIGOT-5927: Some items NBT data disappears

Spigot Changes:
beb7d47c Rebuild patches

Fixes #3738
2020-07-03 02:19:33 -04:00

88 lines
4.2 KiB
Diff

From 0000000000000000000000000000000000000000 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 cea65c1d80a7bd303cf28a6fe2bd73d542a19557..92ceb8bb7d7c47e395dfdcfe6d01f807c048ddc1 100644
--- a/src/main/java/net/minecraft/server/EnchantmentManager.java
+++ b/src/main/java/net/minecraft/server/EnchantmentManager.java
@@ -251,6 +251,11 @@ public class EnchantmentManager {
return getEnchantmentLevel(Enchantments.CHANNELING, itemstack) > 0;
}
+ // Paper - OBFHELPER
+ public static @javax.annotation.Nonnull ItemStack getRandomEquippedItemWithEnchant(Enchantment enchantment, EntityLiving entityliving) {
+ Entry<EnumItemSlot, ItemStack> entry = b(enchantment, entityliving);
+ return entry != null ? entry.getValue() : ItemStack.NULL_ITEM;
+ }
@Nullable
public static Entry<EnumItemSlot, ItemStack> b(Enchantment enchantment, EntityLiving entityliving) {
return a(enchantment, entityliving, (itemstack) -> {
diff --git a/src/main/java/net/minecraft/server/EntityExperienceOrb.java b/src/main/java/net/minecraft/server/EntityExperienceOrb.java
index e7fb982913f391bafd608c4626086cd9cab7fad7..7f2d8de12473c5413bbfc10ea0947d6a30802645 100644
--- a/src/main/java/net/minecraft/server/EntityExperienceOrb.java
+++ b/src/main/java/net/minecraft/server/EntityExperienceOrb.java
@@ -249,10 +249,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 758981757996a8c9aa5066640e5b6c293bb7fc64..e2f707e461e2e13f240a30a94c9acf191828219b 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1073,8 +1073,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);
}