diff options
author | Wesley Wolfe <weswolf@aol.com> | 2013-01-18 02:21:38 -0600 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2013-01-18 02:58:07 -0600 |
commit | 01fc296fefdfc5c3c829f13e928f7ab920c4c6cb (patch) | |
tree | 9d8f17171e6aefb5cff328093f42ce4624496979 /src/main/java/org | |
parent | 5934a65729691e3437d22ddad98cd0b1a7df0c5c (diff) | |
download | craftbukkit-01fc296fefdfc5c3c829f13e928f7ab920c4c6cb.tar craftbukkit-01fc296fefdfc5c3c829f13e928f7ab920c4c6cb.tar.gz craftbukkit-01fc296fefdfc5c3c829f13e928f7ab920c4c6cb.tar.lz craftbukkit-01fc296fefdfc5c3c829f13e928f7ab920c4c6cb.tar.xz craftbukkit-01fc296fefdfc5c3c829f13e928f7ab920c4c6cb.zip |
Improve the item meta deserialization code-style
Fixed the ItemMetaFireworkTest
Add set power unit tests for FireworkMeta
Diffstat (limited to 'src/main/java/org')
9 files changed, 43 insertions, 110 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java index a2a91dec..0a65280e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java @@ -259,9 +259,4 @@ class CraftMetaBook extends CraftMetaItem implements BookMeta { return builder; } - - @Override - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.BOOK; - } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCharge.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCharge.java index 40ac8adf..bff3be9d 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCharge.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCharge.java @@ -8,7 +8,6 @@ import org.bukkit.FireworkEffect; import org.bukkit.Material; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; -import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta.Deserializers; import org.bukkit.inventory.meta.FireworkEffectMeta; import com.google.common.collect.ImmutableMap.Builder; @@ -126,9 +125,4 @@ class CraftMetaCharge extends CraftMetaItem implements FireworkEffectMeta { return builder; } - - @Override - Deserializers deserializer() { - return Deserializers.FIREWORK_EFFECT; - } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java index 4dd39444..757c9d44 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java @@ -8,7 +8,6 @@ import net.minecraft.server.NBTTagCompound; import org.bukkit.Material; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; -import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta.Deserializers; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.meta.EnchantmentStorageMeta; @@ -123,11 +122,6 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage return builder; } - @Override - Deserializers deserializer() { - return Deserializers.ENCHANTED; - } - boolean isEnchantedEmpty() { return !hasStoredEnchants(); } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java index 5d955ef2..3f9e4a38 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java @@ -17,7 +17,6 @@ import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific; import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific.To; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; -import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta.Deserializers; import org.bukkit.inventory.meta.FireworkMeta; import com.google.common.collect.ImmutableList; @@ -315,11 +314,6 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } @Override - Deserializers deserializer() { - return Deserializers.FIREWORK; - } - - @Override public CraftMetaFirework clone() { CraftMetaFirework meta = (CraftMetaFirework) super.clone(); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index ea6efcaa..519d900e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -4,6 +4,8 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -86,79 +88,55 @@ class CraftMetaItem implements ItemMeta, Repairable { public static class SerializableMeta implements ConfigurationSerializable { static final String TYPE_FIELD = "meta-type"; - enum Deserializers { - BOOK { - @Override - CraftMetaBook deserialize(Map<String, Object> map) { - return new CraftMetaBook(map); + static final ImmutableMap<Class<? extends CraftMetaItem>, String> classMap; + static final ImmutableMap<String, Constructor<? extends CraftMetaItem>> constructorMap; + + static { + classMap = ImmutableMap.<Class<? extends CraftMetaItem>, String>builder() + .put(CraftMetaBook.class, "BOOK") + .put(CraftMetaSkull.class, "SKULL") + .put(CraftMetaLeatherArmor.class, "LEATHER_ARMOR") + .put(CraftMetaMap.class, "MAP") + .put(CraftMetaPotion.class, "POTION") + .put(CraftMetaEnchantedBook.class, "ENCHANTED") + .put(CraftMetaFirework.class, "FIREWORK") + .put(CraftMetaCharge.class, "FIREWORK_EFFECT") + .put(CraftMetaItem.class, "UNSPECIFIC") + .build(); + + final ImmutableMap.Builder<String, Constructor<? extends CraftMetaItem>> classConstructorBuilder = ImmutableMap.builder(); + for (Map.Entry<Class<? extends CraftMetaItem>, String> mapping : classMap.entrySet()) { + try { + classConstructorBuilder.put(mapping.getValue(), mapping.getKey().getDeclaredConstructor(Map.class)); + } catch (NoSuchMethodException e) { + throw new AssertionError(e); } - }, - SKULL { - @Override - CraftMetaSkull deserialize(Map<String, Object> map) { - return new CraftMetaSkull(map); - } - }, - LEATHER_ARMOR { - @Override - CraftMetaLeatherArmor deserialize(Map<String, Object> map) { - return new CraftMetaLeatherArmor(map); - } - }, - MAP { - @Override - CraftMetaMap deserialize(Map<String, Object> map) { - return new CraftMetaMap(map); - } - }, - POTION { - @Override - CraftMetaPotion deserialize(Map<String, Object> map) { - return new CraftMetaPotion(map); - } - }, - ENCHANTED { - @Override - CraftMetaEnchantedBook deserialize(Map<String, Object> map) { - return new CraftMetaEnchantedBook(map); - } - }, - FIREWORK { - @Override - CraftMetaFirework deserialize(Map<String, Object> map) { - return new CraftMetaFirework(map); - } - }, - FIREWORK_EFFECT { - @Override - CraftMetaCharge deserialize(Map<String, Object> map) { - return new CraftMetaCharge(map); - } - }, - UNSPECIFIC { - @Override - CraftMetaItem deserialize(Map<String, Object> map) { - return new CraftMetaItem(map); - } - }; - - abstract CraftMetaItem deserialize(Map<String, Object> map); + } + constructorMap = classConstructorBuilder.build(); } private SerializableMeta() { } - public static ItemMeta deserialize(Map<String, Object> map) { + public static ItemMeta deserialize(Map<String, Object> map) throws Throwable { Validate.notNull(map, "Cannot deserialize null map"); String type = getString(map, TYPE_FIELD, false); - Deserializers deserializer = Deserializers.valueOf(type); + Constructor<? extends CraftMetaItem> constructor = constructorMap.get(type); - if (deserializer == null) { + if (constructor == null) { throw new IllegalArgumentException(type + " is not a valid " + TYPE_FIELD); } - return deserializer.deserialize(map); + try { + return constructor.newInstance(map); + } catch (final InstantiationException e) { + throw new AssertionError(e); + } catch (final IllegalAccessException e) { + throw new AssertionError(e); + } catch (final InvocationTargetException e) { + throw e.getCause(); + } } public Map<String, Object> serialize() { @@ -516,7 +494,7 @@ class CraftMetaItem implements ItemMeta, Repairable { public final Map<String, Object> serialize() { ImmutableMap.Builder<String, Object> map = ImmutableMap.builder(); - map.put(SerializableMeta.TYPE_FIELD, deserializer().name()); + map.put(SerializableMeta.TYPE_FIELD, SerializableMeta.classMap.get(getClass())); serialize(map); return map.build(); } @@ -553,11 +531,6 @@ class CraftMetaItem implements ItemMeta, Repairable { builder.put(key.BUKKIT, enchants.build()); } - @Overridden - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.UNSPECIFIC; - } - static void safelyAdd(Iterable<?> addFrom, Collection<String> addTo, int maxItemLength) { if (addFrom == null) { return; @@ -584,6 +557,6 @@ class CraftMetaItem implements ItemMeta, Repairable { @Override public final String toString() { - return deserializer().toString() + "_META:" + serialize(); // TODO: cry + return SerializableMeta.classMap.get(getClass()) + "_META:" + serialize(); // TODO: cry } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java index 6cb3bb1f..15bb4383 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java @@ -46,6 +46,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta { setColor(SerializableMeta.getObject(Color.class, map, COLOR.BUKKIT, true)); } + @Override void applyToItem(NBTTagCompound itemTag) { super.applyToItem(itemTag); @@ -63,6 +64,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta { return !(hasColor()); } + @Override boolean applicableTo(Material type) { switch(type) { case LEATHER_HELMET: @@ -104,11 +106,6 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta { } @Override - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.LEATHER_ARMOR; - } - - @Override boolean equalsCommon(CraftMetaItem meta) { if (!super.equalsCommon(meta)) { return false; @@ -126,6 +123,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta { return super.notUncommon(meta) && (meta instanceof CraftMetaLeatherArmor || isLeatherArmorEmpty()); } + @Override int applyHash() { final int original; int hash = original = super.applyHash(); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java index 5bad0edf..d8af3905 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java @@ -132,9 +132,4 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta { return builder; } - - @Override - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.MAP; - } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java index 3d565f64..ca024d0b 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java @@ -252,9 +252,4 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { return builder; } - - @Override - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.POTION; - } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java index 2bef968e..41efa552 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java @@ -126,9 +126,4 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { } return builder; } - - @Override - SerializableMeta.Deserializers deserializer() { - return SerializableMeta.Deserializers.SKULL; - } } |