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 name: mcore5
version: 1.1.3 version: 1.1.4
main: com.massivecraft.mcore5.MCore main: com.massivecraft.mcore5.MCore
load: startup load: startup
permissions: 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.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta; 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.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.MapMeta;
@ -30,11 +36,11 @@ import com.massivecraft.mcore5.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore5.xlib.gson.JsonSerializer; import com.massivecraft.mcore5.xlib.gson.JsonSerializer;
/** /**
* This is a GSON serializer/deserializer for the Bukkit ItemStack. * This is a GSON serializer/deserializer for the Bukkit ItemStack. Why not use
* Why not use the built in Bukkit serializer/deserializer? I would have loved to do that :) * 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. * but sadly that one is YAML centric and cannot be used with json in a good
* This serializer requires manual updating to work but produces clean json. * way. This serializer requires manual updating to work but produces clean
* See the file itemstackformat.txt for more info. * json. See the file itemstackformat.txt for more info.
*/ */
public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack> public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSerializer<ItemStack>
{ {
@ -63,8 +69,14 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
public static final String SKULL_OWNER = "skull"; public static final String SKULL_OWNER = "skull";
// TODO rename to potion-effects?
public static final String POTION_EFFECTS = "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 // OTHER CONSTANTS
// -------------------------------------------- // // -------------------------------------------- //
@ -179,7 +191,8 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
// BASIC: ID // BASIC: ID
// -------------------------------------------- // // -------------------------------------------- //
public static void transferId(ItemStack stack, JsonObject json, boolean stack2json) public static void transferId(ItemStack stack, JsonObject json,
boolean stack2json)
{ {
if (stack2json) if (stack2json)
{ {
@ -286,13 +299,13 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
if (meta2json) if (meta2json)
{ {
if (!meta.hasLore()) return; if (!meta.hasLore()) return;
json.add(LORE, fromStringCollection(meta.getLore())); json.add(LORE, convertStringList(meta.getLore()));
} }
else else
{ {
JsonElement element = json.get(LORE); JsonElement element = json.get(LORE);
if (element == null) return; if (element == null) return;
meta.setLore(toStringCollection(element)); meta.setLore(convertStringList(element));
} }
} }
@ -305,27 +318,15 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
if (meta2json) if (meta2json)
{ {
if (!meta.hasEnchants()) return; if (!meta.hasEnchants()) return;
json.add(ENCHANTS, convertEnchantLevelMap(meta.getEnchants()));
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);
} }
else else
{ {
JsonElement element = json.get(ENCHANTS); JsonElement element = json.get(ENCHANTS);
if (element == null) return; if (element == null) return;
for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet())
JsonObject jsonEnchantments = element.getAsJsonObject();
for (Entry<String, JsonElement> enchantmentEntry: jsonEnchantments.entrySet())
{ {
int id = Integer.valueOf(enchantmentEntry.getKey()); meta.addEnchant(entry.getKey(), entry.getValue(), true);
Enchantment ench = Enchantment.getById(id);
int lvl = enchantmentEntry.getValue().getAsInt();
meta.addEnchant(ench, lvl, true);
} }
} }
} }
@ -379,6 +380,18 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
{ {
transferSkullMeta((SkullMeta) meta, json, meta2json); 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);
}
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -427,13 +440,13 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
if (meta2json) if (meta2json)
{ {
if (!meta.hasTitle()) return; if (!meta.hasTitle()) return;
json.add(BOOK_PAGES, fromStringCollection(meta.getPages())); json.add(BOOK_PAGES, convertStringList(meta.getPages()));
} }
else else
{ {
JsonElement element = json.get(BOOK_PAGES); JsonElement element = json.get(BOOK_PAGES);
if (element == null) return; if (element == null) return;
meta.setPages(toStringCollection(element)); meta.setPages(convertStringList(element));
} }
} }
@ -487,7 +500,7 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
if (meta2json) if (meta2json)
{ {
if (!meta.hasCustomEffects()) return; if (!meta.hasCustomEffects()) return;
json.add(POTION_EFFECTS, PotionEffectsAdapter.toJson(meta.getCustomEffects())); json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects()));
} }
else else
{ {
@ -495,7 +508,7 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
if (element == null) return; if (element == null) return;
meta.clearCustomEffects(); meta.clearCustomEffects();
for (PotionEffect pe : PotionEffectsAdapter.fromJson(element)) for (PotionEffect pe : convertPotionEffectList(element))
{ {
meta.addCustomEffect(pe, false); meta.addCustomEffect(pe, false);
} }
@ -521,11 +534,75 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
} }
} }
// -------------------------------------------- //
// 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 // MINI UTILS
// -------------------------------------------- // // -------------------------------------------- //
public static JsonArray fromStringCollection(Collection<String> strings) // String List
public static JsonArray convertStringList(Collection<String> strings)
{ {
JsonArray ret = new JsonArray(); JsonArray ret = new JsonArray();
for (String string : strings) for (String string : strings)
@ -535,9 +612,9 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
return ret; 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>(); List<String> ret = new ArrayList<String>();
Iterator<JsonElement> iter = array.iterator(); Iterator<JsonElement> iter = array.iterator();
@ -550,11 +627,100 @@ public class ItemStackAdapterV2 implements JsonDeserializer<ItemStack>, JsonSeri
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
// -------------------------------------------- // // -------------------------------------------- //
public static ItemStackAdapterV2 i = new ItemStackAdapterV2(); 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;
}
}