Item adapters for Gson and minor tweaks.
This commit is contained in:
parent
8b5c8fd354
commit
9f3aaa3a30
@ -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());
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -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<Inventory>, JsonSerializer<Inventory>
|
||||
{
|
||||
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<ItemStack> itemStacks = new ArrayList<ItemStack>();
|
||||
public static final String SIZE = "size";
|
||||
|
||||
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<String, JsonElement> 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;
|
||||
}
|
||||
}
|
||||
// -------------------------------------------- //
|
||||
// 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<Enchantment, Integer> 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)
|
||||
{
|
||||
|
122
src/com/massivecraft/mcore3/gson/ItemStackAdapter.java
Normal file
122
src/com/massivecraft/mcore3/gson/ItemStackAdapter.java
Normal file
@ -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<ItemStack>, JsonSerializer<ItemStack>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// 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<Enchantment, Integer> 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<String, JsonElement> enchantmentEntry: jsonEnchantments.entrySet())
|
||||
{
|
||||
int enchantmentId = Integer.valueOf(enchantmentEntry.getKey());
|
||||
Integer enchantmentLevel = Integer.valueOf(enchantmentEntry.getValue().getAsString());
|
||||
stack.addUnsafeEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
@ -331,7 +331,12 @@ public class Txt
|
||||
|
||||
public static String getMaterialName(Material material)
|
||||
{
|
||||
return material.toString().replace('_', ' ').toLowerCase();
|
||||
List<String> parts = new ArrayList<String>();
|
||||
for (String part : material.toString().toLowerCase().split("_"))
|
||||
{
|
||||
parts.add(upperCaseFirst(part));
|
||||
}
|
||||
return implode(parts, " ");
|
||||
}
|
||||
|
||||
public static String getMaterialName(int materialId)
|
||||
|
Loading…
Reference in New Issue
Block a user