From 154a6bba2167c053e24ccdaaaa894e1716f29c8d Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 4 Aug 2018 01:13:10 -0400 Subject: [PATCH] Fix ItemStack.removeEnchantment returning wrong value --- .../Add-ArmorStand-Item-Meta.patch | 10 +++--- .../Handle-Item-Meta-Inconsistencies.patch | 31 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Spigot-Server-Patches/Add-ArmorStand-Item-Meta.patch b/Spigot-Server-Patches/Add-ArmorStand-Item-Meta.patch index 7f3017c94c..e7534fecc4 100644 --- a/Spigot-Server-Patches/Add-ArmorStand-Item-Meta.patch +++ b/Spigot-Server-Patches/Add-ArmorStand-Item-Meta.patch @@ -13,7 +13,7 @@ starting point for future additions in this area. Fixes GH-559 diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java -index c2f26577c..851960afb 100644 +index c2f26577c7..851960afbb 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java @@ -0,0 +0,0 @@ public final class CraftItemFactory implements ItemFactory { @@ -26,7 +26,7 @@ index c2f26577c..851960afb 100644 case CHEST: case TRAPPED_CHEST: diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java -index cdf16e15a..aa99254ff 100644 +index 8ffe18b092..49576e6653 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -0,0 +0,0 @@ public final class CraftItemStack extends ItemStack { @@ -40,7 +40,7 @@ index cdf16e15a..aa99254ff 100644 case TRAPPED_CHEST: diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java new file mode 100644 -index 000000000..dc372f6db +index 0000000000..dc372f6db1 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java @@ -0,0 +0,0 @@ @@ -350,7 +350,7 @@ index 000000000..dc372f6db + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java -index 0cdc8007a..2ab4a1cf4 100644 +index 0cdc8007a4..2ab4a1cf41 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Repairable { @@ -379,7 +379,7 @@ index 0cdc8007a..2ab4a1cf4 100644 } return HANDLED_TAGS; diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java -index 1f537d584..a29731f1d 100644 +index 1f537d5844..a29731f1d1 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -0,0 +0,0 @@ import static org.hamcrest.Matchers.*; diff --git a/Spigot-Server-Patches/Handle-Item-Meta-Inconsistencies.patch b/Spigot-Server-Patches/Handle-Item-Meta-Inconsistencies.patch index e2c0632dc4..69fb5e8550 100644 --- a/Spigot-Server-Patches/Handle-Item-Meta-Inconsistencies.patch +++ b/Spigot-Server-Patches/Handle-Item-Meta-Inconsistencies.patch @@ -18,7 +18,7 @@ For consistency, the old API methods now forward to use the ItemMeta API equivalents, and should deprecate the old API's. diff --git a/src/main/java/net/minecraft/server/ItemStack.java b/src/main/java/net/minecraft/server/ItemStack.java -index 82d72ea15..5047a57e9 100644 +index 82d72ea158..5047a57e9b 100644 --- a/src/main/java/net/minecraft/server/ItemStack.java +++ b/src/main/java/net/minecraft/server/ItemStack.java @@ -0,0 +0,0 @@ public final class ItemStack { @@ -69,7 +69,7 @@ index 82d72ea15..5047a57e9 100644 public boolean hasEnchantments() { diff --git a/src/main/java/net/minecraft/server/NBTTagList.java b/src/main/java/net/minecraft/server/NBTTagList.java -index ca9eb2f3b..576c3b714 100644 +index ca9eb2f3b2..576c3b7141 100644 --- a/src/main/java/net/minecraft/server/NBTTagList.java +++ b/src/main/java/net/minecraft/server/NBTTagList.java @@ -0,0 +0,0 @@ public class NBTTagList extends NBTBase { @@ -86,7 +86,7 @@ index ca9eb2f3b..576c3b714 100644 public NBTTagList() {} diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java -index fb1dc542d..cdf16e15a 100644 +index fb1dc542dd..8ffe18b092 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -0,0 +0,0 @@ import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS; @@ -156,7 +156,13 @@ index fb1dc542d..cdf16e15a 100644 - NBTTagList list = getEnchantmentList(handle), listCopy; - if (list == null) { - return 0; -- } ++ // Paper start - replace entire method ++ final ItemMeta itemMeta = getItemMeta(); ++ int level = itemMeta.getEnchantLevel(ench); ++ if (level > 0) { ++ itemMeta.removeEnchant(ench); ++ setItemMeta(itemMeta); + } - int index = Integer.MIN_VALUE; - int level = Integer.MIN_VALUE; - int size = list.size(); @@ -178,17 +184,9 @@ index fb1dc542d..cdf16e15a 100644 - handle.getTag().remove(ENCHANTMENTS.NBT); - if (handle.getTag().isEmpty()) { - handle.setTag(null); -+ // Paper start - replace entire method, maintain backwards compat of returning previous level. -+ final ItemMeta itemMeta = getItemMeta(); -+ final Iterator iterator = itemMeta.getEnchants().keySet().iterator(); -+ for (int i = 0; iterator.hasNext(); i++) { -+ if (iterator.next().equals(ench)) { -+ itemMeta.removeEnchant(ench); -+ setItemMeta(itemMeta); -+ return i; - } +- } - return level; - } +- } - - // This is workaround for not having an index removal - listCopy = new NBTTagList(); @@ -199,9 +197,8 @@ index fb1dc542d..cdf16e15a 100644 - } - handle.getTag().set(ENCHANTMENTS.NBT, listCopy); - -- return level; + return level; + // Paper end -+ return 0; } @Override @@ -212,7 +209,7 @@ index fb1dc542d..cdf16e15a 100644 static Map getEnchantments(net.minecraft.server.ItemStack item) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java -index c743ae066..0cdc8007a 100644 +index c743ae066e..0cdc8007a4 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -0,0 +0,0 @@ import java.lang.annotation.RetentionPolicy;