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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations