Support some but not yet all Minecraft 1.8 ItemStack serialization. Backwards compatibility for 1.7 included.

This commit is contained in:
Olof Larsson 2015-04-10 13:11:58 +02:00
parent 30537ad7e3
commit 79e1d4171e
5 changed files with 1027 additions and 711 deletions

View File

@ -208,4 +208,19 @@
...
]
firework-flight: int
unbreakable: bool
flags:
[
"HIDE_UNBREAKABLE"
...
]
banner-base: byte (dye color) // It seems this one never is used
banner:
[
str (pattern id), byte (dye color),
str (pattern id), byte (dye color),
str (pattern id), byte (dye color),
...
]
}

View File

@ -1,38 +1,11 @@
package com.massivecraft.massivecore.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.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonParseException;
import com.massivecraft.massivecore.xlib.gson.JsonPrimitive;
import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
@ -43,60 +16,43 @@ import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
* way. This serializer requires manual updating to work but produces clean
* json. See the file itemstackformat.txt for more info.
*/
@SuppressWarnings("deprecation")
public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack>
{
// -------------------------------------------- //
// FIELD NAME CONSTANTS
// INSTANCE
// -------------------------------------------- //
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";
public static ItemStackAdapter i = new ItemStackAdapter();
public static ItemStackAdapter get() { return i; }
// -------------------------------------------- //
// OTHER CONSTANTS
// FIELDS
// -------------------------------------------- //
public static final int DEFAULT_ID;
public static final int DEFAULT_COUNT;
public static final int DEFAULT_DAMAGE;
static
protected ItemStackAdapterInner inner;
public ItemStackAdapterInner getInner() { return this.inner; }
public void setInner(ItemStackAdapterInner inner) { this.inner = inner; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public ItemStackAdapter()
{
ItemStack stack = createItemStack();
DEFAULT_ID = stack.getTypeId();
DEFAULT_COUNT = stack.getAmount();
DEFAULT_DAMAGE = stack.getDurability();
// 1.7
this.inner = ItemStackAdapterInnerV1_7.get();
// 1.8
try
{
this.inner = ItemStackAdapterInnerV1_8.get();
}
catch (Throwable t)
{
return;
}
}
// -------------------------------------------- //
// GSON INTERFACE IMPLEMENTATION
// -------------------------------------------- //
@ -104,650 +60,13 @@ public class ItemStackAdapter implements JsonDeserializer<ItemStack>, JsonSerial
@Override
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context)
{
return erialize(src);
return this.getInner().erialize(src);
}
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return erialize(json);
return this.getInner().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;
try
{
json.add(BOOK_PAGES, convertStringList(meta.getPages()));
}
catch (Exception e)
{
e.printStackTrace();
// It seems CraftMetaBook#getPages some times throw an NPE.
}
}
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 ItemStackAdapter i = new ItemStackAdapter();
public static ItemStackAdapter get() { return i; }
}

View File

@ -0,0 +1,12 @@
package com.massivecraft.massivecore.adapter;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
public interface ItemStackAdapterInner
{
public JsonObject erialize(ItemStack stack);
public ItemStack erialize(JsonElement jsonElement);
}

View File

@ -0,0 +1,711 @@
package com.massivecraft.massivecore.adapter;
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.Material;
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.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonPrimitive;
@SuppressWarnings("deprecation")
public class ItemStackAdapterInnerV1_7 implements ItemStackAdapterInner
{
// -------------------------------------------- //
// CONSTANTS: NAMES
// -------------------------------------------- //
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";
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ItemStackAdapterInnerV1_7 i = new ItemStackAdapterInnerV1_7();
public static ItemStackAdapterInnerV1_7 get() { return i; }
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
@Override
public JsonObject erialize(ItemStack stack)
{
// Check for "nothing"
if (stack == null) return null;
if (stack.getType() == Material.AIR) return null;
if (stack.getAmount() == 0) return null;
// Create a new JsonObject
JsonObject json = new JsonObject();
// Transfer data from stack to json
this.transferAll(stack, json, true);
return json;
}
@Override
public 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
this.transferAll(stack, json, false);
return stack;
}
// -------------------------------------------- //
// NOARG STACK CONSTRUCTOR
// -------------------------------------------- //
public static final int DEFAULT_ID = 0;
public static final int DEFAULT_COUNT = 1;
public static final int DEFAULT_DAMAGE = 0;
public ItemStack createItemStack()
{
return new ItemStack(DEFAULT_ID, DEFAULT_COUNT, (short)DEFAULT_DAMAGE);
}
// -------------------------------------------- //
// ALL
// -------------------------------------------- //
public void transferAll(ItemStack stack, JsonObject json, boolean stack2json)
{
this.transferBasic(stack, json, stack2json);
ItemMeta meta = stack.getItemMeta();
this.transferMeta(meta, json, stack2json);
if (stack2json == false)
{
stack.setItemMeta(meta);
}
}
// -------------------------------------------- //
// BASIC
// -------------------------------------------- //
public void transferBasic(ItemStack stack, JsonObject json, boolean stack2json)
{
this.transferId(stack, json, stack2json);
this.transferCount(stack, json, stack2json);
this.transferDamage(stack, json, stack2json);
}
// -------------------------------------------- //
// BASIC: ID
// -------------------------------------------- //
public 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 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 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 void transferMeta(ItemMeta meta, JsonObject json, boolean meta2json)
{
this.transferMetaUnspecific(meta, json, meta2json);
this.transferMetaSpecific(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META
// -------------------------------------------- //
public void transferMetaUnspecific(ItemMeta meta, JsonObject json, boolean meta2json)
{
this.transferName(meta, json, meta2json);
this.transferLore(meta, json, meta2json);
this.transferEnchants(meta, json, meta2json);
this.transferRepaircost(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META: NAME
// -------------------------------------------- //
public 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 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 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 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 void transferMetaSpecific(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta instanceof BookMeta)
{
this.transferBook((BookMeta) meta, json, meta2json);
}
else if (meta instanceof LeatherArmorMeta)
{
this.transferLeatherArmor((LeatherArmorMeta) meta, json, meta2json);
}
else if (meta instanceof MapMeta)
{
this.transferMap((MapMeta) meta, json, meta2json);
}
else if (meta instanceof PotionMeta)
{
this.transferPotion((PotionMeta) meta, json, meta2json);
}
else if (meta instanceof SkullMeta)
{
this.transferSkull((SkullMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkEffectMeta)
{
this.transferFireworkEffect((FireworkEffectMeta) meta, json, meta2json);
}
else if (meta instanceof FireworkMeta)
{
this.transferFirework((FireworkMeta) meta, json, meta2json);
}
else if (meta instanceof EnchantmentStorageMeta)
{
this.transferEnchantmentStorage((EnchantmentStorageMeta) meta, json, meta2json);
}
}
// -------------------------------------------- //
// SPECIFIC META: BOOK
// -------------------------------------------- //
public void transferBook(BookMeta meta, JsonObject json, boolean meta2json)
{
this.transferTitle(meta, json, meta2json);
this.transferAuthor(meta, json, meta2json);
this.transferPages(meta, json, meta2json);
}
public 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 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 void transferPages(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if (!meta.hasTitle()) return;
try
{
json.add(BOOK_PAGES, convertStringList(meta.getPages()));
}
catch (Exception e)
{
e.printStackTrace();
// It seems CraftMetaBook#getPages some times throw an NPE.
}
}
else
{
JsonElement element = json.get(BOOK_PAGES);
if (element == null) return;
meta.setPages(convertStringList(element));
}
}
// -------------------------------------------- //
// SPECIFIC META: LEATHER ARMOR
// -------------------------------------------- //
public void transferLeatherArmor(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 void transferMap(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 void transferPotion(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 void transferSkull(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 void transferFireworkEffect(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 void transferFirework(FireworkMeta meta, JsonObject json, boolean meta2json)
{
this.transferFireworkEffects(meta, json, meta2json);
this.transferFireworkPower(meta, json, meta2json);
}
public void transferFireworkEffects(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 void transferFireworkPower(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 void transferEnchantmentStorage(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;
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;
}
}

View File

@ -0,0 +1,259 @@
package com.massivecraft.massivecore.adapter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.bukkit.DyeColor;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.ItemMeta;
import com.massivecraft.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonPrimitive;
@SuppressWarnings("deprecation")
public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7
{
// -------------------------------------------- //
// CONSTANTS: NAMES
// -------------------------------------------- //
public static final String UNBREAKABLE = "unbreakable";
public static final String ITEM_FLAGS = "flags";
public static final String BANNER_BASE = "banner-base";
public static final String BANNER_PATTERNS = "banner";
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ItemStackAdapterInnerV1_8 i = new ItemStackAdapterInnerV1_8();
public static ItemStackAdapterInnerV1_8 get() { return i; }
// -------------------------------------------- //
// UNSPECIFIC META
// -------------------------------------------- //
@Override
public void transferMetaUnspecific(ItemMeta meta, JsonObject json, boolean meta2json)
{
super.transferMetaUnspecific(meta, json, meta2json);
this.transferUnbreakable(meta, json, meta2json);
this.transferItemFlags(meta, json, meta2json);
}
// -------------------------------------------- //
// UNSPECIFIC META: UNBREAKABLE
// -------------------------------------------- //
public void transferUnbreakable(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
boolean unbreakable = meta.spigot().isUnbreakable();
if ( ! unbreakable) return;
json.addProperty(UNBREAKABLE, unbreakable);
}
else
{
JsonElement element = json.get(UNBREAKABLE);
if (element == null) return;
meta.spigot().setUnbreakable(element.getAsBoolean());
}
}
// -------------------------------------------- //
// UNSPECIFIC META: ITEM FLAGS
// -------------------------------------------- //
public void transferItemFlags(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
JsonArray value = convertItemFlags(meta.getItemFlags());
if (value == null) return;
json.add(ITEM_FLAGS, value);
}
else
{
JsonElement element = json.get(ITEM_FLAGS);
if (element == null) return;
Set<ItemFlag> flags = convertItemFlags(element);
meta.addItemFlags(flags.toArray(new ItemFlag[0]));
}
}
public static JsonArray convertItemFlags(Set<ItemFlag> flags)
{
// Null
if (flags == null) return null;
if (flags.isEmpty()) return null;
// Create Ret
JsonArray ret = new JsonArray();
// Fill Ret
for (ItemFlag flag : flags)
{
ret.add(new JsonPrimitive(flag.name()));
}
// Return Ret
return ret;
}
public static Set<ItemFlag> convertItemFlags(JsonElement jsonElement)
{
// Create Ret
Set<ItemFlag> ret = new HashSet<ItemFlag>();
// Fill Ret
JsonArray json = jsonElement.getAsJsonArray();
for (JsonElement element : json)
{
try
{
ItemFlag flag = ItemFlag.valueOf(element.getAsString());
ret.add(flag);
}
catch (IllegalArgumentException ex)
{
// Ignore when we got a old String which does not map to a Enum value anymore.
}
}
// Return Ret
return ret;
}
// -------------------------------------------- //
// SPECIFIC META
// -------------------------------------------- //
@Override
public void transferMetaSpecific(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta instanceof BannerMeta)
{
this.transferBanner((BannerMeta) meta, json, meta2json);
}
else
{
super.transferMetaSpecific(meta, json, meta2json);
}
}
// -------------------------------------------- //
// SPECIFIC META: BANNER
// -------------------------------------------- //
public void transferBanner(BannerMeta meta, JsonObject json, boolean meta2json)
{
this.transferBannerBase(meta, json, meta2json);
this.transferBannerPatterns(meta, json, meta2json);
}
// -------------------------------------------- //
// SPECIFIC META: BANNER BASE
// -------------------------------------------- //
public void transferBannerBase(BannerMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
DyeColor baseColor = meta.getBaseColor();
// The default base color is null.
// This occurs when no patterns are set.
// In those cases the damage value of the item is used to denote color.
if (baseColor == null) return;
byte data = baseColor.getDyeData();
json.addProperty(BANNER_BASE, data);
}
else
{
JsonElement element = json.get(BANNER_BASE);
if (element == null) return;
DyeColor baseColor = DyeColor.getByDyeData(element.getAsByte());
meta.setBaseColor(baseColor);
}
}
// -------------------------------------------- //
// SPECIFIC META: BANNER PATTERNS
// -------------------------------------------- //
public void transferBannerPatterns(BannerMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
JsonArray data = convertBannerPatterns(meta.getPatterns());
if (data == null) return;
json.add(BANNER_PATTERNS, data);
}
else
{
JsonElement element = json.get(BANNER_PATTERNS);
if (element == null) return;
List<Pattern> patterns = convertBannerPatterns(element);
meta.setPatterns(patterns);
}
}
public static JsonArray convertBannerPatterns(List<Pattern> patterns)
{
// Null
if (patterns == null) return null;
if (patterns.isEmpty()) return null;
// Create Ret
JsonArray ret = new JsonArray();
// Fill Ret
for (Pattern pattern : patterns)
{
String i = pattern.getPattern().getIdentifier();
Byte c = pattern.getColor().getDyeData();
ret.add(new JsonPrimitive(i));
ret.add(new JsonPrimitive(c));
}
// Return Ret
return ret;
}
public static List<Pattern> convertBannerPatterns(JsonElement jsonElement)
{
// Create Ret
List<Pattern> ret = new ArrayList<Pattern>();
// Fill Ret
JsonArray json = jsonElement.getAsJsonArray();
Iterator<JsonElement> iter = json.iterator();
while (iter.hasNext())
{
JsonElement ie = iter.next();
if ( ! iter.hasNext()) break;
JsonElement ce = iter.next();
PatternType type = PatternType.getByIdentifier(ie.getAsString());
DyeColor color = DyeColor.getByDyeData(ce.getAsByte());
ret.add(new Pattern(color, type));
}
// Return Ret
return ret;
}
}