From 0e6cf5edaab921a3d54b27009c13a0a304e4db41 Mon Sep 17 00:00:00 2001 From: Aikar Date: Thu, 28 May 2015 23:00:19 -0400 Subject: [PATCH] Handle Item Meta Inconsistencies First, Enchantment order would blow away seeing 2 items as the same, however the Client forces enchantment list in a certain order, as well as does the /enchant command. Anvils can insert it into forced order, causing 2 same items to be considered different. This change makes unhandled NBT Tags and Enchantments use a sorted tree map, so they will always be in a consistent order. Additionally, the old enchantment API was never updated when ItemMeta was added, resulting in 2 different ways to modify an items enchantments. 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 0c54eaa529..c7ef9c6890 100644 --- a/src/main/java/net/minecraft/server/ItemStack.java +++ b/src/main/java/net/minecraft/server/ItemStack.java @@ -7,6 +7,8 @@ import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.util.Collections; +import java.util.Comparator; import java.util.Locale; import java.util.Objects; import java.util.Random; @@ -56,6 +58,22 @@ public final class ItemStack { decimalformat.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ROOT)); return decimalformat; } + // Paper start + private static final java.util.Comparator enchantSorter = java.util.Comparator.comparing(o -> o.getString("id")); + private void processEnchantOrder(NBTTagCompound tag) { + if (tag == null || !tag.hasKeyOfType("Enchantments", 9)) { + return; + } + NBTTagList list = tag.getList("Enchantments", 10); + if (list.size() < 2) { + return; + } + try { + //noinspection unchecked + list.sort((Comparator) enchantSorter); // Paper + } catch (Exception ignored) {} + } + // Paper end public ItemStack(IMaterial imaterial) { this(imaterial, 1); @@ -98,6 +116,7 @@ public final class ItemStack { if (nbttagcompound.hasKeyOfType("tag", 10)) { // CraftBukkit start - make defensive copy as this data may be coming from the save thread this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone(); + processEnchantOrder(this.tag); // Paper this.getItem().a(this.tag); // CraftBukkit end } @@ -575,6 +594,7 @@ public final class ItemStack { // Paper end public void setTag(@Nullable NBTTagCompound nbttagcompound) { this.tag = nbttagcompound; + processEnchantOrder(this.tag); // Paper } public IChatBaseComponent getName() { @@ -651,6 +671,7 @@ public final class ItemStack { nbttagcompound.setString("id", String.valueOf(Enchantment.enchantments.b(enchantment))); nbttagcompound.setShort("lvl", (short) ((byte) i)); nbttaglist.add((NBTBase) nbttagcompound); + processEnchantOrder(nbttagcompound); // Paper } public boolean hasEnchantments() { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index f4672b9a48..8c0409797e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -4,6 +4,7 @@ import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS; import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS_ID; import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS_LVL; +import java.util.Iterator; import java.util.Map; import net.minecraft.server.EnchantmentManager; @@ -186,28 +187,11 @@ public final class CraftItemStack extends ItemStack { public void addUnsafeEnchantment(Enchantment ench, int level) { Validate.notNull(ench, "Cannot add null enchantment"); - if (!makeTag(handle)) { - return; - } - NBTTagList list = getEnchantmentList(handle); - if (list == null) { - list = new NBTTagList(); - handle.getTag().set(ENCHANTMENTS.NBT, list); - } - int size = list.size(); - - for (int i = 0; i < size; i++) { - NBTTagCompound tag = (NBTTagCompound) list.get(i); - String id = tag.getString(ENCHANTMENTS_ID.NBT); - if (id.equals(ench.getKey().toString())) { - tag.setShort(ENCHANTMENTS_LVL.NBT, (short) level); - return; - } - } - NBTTagCompound tag = new NBTTagCompound(); - tag.setString(ENCHANTMENTS_ID.NBT, ench.getKey().toString()); - tag.setShort(ENCHANTMENTS_LVL.NBT, (short) level); - list.add(tag); + // Paper start - Replace whole method + final ItemMeta itemMeta = getItemMeta(); + itemMeta.addEnchant(ench, level, true); + setItemMeta(itemMeta); + // Paper end } static boolean makeTag(net.minecraft.server.ItemStack item) { @@ -224,66 +208,32 @@ public final class CraftItemStack extends ItemStack { @Override public boolean containsEnchantment(Enchantment ench) { - return getEnchantmentLevel(ench) > 0; + return hasItemMeta() && getItemMeta().hasEnchant(ench); // Paper - use meta } @Override public int getEnchantmentLevel(Enchantment ench) { - Validate.notNull(ench, "Cannot find null enchantment"); - if (handle == null) { - return 0; - } - return EnchantmentManager.getEnchantmentLevel(CraftEnchantment.getRaw(ench), handle); + return hasItemMeta() ? getItemMeta().getEnchantLevel(ench) : 0; // Paper - replace entire method with meta } @Override public int removeEnchantment(Enchantment ench) { Validate.notNull(ench, "Cannot remove null enchantment"); - 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(); - - for (int i = 0; i < size; i++) { - NBTTagCompound enchantment = (NBTTagCompound) list.get(i); - String id = enchantment.getString(ENCHANTMENTS_ID.NBT); - if (id.equals(ench.getKey().toString())) { - index = i; - level = 0xffff & enchantment.getShort(ENCHANTMENTS_LVL.NBT); - break; - } - } - - if (index == Integer.MIN_VALUE) { - return 0; - } - if (size == 1) { - handle.getTag().remove(ENCHANTMENTS.NBT); - if (handle.getTag().isEmpty()) { - handle.setTag(null); - } - return level; - } - - // This is workaround for not having an index removal - listCopy = new NBTTagList(); - for (int i = 0; i < size; i++) { - if (i != index) { - listCopy.add(list.get(i)); - } - } - handle.getTag().set(ENCHANTMENTS.NBT, listCopy); - return level; + // Paper end } @Override public Map getEnchantments() { - return getEnchantments(handle); + return hasItemMeta() ? getItemMeta().getEnchants() : ImmutableMap.of(); // Paper - use Item Meta } 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 5464923759..94b19d1095 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -8,12 +8,14 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import net.minecraft.server.IChatBaseComponent; +import com.google.common.collect.ImmutableSortedMap; import net.minecraft.server.NBTBase; import net.minecraft.server.NBTTagCompound; import net.minecraft.server.NBTTagList; @@ -47,6 +49,7 @@ import java.util.Arrays; import java.util.EnumSet; import java.util.HashSet; import java.util.Set; +import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import net.minecraft.server.ChatComponentText; @@ -235,7 +238,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { private IChatBaseComponent displayName; private IChatBaseComponent locName; private List lore; - private Map enchantments; + private EnchantmentMap enchantments; // Paper private int repairCost; private int hideFlag; private boolean unbreakable; @@ -244,7 +247,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { private static final Set HANDLED_TAGS = Sets.newHashSet(); private NBTTagCompound internalTag; - private final Map unhandledTags = new HashMap(); + private final Map unhandledTags = new TreeMap<>(); // Paper CraftMetaItem(CraftMetaItem meta) { if (meta == null) { @@ -259,7 +262,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { } if (meta.enchantments != null) { // Spigot - this.enchantments = new HashMap(meta.enchantments); + this.enchantments = new EnchantmentMap(meta.enchantments); // Paper } this.repairCost = meta.repairCost; @@ -492,13 +495,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { } } - static Map buildEnchantments(NBTTagCompound tag, ItemMetaKey key) { + static EnchantmentMap buildEnchantments(NBTTagCompound tag, ItemMetaKey key) { // Paper if (!tag.hasKey(key.NBT)) { return null; } NBTTagList ench = tag.getList(key.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND); - Map enchantments = new HashMap(ench.size()); + EnchantmentMap enchantments = new EnchantmentMap(); // Paper for (int i = 0; i < ench.size(); i++) { String id = ((NBTTagCompound) ench.get(i)).getString(ENCHANTMENTS_ID.NBT); @@ -573,13 +576,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { void deserializeInternal(NBTTagCompound tag, Object context) { } - static Map buildEnchantments(Map map, ItemMetaKey key) { + static EnchantmentMap buildEnchantments(Map map, ItemMetaKey key) { // Paper Map ench = SerializableMeta.getObject(Map.class, map, key.BUKKIT, true); if (ench == null) { return null; } - Map enchantments = new HashMap(ench.size()); + EnchantmentMap enchantments = new EnchantmentMap(); // Paper for (Map.Entry entry : ench.entrySet()) { // Doctor older enchants String enchantKey = entry.getKey().toString(); @@ -734,13 +737,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { } public Map getEnchants() { - return hasEnchants() ? ImmutableMap.copyOf(enchantments) : ImmutableMap.of(); + return hasEnchants() ? ImmutableSortedMap.copyOfSorted(enchantments) : ImmutableMap.of(); // Paper } public boolean addEnchant(Enchantment ench, int level, boolean ignoreRestrictions) { Validate.notNull(ench, "Enchantment cannot be null"); if (enchantments == null) { - enchantments = new HashMap(4); + enchantments = new EnchantmentMap(); // Paper } if (ignoreRestrictions || level >= ench.getStartLevel() && level <= ench.getMaxLevel()) { @@ -928,7 +931,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { clone.lore = new ArrayList(this.lore); } if (this.enchantments != null) { - clone.enchantments = new HashMap(this.enchantments); + clone.enchantments = new EnchantmentMap(this.enchantments); // Paper } clone.hideFlag = this.hideFlag; clone.unbreakable = this.unbreakable; @@ -1097,6 +1100,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { } } + // Paper start + private static class EnchantmentMap extends TreeMap { + private EnchantmentMap(Map enchantments) { + this(); + putAll(enchantments); + } + + private EnchantmentMap() { + super(Comparator.comparing(o -> o.getKey().toString())); + } + + public EnchantmentMap clone() { + return (EnchantmentMap) super.clone(); + } + } + // Paper end + // Spigot start private final Spigot spigot = new Spigot() { -- 2.18.0