Tag Serialization:

- Store and restore names (only in Unsafe save)
This commit is contained in:
Jascha Starke 2012-12-07 18:29:03 +01:00
parent f0e6e853a6
commit e726509472
2 changed files with 87 additions and 2 deletions

View file

@ -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<String, Object> serialized = is.serialize();
Map<String, Object> 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<String, Object> map = sect.getConfigurationSection("tag").getValues(false);
result = NBTagSerializer.unserializeTags(result, map);
}
return result;
} else if (is instanceof Map) {
return ItemStack.deserialize((Map<String, Object>) is);

View file

@ -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<String, Object> serializeTags(ItemStack cis) {
Map<String, Object> 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<String, Object>();
map.put("name", display.getString("Name"));
}
}
return map;
}
public static ItemStack unserializeTags(ItemStack cis, Map<String, Object> 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();
}
}