Removed the old version of the item stack adapter. This is good since we loose some CraftBukkit usage.

This commit is contained in:
Olof Larsson 2013-02-28 19:54:41 +01:00
parent 8c11390bf9
commit fc22f60e37
6 changed files with 710 additions and 1417 deletions

View File

@ -1,5 +1,5 @@
name: mcore name: mcore
version: 6.0.1 version: 6.1.0
main: com.massivecraft.mcore.MCore main: com.massivecraft.mcore.MCore
load: startup load: startup
permissions: permissions:

View File

@ -1,61 +1,739 @@
package com.massivecraft.mcore.adapter; package com.massivecraft.mcore.adapter;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import org.bukkit.inventory.ItemStack; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionEffect;
import com.massivecraft.mcore.xlib.gson.JsonArray;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer; import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement; import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonObject; import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.JsonParseException; import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer; import com.massivecraft.mcore.xlib.gson.JsonSerializer;
/**
* This is a GSON serializer/deserializer for the Bukkit ItemStack. Why not use
* the built in Bukkit serializer/deserializer? I would have loved to do that :)
* but sadly that one is YAML centric and cannot be used with json in a good
* way. This serializer requires manual updating to work but produces clean
* json. See the file itemstackformat.txt for more info.
*/
public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack> public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack>
{ {
// -------------------------------------------- // // -------------------------------------------- //
// IMPLEMENTATION // FIELD NAME CONSTANTS
// -------------------------------------------- //
public static final String ID = "id";
public static final String COUNT = "count";
public static final String DAMAGE = "damage";
public static final String NAME = "name";
public static final String LORE = "lore";
public static final String ENCHANTS = "enchants";
public static final String REPAIRCOST = "repaircost";
public static final String BOOK_TITLE = "title";
public static final String BOOK_AUTHOR = "author";
public static final String BOOK_PAGES = "pages";
public static final String LEATHER_ARMOR_COLOR = "color";
public static final String MAP_SCALING = "scaling";
public static final String SKULL_OWNER = "skull";
// We renamed "effects" to "potion-effects".
public static final String POTION_EFFECTS_OLD = "effects";
public static final String POTION_EFFECTS = "potion-effects";
public static final String FIREWORK_EFFECT = "firework-effect";
public static final String FIREWORK_EFFECTS = "firework-effects";
public static final String FIREWORK_FLIGHT = "firework-flight";
public static final String STORED_ENCHANTS = "stored-enchants";
// -------------------------------------------- //
// OTHER CONSTANTS
// -------------------------------------------- //
public static final int DEFAULT_ID;
public static final int DEFAULT_COUNT;
public static final int DEFAULT_DAMAGE;
static
{
ItemStack stack = createItemStack();
DEFAULT_ID = stack.getTypeId();
DEFAULT_COUNT = stack.getAmount();
DEFAULT_DAMAGE = stack.getDurability();
}
// -------------------------------------------- //
// GSON INTERFACE IMPLEMENTATION
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public JsonElement serialize(ItemStack itemStack, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context)
{ {
// We always use the latest version when we serialize. return erialize(src);
return ItemStackAdapterV2.erialize(itemStack);
} }
@Override @Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{ {
// We need to decide what version to use when we deserialize. return erialize(json);
JsonDeserializer<ItemStack> deserializer = pickItemStackDeserializer(json);
return deserializer.deserialize(json, typeOfT, context);
} }
// -------------------------------------------- // // -------------------------------------------- //
// PICK ITEM STACK ADAPTER // WRITE
// -------------------------------------------- // // -------------------------------------------- //
protected static JsonDeserializer<ItemStack> pickItemStackDeserializer(JsonElement jsonElement) public static JsonObject erialize(ItemStack stack)
{ {
JsonDeserializer<ItemStack> ret = ItemStackAdapterV2.get();
// Check for "nothing" // Check for "nothing"
if (jsonElement == null) return ret; if (stack == null) return null;
if (stack.getTypeId() == 0) return null;
if (stack.getAmount() == 0) return null;
// Create a new JsonObject
JsonObject json = new JsonObject();
// Transfer data from stack to json
transferAll(stack, json, true);
return json;
}
public static ItemStack erialize(JsonElement jsonElement)
{
// Check for "nothing"
if (jsonElement == null) return null;
// Must be a JsonObject // Must be a JsonObject
if (jsonElement.isJsonObject() == false) return ret; if (jsonElement.isJsonObject() == false) return null;
JsonObject json = jsonElement.getAsJsonObject(); JsonObject json = jsonElement.getAsJsonObject();
// Is it V1? // Create a new ItemStack
if (json.has(ItemStackAdapterV1.TYPE)) ItemStack stack = createItemStack();
// Transfer data from json to stack
transferAll(stack, json, false);
return stack;
}
// -------------------------------------------- //
// NOARG STACK CONSTRUCTOR
// -------------------------------------------- //
public static ItemStack createItemStack()
{
return new ItemStack(0);
}
// -------------------------------------------- //
// ALL
// -------------------------------------------- //
public static void transferAll(ItemStack stack, JsonObject json, boolean stack2json)
{
transferBasic(stack, json, stack2json);
ItemMeta meta = stack.getItemMeta();
transferMeta(meta, json, stack2json);
if (stack2json == false)
{ {
ret = ItemStackAdapterV1.get(); stack.setItemMeta(meta);
}
}
// -------------------------------------------- //
// BASIC
// -------------------------------------------- //
public static void transferBasic(ItemStack stack, JsonObject json, boolean stack2json)
{
transferId(stack, json, stack2json);
transferCount(stack, json, stack2json);
transferDamage(stack, json, stack2json);
}
// -------------------------------------------- //
// BASIC: ID
// -------------------------------------------- //
public static void transferId(ItemStack stack, JsonObject json,
boolean stack2json)
{
if (stack2json)
{
int id = stack.getTypeId();
if (id == DEFAULT_ID) return;
json.addProperty(ID, id);
}
else
{
JsonElement element = json.get(ID);
if (element == null) return;
stack.setTypeId(element.getAsInt());
}
}
// -------------------------------------------- //
// BASIC: COUNT
// -------------------------------------------- //
public static void transferCount(ItemStack stack, JsonObject json, boolean stack2json)
{
if (stack2json)
{
int count = stack.getAmount();
if (count == DEFAULT_COUNT) return;
json.addProperty(COUNT, count);
}
else
{
JsonElement element = json.get(COUNT);
if (element == null) return;
stack.setAmount(element.getAsInt());
}
}
// -------------------------------------------- //
// BASIC: DAMAGE
// -------------------------------------------- //
public static void transferDamage(ItemStack stack, JsonObject json, boolean stack2json)
{
// Durability is a weird name since it is the amount of damage.
if (stack2json)
{
int damage = stack.getDurability();
if (damage == DEFAULT_DAMAGE) return;
json.addProperty(DAMAGE, damage);
}
else
{
JsonElement element = json.get(DAMAGE);
if (element == null) return;
stack.setDurability(element.getAsShort());
}
}
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static void transferMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
transferUnspecificMeta(meta, json, meta2json);
transferSpecificMeta(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META
// -------------------------------------------- //
public static void transferUnspecificMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
transferName(meta, json, meta2json);
transferLore(meta, json, meta2json);
transferEnchants(meta, json, meta2json);
transferRepaircost(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META: NAME
// -------------------------------------------- //
public static void transferName(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasDisplayName()) return;
json.addProperty(NAME, meta.getDisplayName());
}
else
{
JsonElement element = json.get(NAME);
if (element == null) return;
meta.setDisplayName(element.getAsString());
}
}
// -------------------------------------------- //
// UNSPECIFIC META: LORE
// -------------------------------------------- //
public static void transferLore(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasLore()) return;
json.add(LORE, convertStringList(meta.getLore()));
}
else
{
JsonElement element = json.get(LORE);
if (element == null) return;
meta.setLore(convertStringList(element));
}
}
// -------------------------------------------- //
// UNSPECIFIC META: ENCHANTS
// -------------------------------------------- //
public static void transferEnchants(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEnchants()) return;
json.add(ENCHANTS, convertEnchantLevelMap(meta.getEnchants()));
}
else
{
JsonElement element = json.get(ENCHANTS);
if (element == null) return;
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
{
meta.addEnchant(entry.getKey(), entry.getValue(), true);
}
}
}
// -------------------------------------------- //
// UNSPECIFIC META: REPAIRCOST
// -------------------------------------------- //
public static void transferRepaircost(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (!(meta instanceof Repairable)) return;
Repairable repairable = (Repairable) meta;
if (meta2json)
{
if (!repairable.hasRepairCost()) return;
json.addProperty(REPAIRCOST, repairable.getRepairCost());
}
else
{
JsonElement element = json.get(REPAIRCOST);
if (element == null) return;
repairable.setRepairCost(element.getAsInt());
}
}
// -------------------------------------------- //
// SPECIFIC META
// -------------------------------------------- //
public static void transferSpecificMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta instanceof BookMeta)
{
transferBookMeta((BookMeta) meta, json, meta2json);
}
else if (meta instanceof LeatherArmorMeta)
{
transferLeatherArmorMeta((LeatherArmorMeta) meta, json, meta2json);
}
else if (meta instanceof MapMeta)
{
transferMapMeta((MapMeta) meta, json, meta2json);
}
else if (meta instanceof PotionMeta)
{
transferPotionMeta((PotionMeta) meta, json, meta2json);
}
else if (meta instanceof SkullMeta)
{
transferSkullMeta((SkullMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkEffectMeta)
{
transferFireworkEffectMeta((FireworkEffectMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkMeta)
{
transferFireworkMeta((FireworkMeta) meta, json, meta2json);
}
else if (meta instanceof EnchantmentStorageMeta)
{
transferEnchantmentStorageMeta((EnchantmentStorageMeta) meta, json, meta2json);
}
}
// -------------------------------------------- //
// SPECIFIC META: BOOK
// -------------------------------------------- //
public static void transferBookMeta(BookMeta meta, JsonObject json, boolean meta2json)
{
transferTitle(meta, json, meta2json);
transferAuthor(meta, json, meta2json);
transferPages(meta, json, meta2json);
}
public static void transferTitle(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.addProperty(BOOK_TITLE, meta.getTitle());
}
else
{
JsonElement element = json.get(BOOK_TITLE);
if (element == null) return;
meta.setTitle(element.getAsString());
}
}
public static void transferAuthor(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.addProperty(BOOK_AUTHOR, meta.getAuthor());
}
else
{
JsonElement element = json.get(BOOK_AUTHOR);
if (element == null) return;
meta.setAuthor(element.getAsString());
}
}
public static void transferPages(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.add(BOOK_PAGES, convertStringList(meta.getPages()));
}
else
{
JsonElement element = json.get(BOOK_PAGES);
if (element == null) return;
meta.setPages(convertStringList(element));
}
}
// -------------------------------------------- //
// SPECIFIC META: LEATHER ARMOR
// -------------------------------------------- //
public static void transferLeatherArmorMeta(LeatherArmorMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
Color color = meta.getColor();
if (Bukkit.getItemFactory().getDefaultLeatherColor().equals(color)) return;
json.addProperty(LEATHER_ARMOR_COLOR, color.asRGB());
}
else
{
JsonElement element = json.get(LEATHER_ARMOR_COLOR);
if (element == null) return;
meta.setColor(Color.fromRGB(element.getAsInt()));
}
}
// -------------------------------------------- //
// SPECIFIC META: MAP
// -------------------------------------------- //
public static void transferMapMeta(MapMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.isScaling()) return;
json.addProperty(MAP_SCALING, true);
}
else
{
JsonElement element = json.get(MAP_SCALING);
if (element == null) return;
meta.setScaling(element.getAsBoolean());
}
}
// -------------------------------------------- //
// SPECIFIC META: POTION
// -------------------------------------------- //
public static void transferPotionMeta(PotionMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasCustomEffects()) return;
json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects()));
}
else
{
JsonElement element = json.get(POTION_EFFECTS);
if (element == null) element = json.get(POTION_EFFECTS_OLD);
if (element == null) return;
meta.clearCustomEffects();
for (PotionEffect pe : convertPotionEffectList(element))
{
meta.addCustomEffect(pe, false);
}
}
}
// -------------------------------------------- //
// SPECIFIC META: SKULL
// -------------------------------------------- //
public static void transferSkullMeta(SkullMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasOwner()) return;
json.addProperty(SKULL_OWNER, meta.getOwner());
}
else
{
JsonElement element = json.get(SKULL_OWNER);
if (element == null) return;
meta.setOwner(element.getAsString());
}
}
// -------------------------------------------- //
// SPECIFIC META: FIREWORK EFFECT
// -------------------------------------------- //
public static void transferFireworkEffectMeta(FireworkEffectMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEffect()) return;
json.add(FIREWORK_EFFECT, FireworkEffectAdapter.toJson(meta.getEffect()));
}
else
{
JsonElement element = json.get(FIREWORK_EFFECT);
if (element == null) return;
meta.setEffect(FireworkEffectAdapter.fromJson(element));
}
}
// -------------------------------------------- //
// SPECIFIC META: FIREWORK
// -------------------------------------------- //
public static void transferFireworkMeta(FireworkMeta meta, JsonObject json, boolean meta2json)
{
transferFireworkMetaEffects(meta, json, meta2json);
transferFireworkMetaPower(meta, json, meta2json);
}
public static void transferFireworkMetaEffects(FireworkMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEffects()) return;
json.add(FIREWORK_EFFECTS, convertFireworkEffectList(meta.getEffects()));
}
else
{
JsonElement element = json.get(FIREWORK_EFFECTS);
if (element == null) return;
meta.clearEffects();
meta.addEffects(convertFireworkEffectList(element));
}
}
public static void transferFireworkMetaPower(FireworkMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
json.addProperty(FIREWORK_FLIGHT, meta.getPower());
}
else
{
JsonElement element = json.get(FIREWORK_FLIGHT);
if (element == null) return;
meta.setPower(element.getAsInt());
}
}
// -------------------------------------------- //
// SPECIFIC META: ENCHANTMENT STORAGE
// -------------------------------------------- //
public static void transferEnchantmentStorageMeta(EnchantmentStorageMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasStoredEnchants()) return;
json.add(STORED_ENCHANTS, convertEnchantLevelMap(meta.getStoredEnchants()));
}
else
{
JsonElement element = json.get(STORED_ENCHANTS);
if (element == null) return;
// TODO: Add a pull request to get rid of this entry set loop!
// TODO: A set, clear, remove all system is missing
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
{
meta.addStoredEnchant(entry.getKey(), entry.getValue(), true);
}
}
}
// -------------------------------------------- //
// MINI UTILS
// -------------------------------------------- //
// String List
public static JsonArray convertStringList(Collection<String> strings)
{
JsonArray ret = new JsonArray();
for (String string : strings)
{
ret.add(new JsonPrimitive(string));
}
return ret;
}
public static List<String> convertStringList(JsonElement jsonElement)
{
JsonArray array = jsonElement.getAsJsonArray();
List<String> ret = new ArrayList<String>();
Iterator<JsonElement> iter = array.iterator();
while (iter.hasNext())
{
JsonElement element = iter.next();
ret.add(element.getAsString());
} }
return ret; return ret;
} }
// PotionEffect List
public static JsonArray convertPotionEffectList(Collection<PotionEffect> potionEffects)
{
JsonArray ret = new JsonArray();
for (PotionEffect e : potionEffects)
{
ret.add(PotionEffectAdapter.toJson(e));
}
return ret;
}
public static List<PotionEffect> convertPotionEffectList(JsonElement jsonElement)
{
if (jsonElement == null) return null;
if ( ! jsonElement.isJsonArray()) return null;
JsonArray array = jsonElement.getAsJsonArray();
List<PotionEffect> ret = new ArrayList<PotionEffect>();
Iterator<JsonElement> iter = array.iterator();
while(iter.hasNext())
{
PotionEffect e = PotionEffectAdapter.fromJson(iter.next());
if (e == null) continue;
ret.add(e);
}
return ret;
}
// FireworkEffect List
public static JsonArray convertFireworkEffectList(Collection<FireworkEffect> fireworkEffects)
{
JsonArray ret = new JsonArray();
for (FireworkEffect fe : fireworkEffects)
{
ret.add(FireworkEffectAdapter.toJson(fe));
}
return ret;
}
public static List<FireworkEffect> convertFireworkEffectList(JsonElement jsonElement)
{
if (jsonElement == null) return null;
if ( ! jsonElement.isJsonArray()) return null;
JsonArray array = jsonElement.getAsJsonArray();
List<FireworkEffect> ret = new ArrayList<FireworkEffect>();
Iterator<JsonElement> iter = array.iterator();
while(iter.hasNext())
{
FireworkEffect fe = FireworkEffectAdapter.fromJson(iter.next());
if (fe == null) continue;
ret.add(fe);
}
return ret;
}
// EnchantLevelMap
public static JsonObject convertEnchantLevelMap(Map<Enchantment, Integer> enchantLevelMap)
{
JsonObject ret = new JsonObject();
for (Entry<Enchantment, Integer> entry : enchantLevelMap.entrySet())
{
ret.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue());
}
return ret;
}
public static Map<Enchantment, Integer> convertEnchantLevelMap(JsonElement jsonElement)
{
JsonObject json = jsonElement.getAsJsonObject();
Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>();
for (Entry<String, JsonElement> entry : json.entrySet())
{
int id = Integer.valueOf(entry.getKey());
Enchantment ench = Enchantment.getById(id);
int lvl = entry.getValue().getAsInt();
ret.put(ench, lvl);
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE // INSTANCE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,214 +0,0 @@
package com.massivecraft.mcore.adapter;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Map.Entry;
import net.minecraft.server.v1_4_R1.NBTBase;
import net.minecraft.server.v1_4_R1.NBTTagCompound;
import org.bukkit.craftbukkit.v1_4_R1.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer;
// TODO: This adapter is deprecated as of 2012-12-20. It should be removed in some time.
public class ItemStackAdapterV1 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";
public static final String TAG = "tag";
// -------------------------------------------- //
// IMPLEMENTATION
// -------------------------------------------- //
@Override
public JsonElement serialize(ItemStack itemStack, Type typeOfSrc, JsonSerializationContext context)
{
return toJson(itemStack);
}
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return fromJson(json);
}
// -------------------------------------------- //
// JSON
// -------------------------------------------- //
public static JsonObject toJson(ItemStack stack)
{
// Check for "nothing"
if (stack == null || stack.getTypeId() == 0 || stack.getAmount() == 0)
{
return null;
}
JsonObject jsonItemStack = new JsonObject();
// Add type id
jsonItemStack.addProperty(TYPE, stack.getTypeId());
// Add amount
if (stack.getAmount() != 1)
{
jsonItemStack.addProperty(AMOUNT, stack.getAmount());
}
// Add damage
if (stack.getDurability() != 0) // Durability is a weird name since it is the amount of damage.
{
jsonItemStack.addProperty(DAMAGE, stack.getDurability());
}
// Add enchantments
if (stack.getEnchantments().size() > 0)
{
JsonObject jsonEnchantments = new JsonObject();
for (Entry<Enchantment, Integer> entry : stack.getEnchantments().entrySet())
{
jsonEnchantments.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue());
}
jsonItemStack.add(ItemStackAdapterV1.ENCHANTMENTS, jsonEnchantments);
}
// Add the tag if there is one
JsonObject tag = getEnchFreeGsonTagFromItemStack(stack);
if (tag != null)
{
jsonItemStack.add(TAG, tag);
}
return jsonItemStack;
}
// Used by method toJson
public static JsonObject getEnchFreeGsonTagFromItemStack(ItemStack stack)
{
if (!(stack instanceof CraftItemStack)) return null;
CraftItemStack craftItemStack = (CraftItemStack)stack;
NBTTagCompound nbt = getHandle(craftItemStack).tag;
if (nbt == null) return null;
JsonObject gsonbt = (JsonObject) NbtGsonConverter.nbtToGsonVal(nbt);
gsonbt.remove("ench");
if (gsonbt.entrySet().size() == 0) return null;
return gsonbt;
}
public static ItemStack fromJson(JsonElement json)
{
// Check for "nothing"
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(TYPE))
{
type = jsonItemStack.get(TYPE).getAsInt();
}
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 tag
if (jsonItemStack.has(TAG))
{
JsonObject jsonbt = jsonItemStack.get(TAG).getAsJsonObject();
CraftItemStack craftItemStack = CraftItemStack.asCraftCopy(stack);
stack = craftItemStack;
NBTBase nbt = NbtGsonConverter.gsonValToNbt(jsonbt, null, NBType.COMPOUND, NBType.UNKNOWN);
getHandle(craftItemStack).tag = (NBTTagCompound) nbt;
}
// 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.addUnsafeEnchantment(Enchantment.getById(enchantmentId), enchantmentLevel);
}
}
return stack;
}
// -------------------------------------------- //
// GET HANDLE
// -------------------------------------------- //
public static Field fieldCraftItemStackDotHandle = null;
static
{
try
{
fieldCraftItemStackDotHandle = CraftItemStack.class.getDeclaredField("handle");
fieldCraftItemStackDotHandle.setAccessible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static net.minecraft.server.v1_4_R1.ItemStack getHandle(CraftItemStack craftItemStack)
{
try
{
return (net.minecraft.server.v1_4_R1.ItemStack) fieldCraftItemStackDotHandle.get(craftItemStack);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
public static ItemStackAdapterV1 i = new ItemStackAdapterV1();
public static ItemStackAdapterV1 get() { return i; }
}

View File

@ -1,744 +0,0 @@
package com.massivecraft.mcore.adapter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionEffect;
import com.massivecraft.mcore.xlib.gson.JsonArray;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer;
/**
* This is a GSON serializer/deserializer for the Bukkit ItemStack. Why not use
* the built in Bukkit serializer/deserializer? I would have loved to do that :)
* but sadly that one is YAML centric and cannot be used with json in a good
* way. This serializer requires manual updating to work but produces clean
* json. See the file itemstackformat.txt for more info.
*/
public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack>
{
// -------------------------------------------- //
// FIELD NAME CONSTANTS
// -------------------------------------------- //
public static final String ID = "id";
public static final String COUNT = "count";
public static final String DAMAGE = "damage";
public static final String NAME = "name";
public static final String LORE = "lore";
public static final String ENCHANTS = "enchants";
public static final String REPAIRCOST = "repaircost";
public static final String BOOK_TITLE = "title";
public static final String BOOK_AUTHOR = "author";
public static final String BOOK_PAGES = "pages";
public static final String LEATHER_ARMOR_COLOR = "color";
public static final String MAP_SCALING = "scaling";
public static final String SKULL_OWNER = "skull";
// We renamed "effects" to "potion-effects".
public static final String POTION_EFFECTS_OLD = "effects";
public static final String POTION_EFFECTS = "potion-effects";
public static final String FIREWORK_EFFECT = "firework-effect";
public static final String FIREWORK_EFFECTS = "firework-effects";
public static final String FIREWORK_FLIGHT = "firework-flight";
public static final String STORED_ENCHANTS = "stored-enchants";
// -------------------------------------------- //
// OTHER CONSTANTS
// -------------------------------------------- //
public static final int DEFAULT_ID;
public static final int DEFAULT_COUNT;
public static final int DEFAULT_DAMAGE;
static
{
ItemStack stack = createItemStack();
DEFAULT_ID = stack.getTypeId();
DEFAULT_COUNT = stack.getAmount();
DEFAULT_DAMAGE = stack.getDurability();
}
// -------------------------------------------- //
// GSON INTERFACE IMPLEMENTATION
// -------------------------------------------- //
@Override
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context)
{
return erialize(src);
}
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return erialize(json);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
public static JsonObject erialize(ItemStack stack)
{
// Check for "nothing"
if (stack == null) return null;
if (stack.getTypeId() == 0) return null;
if (stack.getAmount() == 0) return null;
// Create a new JsonObject
JsonObject json = new JsonObject();
// Transfer data from stack to json
transferAll(stack, json, true);
return json;
}
public static ItemStack erialize(JsonElement jsonElement)
{
// Check for "nothing"
if (jsonElement == null) return null;
// Must be a JsonObject
if (jsonElement.isJsonObject() == false) return null;
JsonObject json = jsonElement.getAsJsonObject();
// Create a new ItemStack
ItemStack stack = createItemStack();
// Transfer data from json to stack
transferAll(stack, json, false);
return stack;
}
// -------------------------------------------- //
// NOARG STACK CONSTRUCTOR
// -------------------------------------------- //
public static ItemStack createItemStack()
{
return new ItemStack(0);
}
// -------------------------------------------- //
// ALL
// -------------------------------------------- //
public static void transferAll(ItemStack stack, JsonObject json, boolean stack2json)
{
transferBasic(stack, json, stack2json);
ItemMeta meta = stack.getItemMeta();
transferMeta(meta, json, stack2json);
if (stack2json == false)
{
stack.setItemMeta(meta);
}
}
// -------------------------------------------- //
// BASIC
// -------------------------------------------- //
public static void transferBasic(ItemStack stack, JsonObject json, boolean stack2json)
{
transferId(stack, json, stack2json);
transferCount(stack, json, stack2json);
transferDamage(stack, json, stack2json);
}
// -------------------------------------------- //
// BASIC: ID
// -------------------------------------------- //
public static void transferId(ItemStack stack, JsonObject json,
boolean stack2json)
{
if (stack2json)
{
int id = stack.getTypeId();
if (id == DEFAULT_ID) return;
json.addProperty(ID, id);
}
else
{
JsonElement element = json.get(ID);
if (element == null) return;
stack.setTypeId(element.getAsInt());
}
}
// -------------------------------------------- //
// BASIC: COUNT
// -------------------------------------------- //
public static void transferCount(ItemStack stack, JsonObject json, boolean stack2json)
{
if (stack2json)
{
int count = stack.getAmount();
if (count == DEFAULT_COUNT) return;
json.addProperty(COUNT, count);
}
else
{
JsonElement element = json.get(COUNT);
if (element == null) return;
stack.setAmount(element.getAsInt());
}
}
// -------------------------------------------- //
// BASIC: DAMAGE
// -------------------------------------------- //
public static void transferDamage(ItemStack stack, JsonObject json, boolean stack2json)
{
// Durability is a weird name since it is the amount of damage.
if (stack2json)
{
int damage = stack.getDurability();
if (damage == DEFAULT_DAMAGE) return;
json.addProperty(DAMAGE, damage);
}
else
{
JsonElement element = json.get(DAMAGE);
if (element == null) return;
stack.setDurability(element.getAsShort());
}
}
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static void transferMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
transferUnspecificMeta(meta, json, meta2json);
transferSpecificMeta(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META
// -------------------------------------------- //
public static void transferUnspecificMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
transferName(meta, json, meta2json);
transferLore(meta, json, meta2json);
transferEnchants(meta, json, meta2json);
transferRepaircost(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META: NAME
// -------------------------------------------- //
public static void transferName(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasDisplayName()) return;
json.addProperty(NAME, meta.getDisplayName());
}
else
{
JsonElement element = json.get(NAME);
if (element == null) return;
meta.setDisplayName(element.getAsString());
}
}
// -------------------------------------------- //
// UNSPECIFIC META: LORE
// -------------------------------------------- //
public static void transferLore(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasLore()) return;
json.add(LORE, convertStringList(meta.getLore()));
}
else
{
JsonElement element = json.get(LORE);
if (element == null) return;
meta.setLore(convertStringList(element));
}
}
// -------------------------------------------- //
// UNSPECIFIC META: ENCHANTS
// -------------------------------------------- //
public static void transferEnchants(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEnchants()) return;
json.add(ENCHANTS, convertEnchantLevelMap(meta.getEnchants()));
}
else
{
JsonElement element = json.get(ENCHANTS);
if (element == null) return;
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
{
meta.addEnchant(entry.getKey(), entry.getValue(), true);
}
}
}
// -------------------------------------------- //
// UNSPECIFIC META: REPAIRCOST
// -------------------------------------------- //
public static void transferRepaircost(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (!(meta instanceof Repairable)) return;
Repairable repairable = (Repairable) meta;
if (meta2json)
{
if (!repairable.hasRepairCost()) return;
json.addProperty(REPAIRCOST, repairable.getRepairCost());
}
else
{
JsonElement element = json.get(REPAIRCOST);
if (element == null) return;
repairable.setRepairCost(element.getAsInt());
}
}
// -------------------------------------------- //
// SPECIFIC META
// -------------------------------------------- //
public static void transferSpecificMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta instanceof BookMeta)
{
transferBookMeta((BookMeta) meta, json, meta2json);
}
else if (meta instanceof LeatherArmorMeta)
{
transferLeatherArmorMeta((LeatherArmorMeta) meta, json, meta2json);
}
else if (meta instanceof MapMeta)
{
transferMapMeta((MapMeta) meta, json, meta2json);
}
else if (meta instanceof PotionMeta)
{
transferPotionMeta((PotionMeta) meta, json, meta2json);
}
else if (meta instanceof SkullMeta)
{
transferSkullMeta((SkullMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkEffectMeta)
{
transferFireworkEffectMeta((FireworkEffectMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkMeta)
{
transferFireworkMeta((FireworkMeta) meta, json, meta2json);
}
else if (meta instanceof EnchantmentStorageMeta)
{
transferEnchantmentStorageMeta((EnchantmentStorageMeta) meta, json, meta2json);
}
}
// -------------------------------------------- //
// SPECIFIC META: BOOK
// -------------------------------------------- //
public static void transferBookMeta(BookMeta meta, JsonObject json, boolean meta2json)
{
transferTitle(meta, json, meta2json);
transferAuthor(meta, json, meta2json);
transferPages(meta, json, meta2json);
}
public static void transferTitle(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.addProperty(BOOK_TITLE, meta.getTitle());
}
else
{
JsonElement element = json.get(BOOK_TITLE);
if (element == null) return;
meta.setTitle(element.getAsString());
}
}
public static void transferAuthor(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.addProperty(BOOK_AUTHOR, meta.getAuthor());
}
else
{
JsonElement element = json.get(BOOK_AUTHOR);
if (element == null) return;
meta.setAuthor(element.getAsString());
}
}
public static void transferPages(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
json.add(BOOK_PAGES, convertStringList(meta.getPages()));
}
else
{
JsonElement element = json.get(BOOK_PAGES);
if (element == null) return;
meta.setPages(convertStringList(element));
}
}
// -------------------------------------------- //
// SPECIFIC META: LEATHER ARMOR
// -------------------------------------------- //
public static void transferLeatherArmorMeta(LeatherArmorMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
Color color = meta.getColor();
if (Bukkit.getItemFactory().getDefaultLeatherColor().equals(color)) return;
json.addProperty(LEATHER_ARMOR_COLOR, color.asRGB());
}
else
{
JsonElement element = json.get(LEATHER_ARMOR_COLOR);
if (element == null) return;
meta.setColor(Color.fromRGB(element.getAsInt()));
}
}
// -------------------------------------------- //
// SPECIFIC META: MAP
// -------------------------------------------- //
public static void transferMapMeta(MapMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.isScaling()) return;
json.addProperty(MAP_SCALING, true);
}
else
{
JsonElement element = json.get(MAP_SCALING);
if (element == null) return;
meta.setScaling(element.getAsBoolean());
}
}
// -------------------------------------------- //
// SPECIFIC META: POTION
// -------------------------------------------- //
public static void transferPotionMeta(PotionMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasCustomEffects()) return;
json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects()));
}
else
{
JsonElement element = json.get(POTION_EFFECTS);
if (element == null) element = json.get(POTION_EFFECTS_OLD);
if (element == null) return;
meta.clearCustomEffects();
for (PotionEffect pe : convertPotionEffectList(element))
{
meta.addCustomEffect(pe, false);
}
}
}
// -------------------------------------------- //
// SPECIFIC META: SKULL
// -------------------------------------------- //
public static void transferSkullMeta(SkullMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasOwner()) return;
json.addProperty(SKULL_OWNER, meta.getOwner());
}
else
{
JsonElement element = json.get(SKULL_OWNER);
if (element == null) return;
meta.setOwner(element.getAsString());
}
}
// -------------------------------------------- //
// SPECIFIC META: FIREWORK EFFECT
// -------------------------------------------- //
public static void transferFireworkEffectMeta(FireworkEffectMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEffect()) return;
json.add(FIREWORK_EFFECT, FireworkEffectAdapter.toJson(meta.getEffect()));
}
else
{
JsonElement element = json.get(FIREWORK_EFFECT);
if (element == null) return;
meta.setEffect(FireworkEffectAdapter.fromJson(element));
}
}
// -------------------------------------------- //
// SPECIFIC META: FIREWORK
// -------------------------------------------- //
public static void transferFireworkMeta(FireworkMeta meta, JsonObject json, boolean meta2json)
{
transferFireworkMetaEffects(meta, json, meta2json);
transferFireworkMetaPower(meta, json, meta2json);
}
public static void transferFireworkMetaEffects(FireworkMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasEffects()) return;
json.add(FIREWORK_EFFECTS, convertFireworkEffectList(meta.getEffects()));
}
else
{
JsonElement element = json.get(FIREWORK_EFFECTS);
if (element == null) return;
meta.clearEffects();
meta.addEffects(convertFireworkEffectList(element));
}
}
public static void transferFireworkMetaPower(FireworkMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
json.addProperty(FIREWORK_FLIGHT, meta.getPower());
}
else
{
JsonElement element = json.get(FIREWORK_FLIGHT);
if (element == null) return;
meta.setPower(element.getAsInt());
}
}
// -------------------------------------------- //
// SPECIFIC META: ENCHANTMENT STORAGE
// -------------------------------------------- //
public static void transferEnchantmentStorageMeta(EnchantmentStorageMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasStoredEnchants()) return;
json.add(STORED_ENCHANTS, convertEnchantLevelMap(meta.getStoredEnchants()));
}
else
{
JsonElement element = json.get(STORED_ENCHANTS);
if (element == null) return;
// TODO: Add a pull request to get rid of this entry set loop!
// TODO: A set, clear, remove all system is missing
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
{
meta.addStoredEnchant(entry.getKey(), entry.getValue(), true);
}
}
}
// -------------------------------------------- //
// MINI UTILS
// -------------------------------------------- //
// String List
public static JsonArray convertStringList(Collection<String> strings)
{
JsonArray ret = new JsonArray();
for (String string : strings)
{
ret.add(new JsonPrimitive(string));
}
return ret;
}
public static List<String> convertStringList(JsonElement jsonElement)
{
JsonArray array = jsonElement.getAsJsonArray();
List<String> ret = new ArrayList<String>();
Iterator<JsonElement> iter = array.iterator();
while (iter.hasNext())
{
JsonElement element = iter.next();
ret.add(element.getAsString());
}
return ret;
}
// PotionEffect List
public static JsonArray convertPotionEffectList(Collection<PotionEffect> potionEffects)
{
JsonArray ret = new JsonArray();
for (PotionEffect e : potionEffects)
{
ret.add(PotionEffectAdapter.toJson(e));
}
return ret;
}
public static List<PotionEffect> convertPotionEffectList(JsonElement jsonElement)
{
if (jsonElement == null) return null;
if ( ! jsonElement.isJsonArray()) return null;
JsonArray array = jsonElement.getAsJsonArray();
List<PotionEffect> ret = new ArrayList<PotionEffect>();
Iterator<JsonElement> iter = array.iterator();
while(iter.hasNext())
{
PotionEffect e = PotionEffectAdapter.fromJson(iter.next());
if (e == null) continue;
ret.add(e);
}
return ret;
}
// FireworkEffect List
public static JsonArray convertFireworkEffectList(Collection<FireworkEffect> fireworkEffects)
{
JsonArray ret = new JsonArray();
for (FireworkEffect fe : fireworkEffects)
{
ret.add(FireworkEffectAdapter.toJson(fe));
}
return ret;
}
public static List<FireworkEffect> convertFireworkEffectList(JsonElement jsonElement)
{
if (jsonElement == null) return null;
if ( ! jsonElement.isJsonArray()) return null;
JsonArray array = jsonElement.getAsJsonArray();
List<FireworkEffect> ret = new ArrayList<FireworkEffect>();
Iterator<JsonElement> iter = array.iterator();
while(iter.hasNext())
{
FireworkEffect fe = FireworkEffectAdapter.fromJson(iter.next());
if (fe == null) continue;
ret.add(fe);
}
return ret;
}
// EnchantLevelMap
public static JsonObject convertEnchantLevelMap(Map<Enchantment, Integer> enchantLevelMap)
{
JsonObject ret = new JsonObject();
for (Entry<Enchantment, Integer> entry : enchantLevelMap.entrySet())
{
ret.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue());
}
return ret;
}
public static Map<Enchantment, Integer> convertEnchantLevelMap(JsonElement jsonElement)
{
JsonObject json = jsonElement.getAsJsonObject();
Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>();
for (Entry<String, JsonElement> entry : json.entrySet())
{
int id = Integer.valueOf(entry.getKey());
Enchantment ench = Enchantment.getById(id);
int lvl = entry.getValue().getAsInt();
ret.put(ench, lvl);
}
return ret;
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
public static ItemStackAdapterV2 i = new ItemStackAdapterV2();
public static ItemStackAdapterV2 get() { return i; }
}

View File

@ -1,94 +0,0 @@
package com.massivecraft.mcore.adapter;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.server.v1_4_R1.NBTBase;
public enum NBType
{
// -------------------------------------------- //
// VALUES
// -------------------------------------------- //
END(0, "end"),
BYTE(1, "byte"),
SHORT(2, "short"),
INT(3, "int"),
LONG(4, "long"),
FLOAT(5, "float"),
DOUBLE(6, "double"),
BYTEARRAY(7, "bytearray"),
STRING(8, "string"),
LIST(9, "list"),
COMPOUND(10, "compound"),
INTARRAY(11, "intarray"),
UNKNOWN(-1, "unknown"),
;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
final byte id;
public byte getId() { return this.id; }
final String name;
public String getName() { return this.name; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private NBType(int id, String name)
{
this.id = (byte)id;
this.name = name;
}
// -------------------------------------------- //
// STATIC UTILS
// -------------------------------------------- //
protected final static transient Map<String, NBType> tagnameToEnum = new HashMap<String, NBType>();
protected final static transient Map<Byte, NBType> byteToEnum = new HashMap<Byte, NBType>();
static
{
for (NBType value : values())
{
tagnameToEnum.put(value.getName(), value);
byteToEnum.put(value.getId(), value);
}
}
public static NBType getByName(String name)
{
NBType ret = tagnameToEnum.get(name);
if (ret == null)
{
ret = UNKNOWN;
}
return ret;
}
public static NBType getById(byte id)
{
NBType ret = byteToEnum.get(id);
if (ret == null)
{
ret = UNKNOWN;
}
return ret;
}
public static NBType getByTag(NBTBase tag)
{
NBType ret = byteToEnum.get(tag.getTypeId());
if (ret == null)
{
ret = UNKNOWN;
}
return ret;
}
}

View File

@ -1,333 +0,0 @@
package com.massivecraft.mcore.adapter;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import net.minecraft.server.v1_4_R1.NBTBase;
import net.minecraft.server.v1_4_R1.NBTTagByte;
import net.minecraft.server.v1_4_R1.NBTTagByteArray;
import net.minecraft.server.v1_4_R1.NBTTagCompound;
import net.minecraft.server.v1_4_R1.NBTTagDouble;
import net.minecraft.server.v1_4_R1.NBTTagEnd;
import net.minecraft.server.v1_4_R1.NBTTagFloat;
import net.minecraft.server.v1_4_R1.NBTTagInt;
import net.minecraft.server.v1_4_R1.NBTTagIntArray;
import net.minecraft.server.v1_4_R1.NBTTagList;
import net.minecraft.server.v1_4_R1.NBTTagLong;
import net.minecraft.server.v1_4_R1.NBTTagShort;
import net.minecraft.server.v1_4_R1.NBTTagString;
import com.massivecraft.mcore.xlib.gson.JsonArray;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
//TODO: This converter is deprecated as of 2012-12-20. It should be removed in some time.
public class NbtGsonConverter
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public final static String TYPE = "type";
public final static String ELEMTYPE = "elemtype";
public final static String VAL = "val";
public final static String NAME = "name";
// -------------------------------------------- //
// GSON TO NBT
// -------------------------------------------- //
public static NBTBase gsonToNbt(JsonElement jsonElement)
{
return gsonToNbt(jsonElement, null);
}
public static NBTBase gsonToNbt(JsonElement jsonElement, String name)
{
// Verify and cast the jsonElement into a jsonObject.
// We could have used jsonObject as parameter type but this method signature is more flexible.
if (!jsonElement.isJsonObject())
{
// must be a json object
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
// Use the internal name if there is one
JsonElement nameElement = jsonObject.get(NAME);
if (nameElement != null)
{
name = nameElement.getAsString();
}
// Fetch the type-info
JsonElement typeElement = jsonObject.get(TYPE);
if (typeElement == null)
{
// must have a type
return null;
}
NBType type = NBType.getByName(typeElement.getAsString());
// Fetch the elemtype-info (used by NBTTagList only)
NBType elemtype = NBType.UNKNOWN;
if (type == NBType.LIST)
{
JsonElement elemtypeElement = jsonObject.get(ELEMTYPE);
if (elemtypeElement != null)
{
elemtype = NBType.getByName(elemtypeElement.getAsString());
}
}
// Fetch the value field
JsonElement val = jsonObject.get(VAL);
// Convert the value based on the info we gathered
return gsonValToNbt(val, name, type, elemtype);
}
public static NBTBase gsonValToNbt(JsonElement val, String name, NBType type, NBType elemtype)
{
NBTBase ret = null;
switch (type)
{
case END:
ret = new NBTTagEnd();
break;
case BYTE:
ret = new NBTTagByte(name, val.getAsByte());
break;
case SHORT:
ret = new NBTTagShort(name, val.getAsShort());
break;
case INT:
ret = new NBTTagInt(name, val.getAsInt());
break;
case LONG:
ret = new NBTTagLong(name, val.getAsLong());
break;
case FLOAT:
ret = new NBTTagFloat(name, val.getAsFloat());
break;
case DOUBLE:
ret = new NBTTagDouble(name, val.getAsDouble());
break;
case BYTEARRAY:
JsonArray jsonBytes = val.getAsJsonArray();
int jsonBytesSize = jsonBytes.size();
byte[] byteArray = new byte[jsonBytesSize];
for (int index = 0 ; index < jsonBytesSize ; index++)
{
byte b = jsonBytes.get(index).getAsByte();
byteArray[index] = b;
}
ret = new NBTTagByteArray(name, byteArray);
break;
case INTARRAY:
JsonArray jsonInts = val.getAsJsonArray();
int jsonIntsSize = jsonInts.size();
int[] intArray = new int[jsonIntsSize];
for (int index = 0 ; index < jsonIntsSize ; index++)
{
int i = jsonInts.get(index).getAsInt();
intArray[index] = i;
}
ret = new NBTTagIntArray(name, intArray);
break;
case STRING:
ret = new NBTTagString(name, val.getAsString());
break;
case LIST:
NBTTagList nbtlist = new NBTTagList(name);
if (!val.isJsonArray())
{
// must be an array
return null;
}
Iterator<JsonElement> iter = val.getAsJsonArray().iterator();
while (iter.hasNext())
{
nbtlist.add(gsonValToNbt(iter.next(), null, elemtype, NBType.UNKNOWN));
}
ret = nbtlist;
break;
case COMPOUND:
NBTTagCompound compound = new NBTTagCompound(name);
if (!val.isJsonObject())
{
// must be an object
return null;
}
JsonObject jsonCompound = val.getAsJsonObject();
for(Entry<String, JsonElement> entry : jsonCompound.entrySet())
{
String childName = entry.getKey();
JsonElement childJson = entry.getValue();
NBTBase child = gsonToNbt(childJson, childName);
if (child == null) continue;
compound.set(childName, child);
}
ret = compound;
break;
}
return ret;
}
// -------------------------------------------- //
// NBT TO GSON
// -------------------------------------------- //
public static JsonObject nbtToGson(NBTBase nbt, boolean includeName)
{
JsonObject ret = new JsonObject();
String name = nbt.getName();
if (includeName && name != null)
{
ret.addProperty(NAME, name);
}
NBType type = NBType.getById(nbt.getTypeId());
ret.addProperty(TYPE, type.getName());
if (type == NBType.LIST)
{
NBType elemtype = NBType.UNKNOWN;
NBTTagList nbtl = (NBTTagList)nbt;
if (nbtl.size() > 0)
{
elemtype = NBType.getByTag(nbtl.get(0));
}
ret.addProperty(ELEMTYPE, elemtype.getName());
}
JsonElement val = nbtToGsonVal(nbt);
if (val == null)
{
return null;
}
ret.add(VAL, val);
return ret;
}
public static JsonElement nbtToGsonVal(NBTBase nbt)
{
JsonElement val = null;
NBType type = NBType.getByTag(nbt);
switch (type)
{
case END:
// this should never happen
break;
case BYTE:
val = new JsonPrimitive(((NBTTagByte) nbt).data);
break;
case SHORT:
val = new JsonPrimitive(((NBTTagShort) nbt).data);
break;
case INT:
val = new JsonPrimitive(((NBTTagInt) nbt).data);
break;
case LONG:
val = new JsonPrimitive(((NBTTagLong) nbt).data);
break;
case FLOAT:
val = new JsonPrimitive(((NBTTagFloat) nbt).data);
break;
case DOUBLE:
val = new JsonPrimitive(((NBTTagDouble) nbt).data);
break;
case BYTEARRAY:
JsonArray jsonBytes = new JsonArray();
for (byte elem : ((NBTTagByteArray) nbt).data)
{
jsonBytes.add(new JsonPrimitive(elem));
}
val = jsonBytes;
break;
case INTARRAY:
JsonArray jsonInts = new JsonArray();
for (int elem : ((NBTTagIntArray) nbt).data)
{
jsonInts.add(new JsonPrimitive(elem));
}
val = jsonInts;
break;
case STRING:
val = new JsonPrimitive(((NBTTagString) nbt).data);
break;
case LIST:
NBTTagList nbtlist = (NBTTagList)nbt;
int size = nbtlist.size();
if (size <= 0)
{
// NBTTagList may not be empty
return null;
}
JsonArray jsonElems = new JsonArray();
for (int i = 0 ; i < size ; i++)
{
jsonElems.add(nbtToGsonVal(nbtlist.get(i)));
}
val = jsonElems;
break;
case COMPOUND:
JsonObject jsonObject = new JsonObject();
for (NBTBase child : getCompoundChildren((NBTTagCompound)nbt))
{
jsonObject.add(child.getName(), nbtToGson(child, false));
}
val = jsonObject;
break;
}
return val;
}
// -------------------------------------------- //
// UTILS
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public static Collection<NBTBase> getCompoundChildren(NBTTagCompound compound)
{
return (Collection<NBTBase>)compound.c();
}
}