From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 22 Nov 2016 00:40:42 -0500
Subject: [PATCH] Fix client rendering skulls from same user

See: https://github.com/PaperMC/Paper/issues/1304

Changes the UUID sent to client to be based on either
the texture payload, or random.

This allows the client to render multiple skull textures from the same user,
for when different skins were used when skull was made.

diff --git a/src/main/java/net/minecraft/server/ItemStack.java b/src/main/java/net/minecraft/server/ItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 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 {
     private int g;
     @Deprecated
     private Item item;
-    private NBTTagCompound tag;
+    NBTTagCompound tag; // Paper -> package private
     private boolean j;
     private Entity k;
     private ShapeDetectorBlock l;
diff --git a/src/main/java/net/minecraft/server/PacketDataSerializer.java b/src/main/java/net/minecraft/server/PacketDataSerializer.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/PacketDataSerializer.java
+++ b/src/main/java/net/minecraft/server/PacketDataSerializer.java
@@ -0,0 +0,0 @@ public class PacketDataSerializer extends ByteBuf {
             if (item.usesDurability() || item.n()) {
                 // Spigot start - filter
                 itemstack = itemstack.cloneItemStack();
-                CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
+                //CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack)); // Paper - This is no longer needed due to NBT being supported
                 // Spigot end
                 nbttagcompound = itemstack.getTag();
+                // Paper start
+                if (nbttagcompound != null && nbttagcompound.hasKeyOfType("SkullOwner", 10)) {
+                    NBTTagCompound owner = nbttagcompound.getCompound("SkullOwner");
+                    if (owner.hasKey("Id")) {
+                        nbttagcompound.setString("SkullOwnerOrig", owner.getString("Id"));
+                        TileEntitySkull.sanitizeUUID(owner);
+                    }
+                }
+                // Paper end
             }
 
             this.a(nbttagcompound);
@@ -0,0 +0,0 @@ public class PacketDataSerializer extends ByteBuf {
             itemstack.setTag(this.l());
             // CraftBukkit start
             if (itemstack.getTag() != null) {
-                CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
+                // Paper start - Fix skulls of same owner - restore orig ID since we changed it on send to client
+                if (itemstack.tag.hasKey("SkullOwnerOrig")) {
+                    NBTTagCompound owner = itemstack.tag.getCompound("SkullOwner");
+                    String ownerOrig = itemstack.tag.getString("SkullOwnerOrig");
+                    if (!owner.isEmpty() && !ownerOrig.isEmpty()) {
+                        owner.setString("Id", ownerOrig);
+                    }
+                    itemstack.tag.remove("SkullOwnerOrig");
+                }
+                // Paper end
+                // CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack)); // Paper - This is no longer needed due to NBT being supported
             }
             // CraftBukkit end
             return itemstack;
diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
@@ -0,0 +0,0 @@ public class PacketPlayOutMapChunk implements Packet<PacketListenerPlayOut> {
 
             if (this.f() || (i & 1 << j) != 0) {
                 NBTTagCompound nbttagcompound = tileentity.b();
+                if (tileentity instanceof TileEntitySkull) { TileEntitySkull.sanitizeTileEntityUUID(nbttagcompound); } // Paper
 
                 this.g.add(nbttagcompound);
             }
diff --git a/src/main/java/net/minecraft/server/TileEntitySkull.java b/src/main/java/net/minecraft/server/TileEntitySkull.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/TileEntitySkull.java
+++ b/src/main/java/net/minecraft/server/TileEntitySkull.java
@@ -0,0 +0,0 @@ public class TileEntitySkull extends TileEntity /*implements ITickable*/ { // Pa
     @Nullable
     @Override
     public PacketPlayOutTileEntityData getUpdatePacket() {
-        return new PacketPlayOutTileEntityData(this.position, 4, this.b());
+        return new PacketPlayOutTileEntityData(this.position, 4, sanitizeTileEntityUUID(this.b())); // Paper
     }
 
+    // Paper start
+    static NBTTagCompound sanitizeTileEntityUUID(NBTTagCompound cmp) {
+        NBTTagCompound owner = cmp.getCompound("Owner");
+        if (!owner.isEmpty()) {
+            sanitizeUUID(owner);
+        }
+        return cmp;
+    }
+
+    static void sanitizeUUID(NBTTagCompound owner) {
+        NBTTagCompound properties = owner.getCompound("Properties");
+        NBTTagList list = null;
+        if (!properties.isEmpty()) {
+            list = properties.getList("textures", 10);
+        }
+
+        if (list != null && !list.isEmpty()) {
+            String textures = ((NBTTagCompound)list.get(0)).getString("Value");
+            if (textures != null && textures.length() > 3) {
+                String uuid = UUID.nameUUIDFromBytes(textures.getBytes()).toString();
+                owner.setString("Id", uuid);
+                return;
+            }
+        }
+        owner.setString("Id", UUID.randomUUID().toString());
+    }
+    // Paper end
+
     @Override
     public NBTTagCompound b() {
         return this.save(new NBTTagCompound());
