Search in sources :

Example 11 with MassiveList

use of com.massivecraft.massivecore.collections.MassiveList in project MassiveCore by MassiveCraft.

the class RegistryType method getInnerTypes.

// -------------------------------------------- //
// GET INNER TYPES
// -------------------------------------------- //
public static List<Type<?>> getInnerTypes(Field field, java.lang.reflect.Type fieldType, int amountRequired) {
    // Annotation
    if (field != null) {
        try {
            EditorTypeInner annotation = ReflectionUtil.getAnnotation(field, EditorTypeInner.class);
            if (annotation != null) {
                // Create
                List<Type<?>> ret = new MassiveList<>();
                // Fill
                Class<?>[] innerTypeClasses = annotation.value();
                for (Class<?> innerTypeClass : innerTypeClasses) {
                    Type<?> innerType = ReflectionUtil.getSingletonInstance(innerTypeClass);
                    ret.add(innerType);
                }
                // Return
                return ret;
            }
        } catch (Throwable t) {
        // This has to do with backwards compatibility (Usually 1.7).
        // The 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.
        }
        if (fieldType == null) {
            fieldType = field.getGenericType();
        }
    }
    // Reflection
    if (fieldType != null) {
        if (fieldType instanceof ParameterizedType) {
            // Create
            List<Type<?>> ret = new MassiveList<>();
            // Fill
            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;
        }
        throw new IllegalArgumentException("Not ParameterizedType: " + fieldType);
    }
    throw new IllegalArgumentException("Failure");
}
Also used : EditorTypeInner(com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner) MassiveList(com.massivecraft.massivecore.collections.MassiveList) ParameterizedType(java.lang.reflect.ParameterizedType) TypeFireworkEffectType(com.massivecraft.massivecore.command.type.enumeration.TypeFireworkEffectType) TypeOcelotType(com.massivecraft.massivecore.command.type.enumeration.TypeOcelotType) TypeRabbitType(com.massivecraft.massivecore.command.type.enumeration.TypeRabbitType) TypeWorldType(com.massivecraft.massivecore.command.type.enumeration.TypeWorldType) EditorType(com.massivecraft.massivecore.command.editor.annotation.EditorType) TypeSkeletonType(com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType) TypeEntityType(com.massivecraft.massivecore.command.type.enumeration.TypeEntityType) ParameterizedType(java.lang.reflect.ParameterizedType)

Example 12 with MassiveList

use of com.massivecraft.massivecore.collections.MassiveList in project MassiveCore by MassiveCraft.

the class TypeAbstractChoice method read.

@Override
public T read(String arg, CommandSender sender) throws MassiveException {
    // NPE Evade
    if (arg == null)
        return null;
    // Exact
    T exact = this.getExactMatch(arg);
    if (exact != null)
        return exact;
    // Get All
    Collection<T> all = this.getAll(sender);
    // Get Options
    Map<String, T> options = this.getOptions();
    if (options == null)
        options = this.createOptions(all);
    // Get Matches
    List<T> matches = this.getMatches(options, arg, false);
    // Exact
    if (matches.size() == 1)
        return matches.get(0);
    // Exception
    MassiveException exception = new MassiveException();
    // Suggestions
    boolean suggestNone = false;
    boolean suggestAmbiguous = false;
    boolean suggestAll = false;
    boolean suggestLevenshtein = false;
    // Nothing Found
    String message;
    if (matches.isEmpty()) {
        message = String.format(MESSAGE_MATCH_NOTHING, this.getName(), arg);
        exception.addMessage(message);
        suggestLevenshtein = true;
    } else // Ambiguous
    {
        message = String.format(MESSAGE_MATCH_AMBIGUOUS, matches.size(), this.getName(), arg);
        exception.addMessage(message);
        suggestAmbiguous = true;
    }
    // Suggest
    if (all.isEmpty())
        suggestNone = true;
    if (all.size() <= this.getListCountMax())
        suggestAll = true;
    if (!this.canList(sender)) {
    } else if (suggestNone) {
        message = String.format(MESSAGE_AVAILABLE_EMPTY, this.getName());
        exception.addMessage(message);
    } else {
        Collection<T> suggestions = null;
        Mson format = SUGGEST_FORMAT;
        Mson comma = SUGGEST_COMMMA;
        Mson and = SUGGEST_AND;
        Mson dot = SUGGEST_DOT;
        if (suggestAmbiguous) {
            suggestions = matches;
            message = MESSAGE_COLON_AMBIGUOUS;
        } else if (suggestAll) {
            suggestions = all;
            message = MESSAGE_COLON_ALL;
        } else if (suggestLevenshtein) {
            suggestions = this.getMatches(options, arg, true);
            message = MESSAGE_COLON_SIMILAR;
        }
        if (suggestions.isEmpty()) {
            exception.addMessage(MESSAGE_SUGGESTIONS_EMPTY);
        } else if (suggestions.size() > this.getListCountMax()) {
            message = String.format(MESSAGE_SUGGESTIONS_MUCH, this.getListCountMax());
            exception.addMessage(message);
        } else {
            List<Mson> visuals = new MassiveList<>();
            for (T value : suggestions) {
                visuals.add(this.getVisualMson(value, sender));
            }
            exception.addMessage(Mson.mson(message, Mson.implodeCommaAndDot(visuals, format, comma, and, dot)));
        }
    }
    // Help
    String help = this.getHelp();
    if (help != null)
        exception.addMessage(help);
    throw exception;
}
Also used : Mson(com.massivecraft.massivecore.mson.Mson) MassiveList(com.massivecraft.massivecore.collections.MassiveList) MassiveException(com.massivecraft.massivecore.MassiveException) Collection(java.util.Collection)

Example 13 with MassiveList

use of com.massivecraft.massivecore.collections.MassiveList in project MassiveCore by MassiveCraft.

the class PropertyReflection method getAll.

// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public static <O> List<PropertyReflection<O, ?>> getAll(Class<O> clazz, Type<O> typeObject) {
    List<PropertyReflection<O, ?>> ret = new MassiveList<>();
    // TODO: Something to consider coding in for the future.
    for (Field field : clazz.getDeclaredFields()) {
        if (!isVisible(field))
            continue;
        PropertyReflection<O, ?> property = get(field, typeObject);
        ret.add(property);
    }
    return ret;
}
Also used : Field(java.lang.reflect.Field) MassiveList(com.massivecraft.massivecore.collections.MassiveList)

Example 14 with MassiveList

use of com.massivecraft.massivecore.collections.MassiveList in project MassiveCore by MassiveCraft.

the class TypeCombined method getVisualMsonInner.

// -------------------------------------------- //
// WRITE VISUAL MSON
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public Mson getVisualMsonInner(T value, CommandSender sender) {
    // Create
    List<Mson> parts = new MassiveList<>();
    // Fill
    for (Entry<Type<?>, Object> entry : this.splitEntriesUser(value)) {
        Type<Object> type = (Type<Object>) entry.getKey();
        Mson part = type.getVisualMson(entry.getValue(), sender);
        if (!this.isVisualMsonNullIncluded() && part == null)
            continue;
        parts.add(part);
    }
    // Return
    return Mson.implode(parts, this.getVisualMsonSeparator());
}
Also used : Mson(com.massivecraft.massivecore.mson.Mson) MassiveList(com.massivecraft.massivecore.collections.MassiveList) Type(com.massivecraft.massivecore.command.type.Type)

Example 15 with MassiveList

use of com.massivecraft.massivecore.collections.MassiveList in project MassiveCore by MassiveCraft.

the class CommandEditAbstract method attemptSetPerform.

protected void attemptSetPerform(V after) {
    String descProperty = this.getProperty().getDisplayName();
    Mson descObject = this.getObjectVisual();
    Mson descValue = this.getInheritedVisual();
    // Create messages
    List<Mson> messages = new MassiveList<>();
    // Before
    // We inform what the value was before.
    messages.add(mson(mson("Before: ").color(ChatColor.AQUA), descValue));
    // Apply
    // We set the new property value.
    this.setValue(after);
    // After
    // We inform what the value is after.
    descValue = this.getInheritedVisual();
    messages.add(mson(mson("After: ").color(ChatColor.AQUA), descValue));
    // Startup
    // We inform what property and object the edit is taking place on.
    // The visual might change after modification, so this should be added after we have made the change.
    descObject = this.getObjectVisual();
    messages.add(0, mson(descProperty, " for ", descObject, " edited:").color(ChatColor.GRAY));
    message(messages);
}
Also used : Mson(com.massivecraft.massivecore.mson.Mson) MassiveList(com.massivecraft.massivecore.collections.MassiveList)

Aggregations

MassiveList (com.massivecraft.massivecore.collections.MassiveList)22 Mson (com.massivecraft.massivecore.mson.Mson)7 SimpleEntry (java.util.AbstractMap.SimpleEntry)4 Entry (java.util.Map.Entry)4 ItemStack (org.bukkit.inventory.ItemStack)3 ChatColor (org.bukkit.ChatColor)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 MassiveException (com.massivecraft.massivecore.MassiveException)1 EditorType (com.massivecraft.massivecore.command.editor.annotation.EditorType)1 EditorTypeInner (com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner)1 Requirement (com.massivecraft.massivecore.command.requirement.Requirement)1 Type (com.massivecraft.massivecore.command.type.Type)1 TypeEnchantment (com.massivecraft.massivecore.command.type.TypeEnchantment)1 TypeEntityType (com.massivecraft.massivecore.command.type.enumeration.TypeEntityType)1 TypeFireworkEffectType (com.massivecraft.massivecore.command.type.enumeration.TypeFireworkEffectType)1 TypeOcelotType (com.massivecraft.massivecore.command.type.enumeration.TypeOcelotType)1 TypeRabbitType (com.massivecraft.massivecore.command.type.enumeration.TypeRabbitType)1 TypeSkeletonType (com.massivecraft.massivecore.command.type.enumeration.TypeSkeletonType)1 TypeWorldType (com.massivecraft.massivecore.command.type.enumeration.TypeWorldType)1 EventMassiveCoreLorePriority (com.massivecraft.massivecore.event.EventMassiveCoreLorePriority)1