Search in sources :

Example 11 with MassiveException

use of com.massivecraft.massivecore.MassiveException 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 12 with MassiveException

use of com.massivecraft.massivecore.MassiveException in project MassiveCore by MassiveCraft.

the class TypeNameAbstract method read.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String read(String arg, CommandSender sender) throws MassiveException {
    if (arg == null)
        throw new NullPointerException("arg");
    // Allow changing capitalization of the current name if lenient.
    String current = this.getCurrentName(sender);
    if (current != null && current.equalsIgnoreCase(arg) && this.isLenient())
        return arg;
    if (this.isNameTaken(arg))
        throw new MassiveException().addMsg("<b>The name \"<h>%s<b>\" is already in use.", arg);
    Integer lengthMin = this.getLengthMin();
    if (lengthMin != null && arg.length() < lengthMin) {
        throw new MassiveException().addMsg("<b>The name must be at least <h>%d<b> characters.", lengthMin);
    }
    Integer lengthMax = this.getLengthMax();
    if (lengthMax != null && arg.length() > lengthMax) {
        throw new MassiveException().addMsg("<b>The name must be at most <h>%d<b> characters.", lengthMax);
    }
    Set<Character> disallowed = new MassiveSet<>();
    for (char character : arg.toCharArray()) {
        if (!this.isCharacterAllowed(character))
            disallowed.add(character);
    }
    // We found some disallowed characters
    if (!disallowed.isEmpty()) {
        String characterViolations = Txt.implode(disallowed, "");
        String pluralityResolution = disallowed.size() == 1 ? " is" : "s are";
        throw new MassiveException().addMsg("<b>The following character%s not allowed: <h>%s<b>.", pluralityResolution, characterViolations);
    }
    return arg;
}
Also used : MassiveException(com.massivecraft.massivecore.MassiveException) MassiveSet(com.massivecraft.massivecore.collections.MassiveSet)

Example 13 with MassiveException

use of com.massivecraft.massivecore.MassiveException in project MassiveCore by MassiveCraft.

the class TypeStringCommand method read.

@Override
public String read(String arg, CommandSender sender) throws MassiveException {
    // Require base command (something at all).
    if (arg.isEmpty())
        throw new MassiveException().addMsg("<b>You must at the very least supply a base command.");
    String[] args = argAsArgs(arg);
    // Smart management of first slash ...
    String alias = args[0];
    // ... if there is such a command just return ...
    Command command = getCommand(alias);
    if (command != null)
        return arg;
    // ... otherwise if starting with slash return it and hope for the best ...
    if (alias.startsWith("/"))
        return arg.substring(1);
    // ... otherwise it's slashless and we return it as is.
    return arg;
}
Also used : Command(org.bukkit.command.Command) MassiveException(com.massivecraft.massivecore.MassiveException)

Example 14 with MassiveException

use of com.massivecraft.massivecore.MassiveException in project MassiveCore by MassiveCraft.

the class TypeAbstractSelect method createExceptionForInvalidArg.

public MassiveException createExceptionForInvalidArg(String arg, CommandSender sender) {
    MassiveException ret = new MassiveException();
    ret.addMsg("<b>No %s matches \"<h>%s<b>\".", this.getName(), arg);
    if (this.canList(sender)) {
        Collection<String> names = this.altNames(sender);
        // Try Levenshtein
        List<String> matches = this.getMatchingAltNames(arg, sender, this.getMaxLevenshteinDistanceForArg(arg));
        if (names.isEmpty()) {
            ret.addMsg("<i>Note: There is no %s available.", this.getName());
        } else if (!matches.isEmpty() && matches.size() <= LIST_COUNT_MAX) {
            String format = Txt.parse("<h>%s");
            String comma = Txt.parse("<i>, ");
            String and = Txt.parse(" <i>or ");
            String dot = Txt.parse("<i>?");
            ret.addMsg("<i>Did you mean %s", Txt.implodeCommaAndDot(matches, format, comma, and, dot));
        } else if (names.size() > LIST_COUNT_MAX) {
            ret.addMsg("<i>More than %d alternatives available.", LIST_COUNT_MAX);
        } else {
            String format = Txt.parse("<h>%s");
            String comma = Txt.parse("<i>, ");
            String and = Txt.parse(" <i>or ");
            String dot = Txt.parse("<i>.");
            ret.addMsg("<i>Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot));
        }
    }
    return ret;
}
Also used : MassiveException(com.massivecraft.massivecore.MassiveException)

Example 15 with MassiveException

use of com.massivecraft.massivecore.MassiveException in project MassiveCore by MassiveCraft.

the class TypeDestination method read.

@Override
public Destination read(String arg, CommandSender sender) throws MassiveException {
    EventMassiveCoreDestination event = new EventMassiveCoreDestination(arg, sender, null);
    event.run();
    MassiveException exception = event.getException();
    if (exception != null)
        throw exception;
    Destination ret = event.getDestination();
    if (ret == null)
        throw new MassiveException().addMsg("<b>Unknown destination \"<h>%s<b>\".", arg);
    // Throw exeption if ps is null.
    ret.getPs(sender);
    return ret;
}
Also used : EventMassiveCoreDestination(com.massivecraft.massivecore.event.EventMassiveCoreDestination) Destination(com.massivecraft.massivecore.teleport.Destination) MassiveException(com.massivecraft.massivecore.MassiveException) EventMassiveCoreDestination(com.massivecraft.massivecore.event.EventMassiveCoreDestination)

Aggregations

MassiveException (com.massivecraft.massivecore.MassiveException)18 Player (org.bukkit.entity.Player)4 EventMassiveCoreDestination (com.massivecraft.massivecore.event.EventMassiveCoreDestination)3 Destination (com.massivecraft.massivecore.teleport.Destination)3 Mson (com.massivecraft.massivecore.mson.Mson)2 PS (com.massivecraft.massivecore.ps.PS)2 EventHandler (org.bukkit.event.EventHandler)2 MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 MassiveSet (com.massivecraft.massivecore.collections.MassiveSet)1 RequirementIsPlayer (com.massivecraft.massivecore.command.requirement.RequirementIsPlayer)1 RegistryType (com.massivecraft.massivecore.command.type.RegistryType)1 Type (com.massivecraft.massivecore.command.type.Type)1 TypePS (com.massivecraft.massivecore.command.type.TypePS)1 TypeDyeColor (com.massivecraft.massivecore.command.type.enumeration.TypeDyeColor)1 TypeString (com.massivecraft.massivecore.command.type.primitive.TypeString)1 PSBuilder (com.massivecraft.massivecore.ps.PSBuilder)1 DestinationJump (com.massivecraft.massivecore.teleport.DestinationJump)1 DestinationPlayer (com.massivecraft.massivecore.teleport.DestinationPlayer)1 DestinationSimple (com.massivecraft.massivecore.teleport.DestinationSimple)1 DestinationThat (com.massivecraft.massivecore.teleport.DestinationThat)1