From 9f3aaa3a3072edf8789f2e63e8e2558410bb5794 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 14 May 2012 22:33:59 +0200 Subject: [PATCH] Item adapters for Gson and minor tweaks. --- src/com/massivecraft/mcore3/MCore.java | 8 +- .../mcore3/gson/InventoryTypeAdapter.java | 182 ++++++------------ .../mcore3/gson/ItemStackAdapter.java | 122 ++++++++++++ src/com/massivecraft/mcore3/util/Txt.java | 7 +- 4 files changed, 199 insertions(+), 120 deletions(-) create mode 100644 src/com/massivecraft/mcore3/gson/ItemStackAdapter.java diff --git a/src/com/massivecraft/mcore3/MCore.java b/src/com/massivecraft/mcore3/MCore.java index bdc0fe65..c2dc2e7d 100644 --- a/src/com/massivecraft/mcore3/MCore.java +++ b/src/com/massivecraft/mcore3/MCore.java @@ -7,9 +7,13 @@ import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import com.massivecraft.mcore3.cmd.Cmd; +import com.massivecraft.mcore3.gson.InventoryTypeAdapter; +import com.massivecraft.mcore3.gson.ItemStackAdapter; import com.massivecraft.mcore3.lib.gson.GsonBuilder; import com.massivecraft.mcore3.persist.One; import com.massivecraft.mcore3.persist.Persist; @@ -108,7 +112,9 @@ public class MCore extends JavaPlugin return new GsonBuilder() .setPrettyPrinting() .disableHtmlEscaping() - .excludeFieldsWithModifiers(Modifier.TRANSIENT); + .excludeFieldsWithModifiers(Modifier.TRANSIENT) + .registerTypeAdapter(ItemStack.class, new ItemStackAdapter()) + .registerTypeAdapter(Inventory.class, new InventoryTypeAdapter()); } // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore3/gson/InventoryTypeAdapter.java b/src/com/massivecraft/mcore3/gson/InventoryTypeAdapter.java index 0a210562..db1a467b 100644 --- a/src/com/massivecraft/mcore3/gson/InventoryTypeAdapter.java +++ b/src/com/massivecraft/mcore3/gson/InventoryTypeAdapter.java @@ -1,13 +1,8 @@ package com.massivecraft.mcore3.gson; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map.Entry; -import java.util.logging.Logger; import org.bukkit.craftbukkit.inventory.CraftInventoryCustom; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -22,126 +17,77 @@ import com.massivecraft.mcore3.lib.gson.JsonSerializer; public class InventoryTypeAdapter implements JsonDeserializer, JsonSerializer { - private static Logger logger = Logger.getLogger(InventoryTypeAdapter.class.getName()); - private static final String SIZE = "size"; - private static final String TYPE = "type"; - private static final String AMOUNT = "amount"; - private static final String DAMAGE = "damage"; - private static final String ENCHANTMENTS = "enchantments"; + // -------------------------------------------- // + // FIELD NAME CONSTANTS + // -------------------------------------------- // - @Override - public Inventory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - try - { - Collection itemStacks = new ArrayList(); - - JsonObject jsonInventory = json.getAsJsonObject(); - int size = jsonInventory.get(SIZE).getAsInt(); - - for (int i = 0; i < size; i++) - { - // Fetch the jsonItemStack or mark it as empty and continue - String stackIdx = String.valueOf(i); - if ( ! jsonInventory.has(stackIdx)) - { - itemStacks.add(null); - continue; - } - JsonObject jsonItemStack = jsonInventory.getAsJsonObject(stackIdx); - - // Populate values - int type = jsonItemStack.get(TYPE).getAsInt(); - int amount = 1; - short damage = 0; - - if (jsonItemStack.has(AMOUNT)) - { - amount = jsonItemStack.get(AMOUNT).getAsInt(); - } - - if (jsonItemStack.has(DAMAGE)) - { - damage = jsonItemStack.get(DAMAGE).getAsShort(); - } - - // Create Non enchanted stack - ItemStack stack = new ItemStack(type, amount, damage); - - // Add enchantments if there are any - if (jsonItemStack.has(ENCHANTMENTS)) - { - JsonObject jsonEnchantments = jsonItemStack.get(ENCHANTMENTS).getAsJsonObject(); - for (Entry enchantmentEntry: jsonEnchantments.entrySet()) - { - int enchantmentId = Integer.valueOf(enchantmentEntry.getKey()); - Integer enchantmentLevel = Integer.valueOf(enchantmentEntry.getValue().getAsString()); - stack.addEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel); - } - } - itemStacks.add(stack); - } - - - Inventory ret = new CraftInventoryCustom(null, itemStacks.size(), "items"); - ret.setContents(itemStacks.toArray(new ItemStack[0])); - return ret; - } - catch (Exception ex) - { - ex.printStackTrace(); - logger.warning("Error encountered while deserializing an inventory."); - return null; - } - } + public static final String SIZE = "size"; + // -------------------------------------------- // + // IMPLEMENTATION + // -------------------------------------------- // + @Override public JsonElement serialize(Inventory src, Type typeOfSrc, JsonSerializationContext context) { - try - { - JsonObject jsonInventory = new JsonObject(); - ItemStack[] itemStacks = src.getContents(); - jsonInventory.add(SIZE, new JsonPrimitive(itemStacks.length)); - - for (int i = 0; i < itemStacks.length; i++) - { - ItemStack itemStack = itemStacks[i]; - if (itemStack == null) continue; - if (itemStack.getTypeId() == 0) continue; - if (itemStack.getAmount() == 0) continue; - JsonObject jsonItemStack = new JsonObject(); - - jsonItemStack.addProperty(TYPE, itemStack.getTypeId()); - if (itemStack.getAmount() != 1) - { - jsonItemStack.addProperty(AMOUNT, itemStack.getAmount()); - } - if (itemStack.getDurability() != 0) // Durability is a weird name since it is the amount of damage. - { - jsonItemStack.addProperty(DAMAGE, itemStack.getDurability()); - } - if (itemStack.getEnchantments().size() > 0) - { - JsonObject jsonEnchantments = new JsonObject(); - for (Entry entry : itemStack.getEnchantments().entrySet()) - { - jsonEnchantments.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue()); - } - jsonItemStack.add(ENCHANTMENTS, jsonEnchantments); - } - jsonInventory.add(String.valueOf(i), jsonItemStack); - } - return jsonInventory; - } - catch (Exception ex) - { - ex.printStackTrace(); - logger.warning("Error encountered while serializing an inventory."); - return null; - } + return serialize(src); } + @Override + public Inventory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + return deserialize(json); + } + + // -------------------------------------------- // + // STATIC LOGIC + // -------------------------------------------- // + + public static JsonElement serialize(Inventory src) + { + JsonObject jsonInventory = new JsonObject(); + ItemStack[] itemStacks = src.getContents(); + jsonInventory.add(SIZE, new JsonPrimitive(itemStacks.length)); + + for (int i = 0; i < itemStacks.length; i++) + { + ItemStack itemStack = itemStacks[i]; + JsonObject jsonItemStack = ItemStackAdapter.serialize(itemStack); + if (jsonItemStack == null) continue; + jsonInventory.add(String.valueOf(i), jsonItemStack); + } + + return jsonInventory; + } + + public static Inventory deserialize(JsonElement json) + { + if ( ! json.isJsonObject()) return null; + JsonObject jsonInventory = json.getAsJsonObject(); + + if ( ! jsonInventory.has(SIZE)) return null; + int size = jsonInventory.get(SIZE).getAsInt(); + + ItemStack[] itemStacks = new ItemStack[size]; + + for (int i = 0; i < size; i++) + { + // Fetch the jsonItemStack or mark it as empty and continue + String stackIdx = String.valueOf(i); + JsonElement jsonItemStack = jsonInventory.get(stackIdx); + ItemStack itemStack = ItemStackAdapter.deserialize(jsonItemStack); + itemStacks[i] = itemStack; + } + + Inventory ret = new CraftInventoryCustom(null, size, "items"); + ret.setContents(itemStacks); + return ret; + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + // This utility is nice to have in many cases :) public static boolean isInventoryEmpty(Inventory inv) { diff --git a/src/com/massivecraft/mcore3/gson/ItemStackAdapter.java b/src/com/massivecraft/mcore3/gson/ItemStackAdapter.java new file mode 100644 index 00000000..286c57d2 --- /dev/null +++ b/src/com/massivecraft/mcore3/gson/ItemStackAdapter.java @@ -0,0 +1,122 @@ +package com.massivecraft.mcore3.gson; + +import java.lang.reflect.Type; +import java.util.Map.Entry; + +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; + +import com.massivecraft.mcore3.lib.gson.JsonDeserializationContext; +import com.massivecraft.mcore3.lib.gson.JsonDeserializer; +import com.massivecraft.mcore3.lib.gson.JsonElement; +import com.massivecraft.mcore3.lib.gson.JsonObject; +import com.massivecraft.mcore3.lib.gson.JsonParseException; +import com.massivecraft.mcore3.lib.gson.JsonSerializationContext; +import com.massivecraft.mcore3.lib.gson.JsonSerializer; + +public class ItemStackAdapter implements JsonDeserializer, JsonSerializer +{ + // -------------------------------------------- // + // FIELD NAME CONSTANTS + // -------------------------------------------- // + + public static final String TYPE = "type"; + public static final String AMOUNT = "amount"; + public static final String DAMAGE = "damage"; + public static final String ENCHANTMENTS = "enchantments"; + + // -------------------------------------------- // + // IMPLEMENTATION + // -------------------------------------------- // + + @Override + public JsonElement serialize(ItemStack itemStack, Type typeOfSrc, JsonSerializationContext context) + { + return serialize(itemStack); + } + + @Override + public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + return deserialize(json); + } + + // -------------------------------------------- // + // STATIC LOGIC + // -------------------------------------------- // + + public static JsonObject serialize(ItemStack itemStack) + { + if (itemStack == null || itemStack.getTypeId() == 0 || itemStack.getAmount() == 0) + { + return null; + } + + JsonObject jsonItemStack = new JsonObject(); + + jsonItemStack.addProperty(ItemStackAdapter.TYPE, itemStack.getTypeId()); + + if (itemStack.getAmount() != 1) + { + jsonItemStack.addProperty(ItemStackAdapter.AMOUNT, itemStack.getAmount()); + } + if (itemStack.getDurability() != 0) // Durability is a weird name since it is the amount of damage. + { + jsonItemStack.addProperty(ItemStackAdapter.DAMAGE, itemStack.getDurability()); + } + if (itemStack.getEnchantments().size() > 0) + { + JsonObject jsonEnchantments = new JsonObject(); + for (Entry entry : itemStack.getEnchantments().entrySet()) + { + jsonEnchantments.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue()); + } + jsonItemStack.add(ItemStackAdapter.ENCHANTMENTS, jsonEnchantments); + } + return jsonItemStack; + } + + public static ItemStack deserialize(JsonElement json) + { + if (json == null || ! json.isJsonObject()) return null; + + JsonObject jsonItemStack = json.getAsJsonObject(); + + // Populate values + int type = 0; + int amount = 1; + short damage = 0; + + if (jsonItemStack.has(ItemStackAdapter.TYPE)) + { + type = jsonItemStack.get(ItemStackAdapter.TYPE).getAsInt(); + } + + if (jsonItemStack.has(ItemStackAdapter.AMOUNT)) + { + amount = jsonItemStack.get(ItemStackAdapter.AMOUNT).getAsInt(); + } + + if (jsonItemStack.has(ItemStackAdapter.DAMAGE)) + { + damage = jsonItemStack.get(ItemStackAdapter.DAMAGE).getAsShort(); + } + + // Create Non enchanted stack + ItemStack stack = new ItemStack(type, amount, damage); + + // Add enchantments if there are any + if (jsonItemStack.has(ItemStackAdapter.ENCHANTMENTS)) + { + JsonObject jsonEnchantments = jsonItemStack.get(ItemStackAdapter.ENCHANTMENTS).getAsJsonObject(); + for (Entry enchantmentEntry: jsonEnchantments.entrySet()) + { + int enchantmentId = Integer.valueOf(enchantmentEntry.getKey()); + Integer enchantmentLevel = Integer.valueOf(enchantmentEntry.getValue().getAsString()); + stack.addUnsafeEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel); + } + } + + return stack; + } +} diff --git a/src/com/massivecraft/mcore3/util/Txt.java b/src/com/massivecraft/mcore3/util/Txt.java index 359d949d..62e81bf2 100644 --- a/src/com/massivecraft/mcore3/util/Txt.java +++ b/src/com/massivecraft/mcore3/util/Txt.java @@ -331,7 +331,12 @@ public class Txt public static String getMaterialName(Material material) { - return material.toString().replace('_', ' ').toLowerCase(); + List parts = new ArrayList(); + for (String part : material.toString().toLowerCase().split("_")) + { + parts.add(upperCaseFirst(part)); + } + return implode(parts, " "); } public static String getMaterialName(int materialId)