From bec8286fb6e1ef10b01cca214e8306d86e985976 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 8 Jun 2016 13:00:40 +0200 Subject: [PATCH] MassiveCore - Editor Improvements. Only single singletons. Shared annotation for inner types. --- itemstackformat.txt | 6 +- src/com/massivecraft/massivecore/Engine.java | 1 - .../massivecore/MassiveCoreMConf.java | 3 +- .../massivecraft/massivecore/SoundEffect.java | 4 +- .../command/MassiveCommandToggle.java | 6 +- .../command/editor/annotation/EditorType.java | 6 +- ...ditorTypeSet.java => EditorTypeInner.java} | 8 +- .../editor/annotation/EditorTypeList.java | 17 -- .../editor/annotation/EditorTypeMap.java | 19 -- .../massivecore/CmdMassiveCoreTest.java | 4 +- .../command/type/RegistryType.java | 201 ++++++++++++------ .../TypeConverterBannerPatternType.java | 22 +- .../command/type/enumeration/TypeSoundId.java | 20 ++ ...eBoolean.java => TypeBooleanAbstract.java} | 36 ++-- .../command/type/primitive/TypeBooleanOn.java | 16 ++ .../type/primitive/TypeBooleanTrue.java | 16 ++ .../type/primitive/TypeBooleanYes.java | 16 ++ .../massivecore/item/DataFireworkEffect.java | 6 +- .../massivecore/item/DataItemStack.java | 14 +- .../massivecore/item/WriterAbstract.java | 4 +- .../massivecore/util/ReflectionUtil.java | 22 ++ 21 files changed, 279 insertions(+), 168 deletions(-) rename src/com/massivecraft/massivecore/command/editor/annotation/{EditorTypeSet.java => EditorTypeInner.java} (63%) delete mode 100644 src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeList.java delete mode 100644 src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeMap.java create mode 100644 src/com/massivecraft/massivecore/command/type/enumeration/TypeSoundId.java rename src/com/massivecraft/massivecore/command/type/primitive/{TypeBoolean.java => TypeBooleanAbstract.java} (72%) create mode 100644 src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanOn.java create mode 100644 src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanTrue.java create mode 100644 src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanYes.java diff --git a/itemstackformat.txt b/itemstackformat.txt index 09f2b4b6..29561b54 100644 --- a/itemstackformat.txt +++ b/itemstackformat.txt @@ -214,12 +214,10 @@ "HIDE_UNBREAKABLE" ... ] - banner-base: byte (dye color) // It seems this one never is used + banner-base: byte (dye color) banner: [ - str (pattern id), byte (dye color), - str (pattern id), byte (dye color), - str (pattern id), byte (dye color), + *banner-pattern* ... ] diff --git a/src/com/massivecraft/massivecore/Engine.java b/src/com/massivecraft/massivecore/Engine.java index 6d57a4e5..0d427848 100644 --- a/src/com/massivecraft/massivecore/Engine.java +++ b/src/com/massivecraft/massivecore/Engine.java @@ -9,7 +9,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockMultiPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.inventory.EquipmentSlot; import org.bukkit.scheduler.BukkitTask; import com.massivecraft.massivecore.collections.MassiveSet; diff --git a/src/com/massivecraft/massivecore/MassiveCoreMConf.java b/src/com/massivecraft/massivecore/MassiveCoreMConf.java index 67cb693b..ca11f6d3 100644 --- a/src/com/massivecraft/massivecore/MassiveCoreMConf.java +++ b/src/com/massivecraft/massivecore/MassiveCoreMConf.java @@ -9,6 +9,7 @@ import org.bukkit.permissions.Permissible; import com.massivecraft.massivecore.command.editor.annotation.EditorName; import com.massivecraft.massivecore.command.editor.annotation.EditorType; import com.massivecraft.massivecore.command.type.TypeMillisDiff; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanOn; import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.PermissionUtil; @@ -122,7 +123,7 @@ public class MassiveCoreMConf extends Entity @EditorType(TypeMillisDiff.class) public volatile long millisBetweenRemotePollWithPusher = TimeUnit.MILLIS_PER_MINUTE * 1; - @EditorType(fieldName = "iOn") + @EditorType(TypeBooleanOn.class) public boolean warnOnLocalAlter = false; // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/SoundEffect.java b/src/com/massivecraft/massivecore/SoundEffect.java index 6258edfc..eeb12fa9 100644 --- a/src/com/massivecraft/massivecore/SoundEffect.java +++ b/src/com/massivecraft/massivecore/SoundEffect.java @@ -11,8 +11,8 @@ import org.bukkit.entity.Player; import com.massivecraft.massivecore.command.editor.annotation.EditorName; import com.massivecraft.massivecore.command.editor.annotation.EditorNullable; import com.massivecraft.massivecore.command.editor.annotation.EditorType; -import com.massivecraft.massivecore.command.type.TypeId; import com.massivecraft.massivecore.command.type.enumeration.TypeSound; +import com.massivecraft.massivecore.command.type.enumeration.TypeSoundId; import com.massivecraft.massivecore.util.MUtil; public final class SoundEffect implements Serializable @@ -25,7 +25,7 @@ public final class SoundEffect implements Serializable @EditorNullable(false) @EditorName("sound") - @EditorType(value = TypeId.class, fieldName = "iSound") + @EditorType(TypeSoundId.class) private final String soundId; public String getSoundId() { return this.soundId; } public Sound getSound() diff --git a/src/com/massivecraft/massivecore/command/MassiveCommandToggle.java b/src/com/massivecraft/massivecore/command/MassiveCommandToggle.java index 6d8ab413..0b7a740a 100644 --- a/src/com/massivecraft/massivecore/command/MassiveCommandToggle.java +++ b/src/com/massivecraft/massivecore/command/MassiveCommandToggle.java @@ -3,7 +3,7 @@ package com.massivecraft.massivecore.command; import java.util.List; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.type.primitive.TypeBoolean; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanOn; import com.massivecraft.massivecore.util.Txt; public abstract class MassiveCommandToggle extends MassiveCommand @@ -29,7 +29,7 @@ public abstract class MassiveCommandToggle extends MassiveCommand public MassiveCommandToggle() { // Parameters - this.addParameter(TypeBoolean.getOn(), "on|off", "toggle"); + this.addParameter(TypeBooleanOn.get(), "on|off", "toggle"); } // -------------------------------------------- // @@ -60,7 +60,7 @@ public abstract class MassiveCommandToggle extends MassiveCommand // Args boolean before = this.getValue(); boolean after = this.readArg( ! before); - String afterDesc = TypeBoolean.getOn().getVisual(after, sender); + String afterDesc = TypeBooleanOn.get().getVisual(after, sender); // NoChange if (after == before) diff --git a/src/com/massivecraft/massivecore/command/editor/annotation/EditorType.java b/src/com/massivecraft/massivecore/command/editor/annotation/EditorType.java index 91c193a8..4c1d4606 100644 --- a/src/com/massivecraft/massivecore/command/editor/annotation/EditorType.java +++ b/src/com/massivecraft/massivecore/command/editor/annotation/EditorType.java @@ -9,9 +9,5 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface EditorType { - // This is the type class. - Class value() default void.class; - - // The name of the singleton instance field to use. - String fieldName() default "i"; + Class value(); } diff --git a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeSet.java b/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeInner.java similarity index 63% rename from src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeSet.java rename to src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeInner.java index b1282683..4e40b186 100644 --- a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeSet.java +++ b/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeInner.java @@ -7,11 +7,7 @@ import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) -public @interface EditorTypeSet +public @interface EditorTypeInner { - // This is the type class. - Class value(); - - // The name of the singleton instance field to use. - String fieldName() default "i"; + Class[] value(); } diff --git a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeList.java b/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeList.java deleted file mode 100644 index b28cecfe..00000000 --- a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeList.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.massivecraft.massivecore.command.editor.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface EditorTypeList -{ - // This is the type class. - Class value(); - - // The name of the singleton instance field to use. - String fieldName() default "i"; -} diff --git a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeMap.java b/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeMap.java deleted file mode 100644 index bf4e176e..00000000 --- a/src/com/massivecraft/massivecore/command/editor/annotation/EditorTypeMap.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.massivecraft.massivecore.command.editor.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface EditorTypeMap -{ - // This is the type class. - Class typeKey(); - Class typeValue(); - - // The name of the singleton instance field to use. - String fieldNameKey() default "i"; - String fieldNameValue() default "i"; -} diff --git a/src/com/massivecraft/massivecore/command/massivecore/CmdMassiveCoreTest.java b/src/com/massivecraft/massivecore/command/massivecore/CmdMassiveCoreTest.java index 7d224934..84b072d8 100644 --- a/src/com/massivecraft/massivecore/command/massivecore/CmdMassiveCoreTest.java +++ b/src/com/massivecraft/massivecore/command/massivecore/CmdMassiveCoreTest.java @@ -14,7 +14,7 @@ import com.massivecraft.massivecore.command.Visibility; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; import com.massivecraft.massivecore.command.type.TypeItemStack; -import com.massivecraft.massivecore.command.type.primitive.TypeBoolean; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes; import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.util.InventoryUtil; @@ -30,7 +30,7 @@ public class CmdMassiveCoreTest extends MassiveCommand this.addAliases("test"); // Parameters - this.addParameter(false, TypeBoolean.getYes(), "set", "no"); + this.addParameter(false, TypeBooleanYes.get(), "set", "no"); // Requirements this.addRequirements(RequirementHasPerm.get(MassiveCorePerm.TEST)); diff --git a/src/com/massivecraft/massivecore/command/type/RegistryType.java b/src/com/massivecraft/massivecore/command/type/RegistryType.java index 1b839807..06437902 100644 --- a/src/com/massivecraft/massivecore/command/type/RegistryType.java +++ b/src/com/massivecraft/massivecore/command/type/RegistryType.java @@ -8,12 +8,11 @@ import java.util.Map.Entry; import java.util.Set; import com.massivecraft.massivecore.collections.ExceptionSet; +import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.WorldExceptionSet; import com.massivecraft.massivecore.command.editor.annotation.EditorType; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeList; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeMap; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeSet; +import com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner; import com.massivecraft.massivecore.command.type.combined.TypeDataBannerPattern; import com.massivecraft.massivecore.command.type.combined.TypeDataPotionEffect; import com.massivecraft.massivecore.command.type.combined.TypeEntry; @@ -44,7 +43,7 @@ import com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType; import com.massivecraft.massivecore.command.type.enumeration.TypeSound; import com.massivecraft.massivecore.command.type.enumeration.TypeVillagerProfession; import com.massivecraft.massivecore.command.type.enumeration.TypeWorldType; -import com.massivecraft.massivecore.command.type.primitive.TypeBoolean; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanTrue; import com.massivecraft.massivecore.command.type.primitive.TypeByte; import com.massivecraft.massivecore.command.type.primitive.TypeDouble; import com.massivecraft.massivecore.command.type.primitive.TypeFloat; @@ -64,111 +63,171 @@ public class RegistryType // -------------------------------------------- // private static final Map, Type> registry = new MassiveMap<>(); + public static void register(Class clazz, Type type) { if (clazz == null) throw new NullPointerException("clazz"); if (type == null) throw new NullPointerException("type"); registry.put(clazz, type); } + public static void register(Type type) { if (type == null) throw new NullPointerException("type"); register(type.getClazz(), type); } + @SuppressWarnings("unchecked") public static Type unregister(Class clazz) { if (clazz == null) throw new NullPointerException("clazz"); return (Type) registry.remove(clazz); } + public static boolean isRegistered(Class clazz) { if (clazz == null) throw new NullPointerException("clazz"); return registry.containsKey(clazz); } - public static Type getType(Field field) + // -------------------------------------------- // + // GET TYPE + // -------------------------------------------- // + + public static Type getType(Field field, java.lang.reflect.Type fieldType, boolean strictThrow) { - try + if (field != null) { - EditorType annotation = field.getAnnotation(EditorType.class); - if (annotation != null) + EditorType annotationType = ReflectionUtil.getAnnotation(field, EditorType.class); + if (annotationType != null) { - Class clazz = annotation.value(); - if (clazz == void.class) clazz = getType(field.getGenericType()).getClass(); - return getType(clazz, annotation.fieldName()); + Class typeClass = annotationType.value(); + Type type = ReflectionUtil.getSingletonInstance(typeClass); + return type; } - EditorTypeList annList = field.getAnnotation(EditorTypeList.class); - if (annList != null) + if (fieldType == null) { - return TypeList.get(getType(annList.value(), annList.fieldName())); + fieldType = field.getGenericType(); } - - EditorTypeSet annSet = field.getAnnotation(EditorTypeSet.class); - if (annSet != null) - { - return TypeSet.get(getType(annSet.value(), annSet.fieldName())); - } - - EditorTypeMap annMap = field.getAnnotation(EditorTypeMap.class); - if (annMap != null) - { - return TypeMap.get(getType(annMap.typeKey(), annMap.fieldNameKey()), getType(annMap.typeValue(), annMap.fieldNameValue())); - } - } - catch (Throwable t) - { - // This has to do with backwards compatibility (Usually 1.7). - // The EditorType annotations may trigger creation of type class instances. - // Those type classes may refer to Bukkit classes not present. - // This issue was first encountered for TypeDataItemStack. } - return getType(field.getGenericType()); - } - private static Type getType(Class clazz, String fieldName) - { - return ReflectionUtil.getField(clazz, fieldName, null); + if (fieldType != null) + { + if (fieldType instanceof ParameterizedType) + { + Class fieldClass = field.getType(); + List> innerTypes; + + if (List.class.isAssignableFrom(fieldClass)) + { + innerTypes = getInnerTypes(field, fieldType, 1); + return TypeList.get(innerTypes.get(0)); + } + + if (Set.class.isAssignableFrom(fieldClass)) + { + innerTypes = getInnerTypes(field, fieldType, 1); + return TypeSet.get(innerTypes.get(0)); + } + + if (Entry.class.isAssignableFrom(fieldClass)) + { + innerTypes = getInnerTypes(field, fieldType, 2); + return TypeEntry.get(innerTypes.get(0), innerTypes.get(1)); + } + + if (Map.class.isAssignableFrom(fieldClass)) + { + innerTypes = getInnerTypes(field, fieldType, 2); + return TypeMap.get(innerTypes.get(0), innerTypes.get(1)); + } + + if (strictThrow) throw new IllegalArgumentException("Unhandled ParameterizedType: " + fieldType); + return null; + } + + if (fieldType instanceof Class) + { + Type type = registry.get(fieldType); + if (strictThrow && type == null) throw new IllegalStateException(fieldType + " is not registered."); + return type; + } + + throw new IllegalArgumentException("Neither ParameterizedType nor Class: " + fieldType); + } + + throw new IllegalArgumentException("No Information Supplied"); } - public static Type getType(java.lang.reflect.Type reflectType) + public static Type getType(Field field, boolean strictThrow) { - if (reflectType instanceof Class) - { - Type type = registry.get(reflectType); - if (type == null) throw new IllegalStateException(reflectType + " is not registered."); - return type; - } + return getType(field, null, strictThrow); + } + + public static Type getType(java.lang.reflect.Type fieldType, boolean strictThrow) + { + return getType(null, fieldType, strictThrow); + } + + public static Type getType(Field field) + { + return getType(field, true); + } + + public static Type getType(java.lang.reflect.Type fieldType) + { + return getType(fieldType, true); + } + + // -------------------------------------------- // + // GET INNER TYPES + // -------------------------------------------- // + + public static List> getInnerTypes(Field field, java.lang.reflect.Type fieldType, int amountRequired) + { + // Create + List> ret = new MassiveList<>(); - if (reflectType instanceof ParameterizedType) + // Fill > Annotation + if (field != null) { - ParameterizedType paramType = (ParameterizedType) reflectType; - Class parent = (Class) paramType.getRawType(); - - if (Map.class.isAssignableFrom(parent)) + EditorTypeInner annotation = ReflectionUtil.getAnnotation(field, EditorTypeInner.class); + if (annotation != null) { - TypeEntry typeEntry = TypeEntry.get(getType(paramType.getActualTypeArguments()[0]), getType(paramType.getActualTypeArguments()[1])); - return TypeMap.get(typeEntry); + Class[] innerTypeClasses = annotation.value(); + for (Class innerTypeClass : innerTypeClasses) + { + Type innerType = ReflectionUtil.getSingletonInstance(innerTypeClass); + ret.add(innerType); + } } - if (List.class.isAssignableFrom(parent)) + if (fieldType == null) { - return TypeList.get(getType(paramType.getActualTypeArguments()[0])); - } - - if (Set.class.isAssignableFrom(parent)) - { - return TypeSet.get(getType(paramType.getActualTypeArguments()[0])); - } - - if (Entry.class.isAssignableFrom(parent)) - { - return TypeEntry.get(getType(paramType.getActualTypeArguments()[0]), getType(paramType.getActualTypeArguments()[1])); + fieldType = field.getGenericType(); } } - throw new IllegalArgumentException("Unknown type: " + reflectType); + // Fill > Reflection + if (fieldType != null) + { + if (fieldType instanceof ParameterizedType) + { + ParameterizedType parameterizedType = (ParameterizedType)fieldType; + int count = 0; + for (java.lang.reflect.Type actualTypeArgument : parameterizedType.getActualTypeArguments()) + { + boolean strictThrow = (amountRequired < 0 || count < amountRequired); + Type innerType = getType(actualTypeArgument, strictThrow); + ret.add(innerType); + count++; + } + } + } + + // Return + return ret; } // -------------------------------------------- // @@ -181,8 +240,8 @@ public class RegistryType public static void registerAll() { // Primitive - register(Boolean.TYPE, TypeBoolean.getTrue()); - register(Boolean.class, TypeBoolean.getTrue()); + register(Boolean.TYPE, TypeBooleanTrue.get()); + register(Boolean.class, TypeBooleanTrue.get()); register(Byte.TYPE, TypeByte.get()); register(Byte.class, TypeByte.get()); @@ -222,6 +281,14 @@ public class RegistryType try { register(TypeRabbitType.get()); + } + catch (Throwable t) + { + + } + + try + { register(TypeDamageModifier.get()); } catch (Throwable t) diff --git a/src/com/massivecraft/massivecore/command/type/convert/TypeConverterBannerPatternType.java b/src/com/massivecraft/massivecore/command/type/convert/TypeConverterBannerPatternType.java index 182e70c0..61f4d30f 100644 --- a/src/com/massivecraft/massivecore/command/type/convert/TypeConverterBannerPatternType.java +++ b/src/com/massivecraft/massivecore/command/type/convert/TypeConverterBannerPatternType.java @@ -1,24 +1,30 @@ package com.massivecraft.massivecore.command.type.convert; -import org.bukkit.block.banner.PatternType; - +import com.massivecraft.massivecore.command.type.Type; import com.massivecraft.massivecore.command.type.enumeration.TypePatternType; import com.massivecraft.massivecore.command.type.primitive.TypeString; +import com.massivecraft.massivecore.item.Converter; import com.massivecraft.massivecore.item.ConverterFromBannerPatternType; import com.massivecraft.massivecore.item.ConverterToBannerPatternType; -public class TypeConverterBannerPatternType extends TypeConverter +// Minecraft 1.7 Compatibility +public class TypeConverterBannerPatternType extends TypeConverter { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - private static TypeConverterBannerPatternType i = new TypeConverterBannerPatternType(); - public static TypeConverterBannerPatternType get() { return i; } - - public TypeConverterBannerPatternType() + private static TypeConverterBannerPatternType i = null; + @SuppressWarnings("unchecked") + public static TypeConverterBannerPatternType get() { - super(TypePatternType.get(), TypeString.get(), ConverterFromBannerPatternType.get(), ConverterToBannerPatternType.get()); + if (i == null) i = new TypeConverterBannerPatternType<>(TypePatternType.get(), ConverterFromBannerPatternType.get(), ConverterToBannerPatternType.get()); + return (TypeConverterBannerPatternType) i; + } + + public TypeConverterBannerPatternType(Type typeA, Converter a2b, Converter b2a) + { + super(typeA, TypeString.get(), a2b, b2a); } } diff --git a/src/com/massivecraft/massivecore/command/type/enumeration/TypeSoundId.java b/src/com/massivecraft/massivecore/command/type/enumeration/TypeSoundId.java new file mode 100644 index 00000000..a8756143 --- /dev/null +++ b/src/com/massivecraft/massivecore/command/type/enumeration/TypeSoundId.java @@ -0,0 +1,20 @@ +package com.massivecraft.massivecore.command.type.enumeration; + +import org.bukkit.Sound; + +import com.massivecraft.massivecore.command.type.TypeId; + +public class TypeSoundId extends TypeId +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeSoundId i = new TypeSoundId(); + public static TypeSoundId get() { return i; } + public TypeSoundId() + { + super(TypeSound.get()); + } + +} diff --git a/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanAbstract.java similarity index 72% rename from src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java rename to src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanAbstract.java index d6ac6d18..46071cc8 100644 --- a/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java +++ b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanAbstract.java @@ -8,36 +8,32 @@ import org.bukkit.command.CommandSender; import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.command.type.TypeAbstractChoice; -public class TypeBoolean extends TypeAbstractChoice +public abstract class TypeBooleanAbstract extends TypeAbstractChoice { // -------------------------------------------- // // CONSTANTS // -------------------------------------------- // + public static final String NAME_YES = "Yes"; + public static final String NAME_TRUE = "True"; + public static final String NAME_ON = "On"; + + public static final String NAME_NO = "No"; + public static final String NAME_FALSE = "False"; + public static final String NAME_OFF = "Off"; + public static final Set NAMES_TRUE = new MassiveSet( - "Yes", - "True", - "On" + NAME_YES, + NAME_TRUE, + NAME_ON ); public static final Set NAMES_FALSE = new MassiveSet( - "No", - "False", - "Off" + NAME_NO, + NAME_FALSE, + NAME_OFF ); - // -------------------------------------------- // - // INSTANCE - // -------------------------------------------- // - - private static TypeBoolean iYes = new TypeBoolean("Yes", "No"); - private static TypeBoolean iTrue = new TypeBoolean("True", "False"); - private static TypeBoolean iOn = new TypeBoolean("On", "Off"); - - public static TypeBoolean getYes() { return iYes; } - public static TypeBoolean getTrue() { return iTrue; } - public static TypeBoolean getOn() { return iOn; } - // -------------------------------------------- // // FIELDS // -------------------------------------------- // @@ -52,7 +48,7 @@ public class TypeBoolean extends TypeAbstractChoice // CONSTRUCT // -------------------------------------------- // - public TypeBoolean(String t, String f) + public TypeBooleanAbstract(String t, String f) { super(Boolean.class); this.stringTrue = t; diff --git a/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanOn.java b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanOn.java new file mode 100644 index 00000000..60c15560 --- /dev/null +++ b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanOn.java @@ -0,0 +1,16 @@ +package com.massivecraft.massivecore.command.type.primitive; + +public class TypeBooleanOn extends TypeBooleanAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeBooleanOn i = new TypeBooleanOn(); + public static TypeBooleanOn get() { return i; } + public TypeBooleanOn() + { + super(NAME_ON, NAME_OFF); + } + +} diff --git a/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanTrue.java b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanTrue.java new file mode 100644 index 00000000..d2eeeeeb --- /dev/null +++ b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanTrue.java @@ -0,0 +1,16 @@ +package com.massivecraft.massivecore.command.type.primitive; + +public class TypeBooleanTrue extends TypeBooleanAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeBooleanTrue i = new TypeBooleanTrue(); + public static TypeBooleanTrue get() { return i; } + public TypeBooleanTrue() + { + super(NAME_TRUE, NAME_FALSE); + } + +} diff --git a/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanYes.java b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanYes.java new file mode 100644 index 00000000..568783c5 --- /dev/null +++ b/src/com/massivecraft/massivecore/command/type/primitive/TypeBooleanYes.java @@ -0,0 +1,16 @@ +package com.massivecraft.massivecore.command.type.primitive; + +public class TypeBooleanYes extends TypeBooleanAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeBooleanYes i = new TypeBooleanYes(); + public static TypeBooleanYes get() { return i; } + public TypeBooleanYes() + { + super(NAME_YES, NAME_NO); + } + +} diff --git a/src/com/massivecraft/massivecore/item/DataFireworkEffect.java b/src/com/massivecraft/massivecore/item/DataFireworkEffect.java index e7a97a5f..93ee0330 100644 --- a/src/com/massivecraft/massivecore/item/DataFireworkEffect.java +++ b/src/com/massivecraft/massivecore/item/DataFireworkEffect.java @@ -12,7 +12,7 @@ import org.bukkit.FireworkEffect; import com.massivecraft.massivecore.collections.MassiveListDef; import com.massivecraft.massivecore.command.editor.annotation.EditorMethods; import com.massivecraft.massivecore.command.editor.annotation.EditorType; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeList; +import com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner; import com.massivecraft.massivecore.command.type.convert.TypeConverterColor; import com.massivecraft.massivecore.command.type.convert.TypeConverterFireworkEffectType; import com.massivecraft.massivecore.comparator.ComparatorSmart; @@ -46,12 +46,12 @@ public class DataFireworkEffect implements Comparable public boolean hasTrail() { return get(this.trail, DEFAULT_TRAIL); } public DataFireworkEffect setTrail(boolean trail) { this.trail = set(trail, DEFAULT_TRAIL); return this; } - @EditorTypeList(TypeConverterColor.class) + @EditorTypeInner(TypeConverterColor.class) private MassiveListDef colors = null; public List getColors() { return get(this.colors, DEFAULT_COLORS); } public DataFireworkEffect setColors(List colors) { this.colors = set(colors, DEFAULT_COLORS); return this; } - @EditorTypeList(TypeConverterColor.class) + @EditorTypeInner(TypeConverterColor.class) @SerializedName("fade-colors") private MassiveListDef fadeColors = null; public List getFadeColors() { return get(this.fadeColors, DEFAULT_FADE_COLORS); } diff --git a/src/com/massivecraft/massivecore/item/DataItemStack.java b/src/com/massivecraft/massivecore/item/DataItemStack.java index 5180f2d1..074cda0f 100644 --- a/src/com/massivecraft/massivecore/item/DataItemStack.java +++ b/src/com/massivecraft/massivecore/item/DataItemStack.java @@ -18,9 +18,7 @@ import com.massivecraft.massivecore.collections.MassiveTreeMapDef; import com.massivecraft.massivecore.collections.MassiveTreeSetDef; import com.massivecraft.massivecore.command.editor.annotation.EditorMethods; import com.massivecraft.massivecore.command.editor.annotation.EditorType; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeList; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeMap; -import com.massivecraft.massivecore.command.editor.annotation.EditorTypeSet; +import com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner; import com.massivecraft.massivecore.command.type.TypeMaterialId; import com.massivecraft.massivecore.command.type.convert.TypeConverterColor; import com.massivecraft.massivecore.command.type.convert.TypeConverterDyeColor; @@ -107,7 +105,7 @@ public class DataItemStack implements Comparable public String getName() { return get(this.name, DEFAULT_NAME); } public DataItemStack setName(String name) { this.name = set(name, DEFAULT_NAME); return this; } - @EditorTypeList(TypeStringParsed.class) + @EditorTypeInner(TypeStringParsed.class) private MassiveListDef lore = null; public List getLore() { return get(this.lore, DEFAULT_LORE); } public DataItemStack setLore(List lore) { this.lore = set(lore, DEFAULT_LORE); return this; } @@ -115,7 +113,7 @@ public class DataItemStack implements Comparable // The Bukkit ItemMeta#getEnchants() is not sorted by the enchant id. // There may be some sort of custom sorting order, I'm not sure. // We are however enforcing sorting by the enchant id ourselves to be sure. - @EditorTypeMap(typeKey = TypeConverterEnchant.class, typeValue = TypeInteger.class) + @EditorTypeInner({TypeConverterEnchant.class, TypeInteger.class}) private MassiveTreeMapDef enchants = null; public Map getEnchants() { return get(this.enchants, DEFAULT_ENCHANTS); } public DataItemStack setEnchants(Map enchants) { this.enchants = set(enchants, DEFAULT_ENCHANTS); return this; } @@ -137,7 +135,7 @@ public class DataItemStack implements Comparable public String getAuthor() { return get(this.author, DEFAULT_AUTHOR); } public DataItemStack setAuthor(String author) { this.author = set(author, DEFAULT_AUTHOR); return this; } - @EditorTypeList(TypeStringParsed.class) + @EditorTypeInner(TypeStringParsed.class) private MassiveListDef pages = null; public List getPages() { return get(this.pages, DEFAULT_PAGES); } public DataItemStack setPages(Collection pages) { this.pages = set(pages, DEFAULT_PAGES); return this; } @@ -206,7 +204,7 @@ public class DataItemStack implements Comparable // FIELDS > STORED ENCHANTS // -------------------------------------------- // - @EditorTypeMap(typeKey = TypeConverterEnchant.class, typeValue = TypeInteger.class) + @EditorTypeInner({TypeConverterEnchant.class, TypeInteger.class}) @SerializedName("stored-enchants") private MassiveTreeMapDef storedEnchants = null; public Map getStoredEnchants() { return get(this.storedEnchants, DEFAULT_STORED_ENCHANTS); } @@ -226,7 +224,7 @@ public class DataItemStack implements Comparable // -------------------------------------------- // // SINCE: 1.8 - @EditorTypeSet(TypeConverterItemFlag.class) + @EditorTypeInner(TypeConverterItemFlag.class) private MassiveTreeSetDef flags = null; public Set getFlags() { return get(this.flags, DEFAULT_FLAGS); } public DataItemStack setFlags(Set flags) { this.flags = set(flags, DEFAULT_FLAGS); return this; } diff --git a/src/com/massivecraft/massivecore/item/WriterAbstract.java b/src/com/massivecraft/massivecore/item/WriterAbstract.java index 39a1f4b5..b7d1a692 100644 --- a/src/com/massivecraft/massivecore/item/WriterAbstract.java +++ b/src/com/massivecraft/massivecore/item/WriterAbstract.java @@ -6,7 +6,7 @@ import java.util.List; import com.massivecraft.massivecore.Engine; import com.massivecraft.massivecore.MassiveCoreMConf; import com.massivecraft.massivecore.collections.MassiveList; -import com.massivecraft.massivecore.command.type.primitive.TypeBoolean; +import com.massivecraft.massivecore.command.type.primitive.TypeBooleanOn; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.ReflectionUtil; import com.massivecraft.massivecore.util.Txt; @@ -114,7 +114,7 @@ public abstract class WriterAbstract extends Engine String message; // Main - message = Txt.parse("%s %s", name, TypeBoolean.getOn().getVisual(success)); + message = Txt.parse("%s %s", name, TypeBooleanOn.get().getVisual(success)); messages.add(message); // Throwable diff --git a/src/com/massivecraft/massivecore/util/ReflectionUtil.java b/src/com/massivecraft/massivecore/util/ReflectionUtil.java index e34fd944..cfc5df8f 100644 --- a/src/com/massivecraft/massivecore/util/ReflectionUtil.java +++ b/src/com/massivecraft/massivecore/util/ReflectionUtil.java @@ -1,5 +1,6 @@ package com.massivecraft.massivecore.util; +import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -240,6 +241,27 @@ public class ReflectionUtil return fallback; } + // -------------------------------------------- // + // ANNOTATION + // -------------------------------------------- // + + public static T getAnnotation(Field field, Class annotationClass) + { + // Fail Fast + if (field == null) throw new NullPointerException("field"); + if (annotationClass == null) throw new NullPointerException("annotationClass"); + + try + { + return field.getAnnotation(annotationClass); + } + catch (Throwable t) + { + t.printStackTrace(); + return null; + } + } + // -------------------------------------------- // // FIELD > GET // -------------------------------------------- //