Skip to content

Arguments

GabRay edited this page Feb 18, 2023 · 6 revisions

Defining an argument

Before adding an argument to a command, you need to declare the argument type.

// Declare a new argument of type `Integer`.
class IntegerArgument extends CommandArgument<Integer> {

    public IntegerArgument(@NotNull final String name) { super(name); }

    @Override
    public void define(final CommandArgumentBuilderSteps.Parser<Integer> builder)
    {
        builder.parser((input, env) -> {
                   return Integer.parseInt(input);
               })
               .tabCompleter((input, env) -> List.of("1", "2", "3"))
               .errorHandler(NumberFormatException.class, "Invalid number");
    }
}

Adding the argument to the command

On classes

@Command(args = {
    @Argument(label = "amount", clazz = IntegerArgument.class)
}
class MoneyCommand extends CommandContext {}

Adding an argument on a class with sub commands will require the player to provide the argument for every subcommands.
For example : /myCommand argName subCommand1, /myCommand argName subCommand2, ...

On sub commands

@Command
class MoneyCommand extends CommandContext {
    @Command(name = "give")
    void giveCommand(@Argument("amount") IntegerArgument arg) {
        int amountToGive = arg.get();
        // ...
    }

    // OR

    @Command(name = "give")
    void giveCommand(@Argument(value = "amount", clazz = IntegerArgument.class) int amount) {
        // ...
    }
}

Adding an argument on a sub command will require the player to provide the argument for this sub command only.
For example : /myCommand subCommand1 argName. If the player do not provide enough arguments, the help message will be shown with a list of commands and their arguments.

Optional arguments

Arguments can be optional. If an optional argument is not given by the player, then the function argument will be null.

@Command(name = "give")
void giveCommand(@Argument(value = "amount", optional = true) @Nullable IntegerArgument argument) {
    if (argument != null) {
        // ...
    }
}

Optional arguments can also have default values.

@Command(name = "give")
void giveCommand(@Argument(value = "amount", optional = true, def = "1") @NotNull IntegerArgument argument) {
    int amountToGive = argument.get();
}

Depending on other arguments

The command environment is passed to tab completers and parsers/suppliers. Therefore, it is possible to make the parsing of an argument depend on a previous argument

class CityArgument extends CommandArgument<City> {

    public CityArgument(@NotNull final String name) { super(name); }

    @Override
    public void define(final CommandArgumentBuilderSteps.Parser< City > builder)
    {
        builder.parser((input, env) -> {
                   Country country = env.get("country", Country.class);
                   return country.getCity(input);
               })
               .tabCompleter((input, env) -> {
                   Country country = env.get("country", Country.class);
                   return country.getCities().stream().map(City::getName).toList();
               });
    }
}

@Command
class MoneyCommand extends CommandContext {
    @Command
    public void giveCommand(@Argument("country") CountryArgument countryArg,
                            @Argument("city") CityArgument cityArg)
    {
    }
}

Clone this wiki locally