From 845336b511bdd3d3d9b8a67346503acc4ad3396a Mon Sep 17 00:00:00 2001 From: Nathan Wolf Date: Thu, 16 Apr 2015 07:51:08 -0700 Subject: Serialize full skull item profile data in internal blob --- .../craftbukkit/inventory/CraftMetaBlockState.java | 15 +++++++++++++ .../craftbukkit/inventory/CraftMetaItem.java | 25 +++++++++++----------- .../craftbukkit/inventory/CraftMetaSkull.java | 25 +++++++++++++++++++++- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java index 32d461a3..d60686d6 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java @@ -4,6 +4,7 @@ import com.google.common.base.Objects; import com.google.common.collect.ImmutableMap; import java.util.Map; import net.minecraft.server.BlockJukeBox; +import net.minecraft.server.NBTBase; import net.minecraft.server.NBTTagCompound; import net.minecraft.server.TileEntity; import net.minecraft.server.TileEntityBanner; @@ -96,6 +97,20 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta } } + @Override + void deserializeInternal(NBTTagCompound tag) { + if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, 10)) { + blockEntityTag = tag.getCompound(BLOCK_ENTITY_TAG.NBT); + } + } + + @Override + void serializeInternal(final Map internalTags) { + if (blockEntityTag != null) { + internalTags.put(BLOCK_ENTITY_TAG.NBT, blockEntityTag); + } + } + @Override ImmutableMap.Builder serialize(ImmutableMap.Builder builder) { super.serialize(builder); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 8b18e1f3..5f123e32 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -376,16 +376,12 @@ class CraftMetaItem implements ItemMeta, Repairable { ByteArrayInputStream buf = new ByteArrayInputStream(Base64.decodeBase64(internal)); try { NBTTagCompound tag = NBTCompressedStreamTools.a(buf); + deserializeInternal(tag); Set keys = tag.c(); for (String key : keys) { if (!getHandledTags().contains(key)) { unhandledTags.put(key, tag.get(key)); } - if (key.equals(CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT) && this instanceof CraftMetaBlockState) { - if (tag.hasKeyOfType(key, 10)) { - ((CraftMetaBlockState) this).blockEntityTag = tag.getCompound(key); - } - } } } catch (IOException ex) { Logger.getLogger(CraftMetaItem.class.getName()).log(Level.SEVERE, null, ex); @@ -393,6 +389,9 @@ class CraftMetaItem implements ItemMeta, Repairable { } } + void deserializeInternal(NBTTagCompound tag) { + } + static Map buildEnchantments(Map map, ItemMetaKey key) { Map ench = SerializableMeta.getObject(Map.class, map, key.BUKKIT, true); if (ench == null) { @@ -715,17 +714,13 @@ class CraftMetaItem implements ItemMeta, Repairable { builder.put(HIDEFLAGS.BUKKIT, hideFlags); } - if (!unhandledTags.isEmpty() || this instanceof CraftMetaBlockState) { + final Map internalTags = new HashMap(unhandledTags); + serializeInternal(internalTags); + if (!internalTags.isEmpty()) { NBTTagCompound internal = new NBTTagCompound(); - for (Map.Entry e : unhandledTags.entrySet()) { + for (Map.Entry e : internalTags.entrySet()) { internal.set(e.getKey(), e.getValue()); } - if (this instanceof CraftMetaBlockState) { - CraftMetaBlockState bs = ((CraftMetaBlockState) this); - if (bs.blockEntityTag != null) { - internal.set(CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT, bs.blockEntityTag); - } - } try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); NBTCompressedStreamTools.a(internal, buf); @@ -738,6 +733,9 @@ class CraftMetaItem implements ItemMeta, Repairable { return builder; } + void serializeInternal(final Map unhandledTags) { + } + static void serializeEnchantments(Map enchantments, ImmutableMap.Builder builder, ItemMetaKey key) { if (enchantments == null || enchantments.isEmpty()) { return; @@ -804,6 +802,7 @@ class CraftMetaItem implements ItemMeta, Repairable { CraftMetaMap.MAP_SCALING.NBT, CraftMetaPotion.POTION_EFFECTS.NBT, CraftMetaSkull.SKULL_OWNER.NBT, + CraftMetaSkull.SKULL_PROFILE.NBT, CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT, CraftMetaBook.BOOK_TITLE.NBT, CraftMetaBook.BOOK_AUTHOR.NBT, diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java index 89912bcf..ead8a98c 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java @@ -3,6 +3,7 @@ package org.bukkit.craftbukkit.inventory; import java.util.Map; import net.minecraft.server.GameProfileSerializer; +import net.minecraft.server.NBTBase; import net.minecraft.server.NBTTagCompound; import org.bukkit.Material; @@ -15,6 +16,10 @@ import com.mojang.authlib.GameProfile; @DelegateDeserialization(SerializableMeta.class) class CraftMetaSkull extends CraftMetaItem implements SkullMeta { + + @ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT) + static final ItemMetaKey SKULL_PROFILE = new ItemMetaKey("SkullProfile"); + static final ItemMetaKey SKULL_OWNER = new ItemMetaKey("SkullOwner", "skull-owner"); static final int MAX_OWNER_LENGTH = 16; @@ -41,7 +46,25 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { CraftMetaSkull(Map map) { super(map); - setOwner(SerializableMeta.getString(map, SKULL_OWNER.BUKKIT, true)); + if (profile == null) { + setOwner(SerializableMeta.getString(map, SKULL_OWNER.BUKKIT, true)); + } + } + + @Override + void deserializeInternal(NBTTagCompound tag) { + if (tag.hasKeyOfType(SKULL_PROFILE.NBT, 10)) { + profile = GameProfileSerializer.deserialize(tag.getCompound(SKULL_PROFILE.NBT)); + } + } + + @Override + void serializeInternal(final Map internalTags) { + if (profile != null) { + NBTTagCompound nbtData = new NBTTagCompound(); + GameProfileSerializer.serialize(nbtData, profile); + internalTags.put(SKULL_PROFILE.NBT, nbtData); + } } @Override -- cgit v1.2.3