Add support for the new ItemMeta.

This commit is contained in:
Olof Larsson 2012-12-21 23:16:33 +01:00
parent c19f8ba061
commit cf23084db6
4 changed files with 435 additions and 176 deletions

View File

@ -1,5 +1,5 @@
name: mcore5
version: 1.1.3
version: 1.1.4
main: com.massivecraft.mcore5.MCore
load: startup
permissions:

View File

@ -0,0 +1,145 @@
package com.massivecraft.mcore5.adapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
import com.massivecraft.mcore5.util.MUtil;
import com.massivecraft.mcore5.xlib.gson.JsonArray;
import com.massivecraft.mcore5.xlib.gson.JsonElement;
import com.massivecraft.mcore5.xlib.gson.JsonObject;
import com.massivecraft.mcore5.xlib.gson.JsonPrimitive;
public class FireworkEffectAdapter
{
// -------------------------------------------- //
// FIELD CONSTANTS
// -------------------------------------------- //
public static final String FLICKER = "flicker";
public static final String TRAIL = "trail";
public static final String COLORS = "colors";
public static final String FADE_COLORS = "fade-colors";
public static final String TYPE = "type";
public static final boolean FLICKER_DEFAULT = false;
public static final boolean TRAIL_DEFAULT = false;
public static final List<Color> COLORS_DEFAULT = Collections.unmodifiableList(MUtil.list(Color.GREEN));
public static final List<Color> FADE_COLORS_DEFAULT = Collections.unmodifiableList(new ArrayList<Color>());
public static final Type TYPE_DEFAULT = Type.BALL_LARGE;
// -------------------------------------------- //
// TO JSON
// -------------------------------------------- //
public static JsonObject toJson(FireworkEffect fireworkEffect)
{
if (fireworkEffect == null) return null;
JsonObject ret = new JsonObject();
ret.addProperty(FLICKER, fireworkEffect.hasFlicker());
ret.addProperty(TRAIL, fireworkEffect.hasTrail());
ret.add(COLORS, fromColorCollection(fireworkEffect.getColors()));
ret.add(FADE_COLORS, fromColorCollection(fireworkEffect.getFadeColors()));
ret.addProperty(TYPE, fireworkEffect.getType().name());
return ret;
}
// -------------------------------------------- //
// FROM JSON
// -------------------------------------------- //
public static FireworkEffect fromJson(JsonElement jsonElement)
{
if (jsonElement == null) return null;
if ( ! jsonElement.isJsonObject()) return null;
JsonObject json = jsonElement.getAsJsonObject();
boolean flicker = FLICKER_DEFAULT;
boolean trail = TRAIL_DEFAULT;
List<Color> colors = COLORS_DEFAULT;
List<Color> fadeColors = FADE_COLORS_DEFAULT;
Type type = TYPE_DEFAULT;
JsonElement element;
element = json.get(FLICKER);
if (element != null)
{
flicker = element.getAsBoolean();
}
element = json.get(TRAIL);
if (element != null)
{
trail = element.getAsBoolean();
}
element = json.get(COLORS);
if (element != null)
{
colors = toColorCollection(element);
}
element = json.get(FADE_COLORS);
if (element != null)
{
fadeColors = toColorCollection(element);
}
element = json.get(TYPE);
if (element != null)
{
type = Type.valueOf(element.getAsString());
}
FireworkEffect ret = FireworkEffect.builder()
.flicker(flicker)
.trail(trail)
.withColor(colors)
.withFade(fadeColors)
.with(type)
.build();
return ret;
}
// -------------------------------------------- //
// MINI UTILS
// -------------------------------------------- //
public static JsonArray fromColorCollection(Collection<Color> colors)
{
JsonArray ret = new JsonArray();
for (Color color : colors)
{
ret.add(new JsonPrimitive(color.asRGB()));
}
return ret;
}
public static List<Color> toColorCollection(JsonElement json)
{
JsonArray array = json.getAsJsonArray();
List<Color> ret = new ArrayList<Color>();
Iterator<JsonElement> iter = array.iterator();
while (iter.hasNext())
{
JsonElement element = iter.next();
ret.add(Color.fromRGB(element.getAsInt()));
}
return ret;
}
}

View File

@ -3,14 +3,20 @@ package com.massivecraft.mcore5.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.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;
@ -30,52 +36,58 @@ import com.massivecraft.mcore5.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore5.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.
* 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";
// TODO rename to potion-effects?
public static final String POTION_EFFECTS = "effects";
public static final String FIREWORK_EFFECT = "firework-effect";
public static final String FIREWORK_EFFECTS = "firework-effects";
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;
// TODO: Awaiting https://bukkit.atlassian.net/browse/BUKKIT-3203
static final Color DEFAULT_LEATHER_COLOR = Color.fromRGB(0xA06540);
static
{
ItemStack stack = createItemStack();
@ -83,11 +95,11 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
DEFAULT_COUNT = stack.getAmount();
DEFAULT_DAMAGE = stack.getDurability();
}
// -------------------------------------------- //
// GSON INTERFACE IMPLEMENTATION
// -------------------------------------------- //
@Override
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context)
{
@ -99,87 +111,88 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
{
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
// 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)
public static void transferId(ItemStack stack, JsonObject json,
boolean stack2json)
{
if (stack2json)
{
@ -194,11 +207,11 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
stack.setTypeId(element.getAsInt());
}
}
// -------------------------------------------- //
// BASIC: COUNT
// -------------------------------------------- //
public static void transferCount(ItemStack stack, JsonObject json, boolean stack2json)
{
if (stack2json)
@ -214,11 +227,11 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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.
@ -235,21 +248,21 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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);
@ -257,16 +270,16 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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;
if (!meta.hasDisplayName()) return;
json.addProperty(NAME, meta.getDisplayName());
}
else
@ -276,87 +289,75 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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, fromStringCollection(meta.getLore()));
if (!meta.hasLore()) return;
json.add(LORE, convertStringList(meta.getLore()));
}
else
{
JsonElement element = json.get(LORE);
if (element == null) return;
meta.setLore(toStringCollection(element));
meta.setLore(convertStringList(element));
}
}
// -------------------------------------------- //
// UNSPECIFIC META: ENCHANTS
// -------------------------------------------- //
public static void transferEnchants(ItemMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if ( ! meta.hasEnchants()) return;
JsonObject enchants = new JsonObject();
for (Entry<Enchantment, Integer> entry : meta.getEnchants().entrySet())
{
enchants.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue());
}
json.add(ENCHANTS, enchants);
if (!meta.hasEnchants()) return;
json.add(ENCHANTS, convertEnchantLevelMap(meta.getEnchants()));
}
else
{
JsonElement element = json.get(ENCHANTS);
if (element == null) return;
JsonObject jsonEnchantments = element.getAsJsonObject();
for (Entry<String, JsonElement> enchantmentEntry: jsonEnchantments.entrySet())
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
{
int id = Integer.valueOf(enchantmentEntry.getKey());
Enchantment ench = Enchantment.getById(id);
int lvl = enchantmentEntry.getValue().getAsInt();
meta.addEnchant(ench, lvl, true);
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 (!(meta instanceof Repairable)) return;
Repairable repairable = (Repairable) meta;
if (meta2json)
{
if ( ! repairable.hasRepairCost()) return;
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)
@ -379,24 +380,36 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
{
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;
if (!meta.hasTitle()) return;
json.addProperty(BOOK_TITLE, meta.getTitle());
}
else
@ -406,12 +419,12 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
meta.setTitle(element.getAsString());
}
}
public static void transferAuthor(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if ( ! meta.hasTitle()) return;
if (!meta.hasTitle()) return;
json.addProperty(BOOK_AUTHOR, meta.getAuthor());
}
else
@ -421,33 +434,33 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
meta.setAuthor(element.getAsString());
}
}
public static void transferPages(BookMeta meta, JsonObject json, boolean meta2json)
{
if (meta2json)
{
if ( ! meta.hasTitle()) return;
json.add(BOOK_PAGES, fromStringCollection(meta.getPages()));
if (!meta.hasTitle()) return;
json.add(BOOK_PAGES, convertStringList(meta.getPages()));
}
else
{
JsonElement element = json.get(BOOK_PAGES);
if (element == null) return;
meta.setPages(toStringCollection(element));
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 (DEFAULT_LEATHER_COLOR.equals(color)) return;
json.addProperty(LEATHER_ARMOR_COLOR, color.asRGB());
}
else
@ -457,60 +470,60 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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;
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, PotionEffectsAdapter.toJson(meta.getCustomEffects()));
if (!meta.hasCustomEffects()) return;
json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects()));
}
else
{
JsonElement element = json.get(POTION_EFFECTS);
if (element == null) return;
meta.clearCustomEffects();
for (PotionEffect pe : PotionEffectsAdapter.fromJson(element))
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;
if (!meta.hasOwner()) return;
json.addProperty(SKULL_OWNER, meta.getOwner());
}
else
@ -520,12 +533,76 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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)
{
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));
}
}
// -------------------------------------------- //
// 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
// -------------------------------------------- //
public static JsonArray fromStringCollection(Collection<String> strings)
// String List
public static JsonArray convertStringList(Collection<String> strings)
{
JsonArray ret = new JsonArray();
for (String string : strings)
@ -534,27 +611,116 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
}
return ret;
}
public static List<String> toStringCollection(JsonElement json)
public static List<String> convertStringList(JsonElement jsonElement)
{
JsonArray array = json.getAsJsonArray();
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; }
public static ItemStackAdapterV2 get()
{
return i;
}
}

View File

@ -1,52 +0,0 @@
package com.massivecraft.mcore5.adapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.bukkit.potion.PotionEffect;
import com.massivecraft.mcore5.xlib.gson.JsonArray;
import com.massivecraft.mcore5.xlib.gson.JsonElement;
public class PotionEffectsAdapter
{
// -------------------------------------------- //
// TO JSON
// -------------------------------------------- //
public static JsonArray toJson(Collection<PotionEffect> potionEffects)
{
JsonArray ret = new JsonArray();
for (PotionEffect pe : potionEffects)
{
ret.add(PotionEffectAdapter.toJson(pe));
}
return ret;
}
// -------------------------------------------- //
// FROM JSON
// -------------------------------------------- //
public static List<PotionEffect> fromJson(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 pe = PotionEffectAdapter.fromJson(iter.next());
if (pe == null) continue;
ret.add(pe);
}
return ret;
}
}