From b64d8524968ef0984bb78dd026192e83b56149d8 Mon Sep 17 00:00:00 2001
From: Jacob Martin <jjm_223@hotmail.com>
Date: Sat, 3 Dec 2016 10:51:53 +1100
Subject: [PATCH] #326: Convert BlockMeta to handle older serialized items.

---
 nms-patches/DataInspectorBlockEntity.patch    | 12 +++++++++
 nms-patches/ItemStack.patch                   | 27 ++++++++++++-------
 .../craftbukkit/inventory/CraftItemStack.java |  1 +
 3 files changed, 30 insertions(+), 10 deletions(-)
 create mode 100644 nms-patches/DataInspectorBlockEntity.patch

diff --git a/nms-patches/DataInspectorBlockEntity.patch b/nms-patches/DataInspectorBlockEntity.patch
new file mode 100644
index 0000000000..182ddf44da
--- /dev/null
+++ b/nms-patches/DataInspectorBlockEntity.patch
@@ -0,0 +1,12 @@
+--- a/net/minecraft/server/DataInspectorBlockEntity.java
++++ b/net/minecraft/server/DataInspectorBlockEntity.java
+@@ -32,7 +32,8 @@
+                 boolean flag;
+ 
+                 if (s1 == null) {
+-                    DataInspectorBlockEntity.a.warn("Unable to resolve BlockEntity for ItemInstance: {}", new Object[] { s});
++                    // CraftBukkit - Remove unnecessary warning (occurs when deserializing a Shulker Box item)
++                    // DataInspectorBlockEntity.a.warn("Unable to resolve BlockEntity for ItemInstance: {}", new Object[] { s});
+                     flag = false;
+                 } else {
+                     flag = !nbttagcompound2.hasKey("id");
diff --git a/nms-patches/ItemStack.patch b/nms-patches/ItemStack.patch
index 98cf9d8477..10ffe6462a 100644
--- a/nms-patches/ItemStack.patch
+++ b/nms-patches/ItemStack.patch
@@ -20,18 +20,15 @@
  public final class ItemStack {
  
      public static final ItemStack a = new ItemStack((Item) null);
-@@ -46,28 +59,49 @@
+@@ -46,28 +59,56 @@
          this.item = item;
          this.damage = j;
          this.count = i;
 +        // CraftBukkit start - Pass to setData to do filtering
 +        if (MinecraftServer.getServer() != null) {
 +            this.setData(j);
-+            NBTTagCompound savedStack = new NBTTagCompound();
-+            this.save(savedStack);
-+            MinecraftServer.getServer().getDataConverterManager().a(DataConverterTypes.ITEM_INSTANCE, savedStack); // PAIL: convert
-+            this.load(savedStack);
 +        }
++        this.convertStack();
 +        // CraftBukkit end
          if (this.damage < 0) {
 -            this.damage = 0;
@@ -41,6 +38,16 @@
          this.F();
      }
  
++    // Called to run this stack through the data converter to handle older storage methods and serialized items
++    public void convertStack() {
++        if (MinecraftServer.getServer() != null) {
++            NBTTagCompound savedStack = new NBTTagCompound();
++            this.save(savedStack);
++            MinecraftServer.getServer().getDataConverterManager().a(DataConverterTypes.ITEM_INSTANCE, savedStack); // PAIL: convert
++            this.load(savedStack);
++        }
++    }
++
      private void F() {
 +        if (this.g && this == ItemStack.a) throw new AssertionError("TRAP"); // CraftBukkit
          this.g = this.isEmpty();
@@ -75,7 +82,7 @@
          this.F();
      }
  
-@@ -94,11 +128,138 @@
+@@ -94,11 +135,138 @@
      }
  
      public EnumInteractionResult placeItem(EntityHuman entityhuman, World world, BlockPosition blockposition, EnumHand enumhand, EnumDirection enumdirection, float f, float f1, float f2) {
@@ -215,7 +222,7 @@
  
          return enuminteractionresult;
      }
-@@ -122,7 +283,7 @@
+@@ -122,7 +290,7 @@
          nbttagcompound.setByte("Count", (byte) this.count);
          nbttagcompound.setShort("Damage", (short) this.damage);
          if (this.tag != null) {
@@ -224,7 +231,7 @@
          }
  
          return nbttagcompound;
-@@ -157,11 +318,30 @@
+@@ -157,11 +325,30 @@
      }
  
      public void setData(int i) {
@@ -257,7 +264,7 @@
      }
  
      public int k() {
-@@ -205,6 +385,11 @@
+@@ -205,6 +392,11 @@
                          entityhuman.b(StatisticList.c(this.item));
                      }
  
@@ -269,7 +276,7 @@
                      this.damage = 0;
                  }
  
-@@ -509,6 +694,12 @@
+@@ -509,6 +701,12 @@
          nbttaglist.add(nbttagcompound);
      }
  
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
index 2183cf2073..b6bce9426b 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
@@ -423,6 +423,7 @@ public final class CraftItemStack extends ItemStack {
         item.setTag(tag);
 
         ((CraftMetaItem) itemMeta).applyToItem(tag);
+        item.convertStack();
 
         return true;
     }