From e726509472bebee3729413eb3dd5b572f927797c Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Fri, 7 Dec 2012 18:29:03 +0100 Subject: [PATCH] Tag Serialization: - Store and restore names (only in Unsafe save) --- .../limitedcreative/store/InvConfStorage.java | 14 +++- .../store/NBTagSerializer.java | 75 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/store/NBTagSerializer.java diff --git a/src/de/jaschastarke/minecraft/limitedcreative/store/InvConfStorage.java b/src/de/jaschastarke/minecraft/limitedcreative/store/InvConfStorage.java index f81d49c..f94318d 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/store/InvConfStorage.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/store/InvConfStorage.java @@ -78,8 +78,14 @@ abstract public class InvConfStorage extends PlayerInventoryStorage { } protected Object serialize(ItemStack is) { - if (Core.plugin.config.getUnsafeStorage()) - return is.serialize(); + if (Core.plugin.config.getUnsafeStorage()) { + Map serialized = is.serialize(); + Map tagData = NBTagSerializer.serializeTags(is); + if (tagData != null) { + serialized.put("tag", tagData); + } + return serialized; + } return is; } @@ -100,6 +106,10 @@ abstract public class InvConfStorage extends PlayerInventoryStorage { } } } + if (sect.contains("tag")) { + Map map = sect.getConfigurationSection("tag").getValues(false); + result = NBTagSerializer.unserializeTags(result, map); + } return result; } else if (is instanceof Map) { return ItemStack.deserialize((Map) is); diff --git a/src/de/jaschastarke/minecraft/limitedcreative/store/NBTagSerializer.java b/src/de/jaschastarke/minecraft/limitedcreative/store/NBTagSerializer.java new file mode 100644 index 0000000..51e005d --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/store/NBTagSerializer.java @@ -0,0 +1,75 @@ +package de.jaschastarke.minecraft.limitedcreative.store; + +import java.util.HashMap; +import java.util.Map; + +import net.minecraft.server.NBTTagCompound; + +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; + +public class NBTagSerializer { + + public static Map serializeTags(ItemStack cis) { + Map map = null; + NBTTagCompound tag = getTag(cis); + if (tag != null && tag.getCompound("display") != null) { + NBTTagCompound display = tag.getCompound("display"); + if (display.hasKey("Name")) { + map = new HashMap(); + map.put("name", display.getString("Name")); + } + } + return map; + } + + public static ItemStack unserializeTags(ItemStack cis, Map data) { + if (data.size() > 0) { + if (!(cis instanceof CraftItemStack)) { + cis = new CraftItemStack(cis); + } + NBTTagCompound nbt = getTag(cis, true); + + if (data.containsKey("name")) { + NBTTagCompound display; + if (nbt.hasKey("display")) { + display = nbt.getCompound("display"); + } else { + display = new NBTTagCompound("display"); + nbt.setCompound("display", display); + } + display.setString("Name", (String) data.get("name")); + } + } + return cis; + } + + private static NBTTagCompound getTag(ItemStack stack, boolean force) { + NBTTagCompound nbt = getTag(stack); + if (nbt == null && force) { + nbt = new NBTTagCompound(); + getMCItemStack(stack).setTag(nbt); + } + return nbt; + } + + private static NBTTagCompound getTag(ItemStack stack) { + net.minecraft.server.ItemStack is = getMCItemStack(stack); + if (is != null) { + return is.getTag(); + } else { + return null; + } + } + + private static net.minecraft.server.ItemStack getMCItemStack(ItemStack stack) { + if (stack instanceof CraftItemStack) { + return getMCItemStack((CraftItemStack) stack); + } else { + return null; + } + } + private static net.minecraft.server.ItemStack getMCItemStack(CraftItemStack stack) { + return stack.getHandle(); + } +}