From 54b2707ba7ef53f7d7523387d3aeab296152d628 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 27 Dec 2012 20:45:21 -0600 Subject: [PATCH] Fix discrepancies in NBT and ItemMeta. Fixes BUKKIT-3279 An ItemStack gains the tag name "tag" when the stack is serialized to NBT, however items don't have a tag *until* they are serialized at least once. So to solve this, we remove the tag name when loading the NBT data. Another problem with NBT are TagLists, when transferring tag lists between the server and the client the names are lost, and so we simply don't add a name to the tag. --- .../java/net/minecraft/server/ItemStack.java | 3 ++- .../craftbukkit/inventory/CraftMetaBook.java | 6 +----- .../craftbukkit/inventory/CraftMetaItem.java | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minecraft/server/ItemStack.java b/src/main/java/net/minecraft/server/ItemStack.java index a09d8d895f..453ca4ac6e 100644 --- a/src/main/java/net/minecraft/server/ItemStack.java +++ b/src/main/java/net/minecraft/server/ItemStack.java @@ -106,7 +106,8 @@ public final class ItemStack { this.count = nbttagcompound.getByte("Count"); this.damage = nbttagcompound.getShort("Damage"); if (nbttagcompound.hasKey("tag")) { - this.tag = nbttagcompound.getCompound("tag"); + // CraftBukkit - clear name from compound + this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").setName(""); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java index b49974b5f4..476089a1be 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java @@ -91,11 +91,7 @@ class CraftMetaBook extends CraftMetaItem implements BookMeta { } if (hasPages()) { - NBTTagList itemPages = new NBTTagList(BOOK_PAGES.NBT); - for (int i = 1; i < pages.size() + 1; i++) { - itemPages.add(new NBTTagString(String.valueOf(i), pages.get(i - 1))); - } - itemData.set(BOOK_PAGES.NBT, itemPages); + itemData.set(BOOK_PAGES.NBT, createStringList(pages, BOOK_PAGES)); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index f8f9f261fb..cc976ebf6b 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -307,11 +307,7 @@ class CraftMetaItem implements ItemMeta, Repairable { } if (hasLore()) { - NBTTagList list = new NBTTagList(LORE.NBT); - for (int i = 0; i < lore.size(); i++) { - list.add(new NBTTagString(String.valueOf(i), lore.get(i))); - } - setDisplayTag(itemTag, LORE.NBT, list); + setDisplayTag(itemTag, LORE.NBT, createStringList(lore, LORE)); } applyEnchantments(enchantments, itemTag, ENCHANTMENTS); @@ -321,6 +317,19 @@ class CraftMetaItem implements ItemMeta, Repairable { } } + static NBTTagList createStringList(List list, ItemMetaKey key) { + if (list == null || list.isEmpty()) { + return null; + } + + NBTTagList tagList = new NBTTagList(key.NBT); + for (int i = 0; i < list.size(); i++) { + tagList.add(new NBTTagString("", list.get(i))); + } + + return tagList; + } + static void applyEnchantments(Map enchantments, NBTTagCompound tag, ItemMetaKey key) { if (enchantments == null || enchantments.size() == 0) { return;