use of net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument in project Utility by anweisen.
the class DefaultCommandManager method isArgumentLengthAssignable.
private boolean isArgumentLengthAssignable(@Nonnull String input, @Nonnull RequiredArgument[] arguments) {
if (input.isEmpty())
return arguments.length == 0;
if (arguments.length == 0)
return false;
String[] split = input.split(" ");
int index = 0;
for (RequiredArgument argument : arguments) {
// We want all following arguments
if (argument.getLength() == 0 && split.length > (index == 0 ? 1 : index))
return true;
index += argument.getLength();
if (index > split.length)
return false;
}
return true;
}
use of net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument in project Utility by anweisen.
the class DefaultCommandManager method parseArguments.
@Nonnull
private ArgumentParseResult parseArguments(@Nonnull RegisteredCommand command, @Nonnull String message, @Nonnull CommandEvent event) {
if (message.isEmpty())
return new ArgumentParseResult(new Tuple<>(new Class<?>[0], new Object[0]), null);
List<Class<?>> types = new ArrayList<>(command.getArguments().length);
List<Object> values = new ArrayList<>(command.getArguments().length);
String[] args = message.split(" ");
int index = 0;
for (RequiredArgument argument : command.getArguments()) {
int collectArgs = argument.getLength() == 0 ? args.length - index : argument.getLength();
ArgumentParser<?, ?> parser = argument.getParser();
ParserOptions options = parser.options();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < collectArgs; i++, index++) {
if (index >= args.length)
throw new IllegalArgumentException("Tried to access argument at index " + index + " for length " + args.length);
if (i > 0)
builder.append(options.getMultiWordSeparator());
builder.append(args[index]);
}
Object parsed = null;
try {
parsed = parser.parseCasted(event, argument.getInfoContainer(), builder.toString());
if (!parser.options().isNullable() && parsed == null)
throw new NullPointerException("No value for input '" + builder + "'");
if (parsed != null) {
if (!argument.getClassOfArgument().isInstance(parsed))
throw new IllegalArgumentParserValueException("Parser for class " + argument.getClassOfArgument().getName() + " returned " + parsed.getClass().getName());
if (argument.getInfoContainer() != null && !parser.validateInfoContainerCasted(argument.getInfoContainer(), parsed))
throw new IllegalArgumentException("Info container is not valid for the given input");
}
types.add(argument.getClassOfArgument());
values.add(parsed);
} catch (IllegalArgumentParserValueException ex) {
// Internal error
throw new WrappedException(ex);
} catch (Exception ex) {
Tuple<String, Object[]> errorMessage = parser.getErrorMessageCasted(argument.getInfoContainer(), parsed);
// Invalid usage
return new ArgumentParseResult(null, errorMessage);
}
}
return new ArgumentParseResult(new Tuple<>(types.toArray(new Class[0]), values.toArray()), null);
}
use of net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument in project Utility by anweisen.
the class DefaultCommandManager method findCommand.
@Nonnull
public Optional<Tuple<String, RegisteredCommand>> findCommand(@Nonnull String input, @Nonnull Collection<RegisteredCommand> matchingName) {
for (RegisteredCommand command : commands) {
for (String name : command.getOptions().getName()) {
boolean beginsWithName = input.startsWith(name);
if (!(beginsWithName || name.startsWith(input)))
continue;
matchingName.add(command);
if (!beginsWithName)
continue;
if (input.length() > name.length() && !input.substring(name.length()).startsWith(" "))
continue;
RequiredArgument[] arguments = command.getArguments();
String argumentInput = input.substring(name.length()).trim();
if (!isArgumentLengthAssignable(argumentInput, arguments))
continue;
return Optional.of(Tuple.of(name, command));
}
}
return Optional.empty();
}
Aggregations