Search in sources :

Example 1 with RequiredArgument

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;
}
Also used : RequiredArgument(net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument)

Example 2 with RequiredArgument

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);
}
Also used : WrappedException(net.anweisen.utilities.common.collection.WrappedException) ParserOptions(net.anweisen.utilities.jda.manager.arguments.ParserOptions) RequiredArgument(net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument) WrappedException(net.anweisen.utilities.common.collection.WrappedException) IllegalArgumentParserValueException(net.anweisen.utilities.jda.manager.arguments.IllegalArgumentParserValueException) IllegalArgumentParserValueException(net.anweisen.utilities.jda.manager.arguments.IllegalArgumentParserValueException) Tuple(net.anweisen.utilities.common.collection.pair.Tuple) Nonnull(javax.annotation.Nonnull)

Example 3 with RequiredArgument

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();
}
Also used : RequiredArgument(net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument) RegisteredCommand(net.anweisen.utilities.jda.manager.hooks.registered.RegisteredCommand) Nonnull(javax.annotation.Nonnull)

Aggregations

RequiredArgument (net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument)3 Nonnull (javax.annotation.Nonnull)2 WrappedException (net.anweisen.utilities.common.collection.WrappedException)1 Tuple (net.anweisen.utilities.common.collection.pair.Tuple)1 IllegalArgumentParserValueException (net.anweisen.utilities.jda.manager.arguments.IllegalArgumentParserValueException)1 ParserOptions (net.anweisen.utilities.jda.manager.arguments.ParserOptions)1 RegisteredCommand (net.anweisen.utilities.jda.manager.hooks.registered.RegisteredCommand)1