Skip to content

Adding arguments

You can add arguments just by adding parameters to your execute methods.

For example, command /tellmini <players> <message> could look like this (Player API):

@Executes
void tellMini(CommandSender sender, List<Player> players, @StringArg(GREEDY) String message) {
Component component = MiniMessage.miniMessage().deserialize(message);
players.forEach(p -> p.sendMessage(component));
}

On the Brigadier side this adds two new arguments: ArgumentTypes.players() and StringArgumentType.greedy(). The argument values are automatically resolved by StrokkCommands.

TellMiniCommandBrigadier.java
// StrokkCommands generates all of this from just the parameters!
Commands.literal("tellmini")
.then(Commands.argument("players", ArgumentTypes.players())
.then(Commands.argument("message", StringArgumentType.greedyString())
.executes(ctx -> {
instance.tellMini(
ctx.getSource().getSender(),
ctx.getArgument("players", PlayerSelectorArgumentResolver.class).resolve(ctx.getSource()),
StringArgumentType.getString(ctx, "message")
);
return Command.SINGLE_SUCCESS;
})
)
);

To declare literals, you can use the @Literal annotation. You can pass a list of strings for multiple-choice literals. For example, a command /tellpreset <player> <first|second|last> could be declared as follows:

TellPresetCommand.java
@Executes
void executeTellPreset(
Player player,
@Literal({"first", "second", "last"}) String preset
) {
final String message = switch (preset) {
case "first" -> "You selected the first choice!";
case "second" -> "This is the second one...";
case "last" -> "...and this is the last one.";
// This will never happen
default -> throw new IllegalStateException("Illegal literal.");
};
// Do something with the message
player.sendPlainMessage(message);
}

This will generate the following Brigadier command tree:

Warning: Brigadier jumpscare
TellPresetCommandBrigadier.java (Paper module)
final TellPresetCommand instance = new TellPresetCommand();
return Commands.literal("tellpreset")
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.literal("second")
.executes(ctx -> {
instance.executeTellPreset(
ctx.getSource().getSender(),
ctx.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(ctx.getSource()).getFirst(),
"second"
);
return Command.SINGLE_SUCCESS;
})
)
.then(Commands.literal("first")
.executes(ctx -> {
instance.executeTellPreset(
ctx.getSource().getSender(),
ctx.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(ctx.getSource()).getFirst(),
"first"
);
return Command.SINGLE_SUCCESS;
})
)
.then(Commands.literal("last")
.executes(ctx -> {
instance.executeTellPreset(
ctx.getSource().getSender(),
ctx.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(ctx.getSource()).getFirst(),
"last"
);
return Command.SINGLE_SUCCESS;
})
)
)
.build();

(( You can find general information on primitive arguments on Paper’s documentation page ))

The following primitive types can be added as parameters:

  • boolean, Boolean
  • int, Integer
  • long, Long
  • float, Float
  • double, Double

The number ones always have an extra @<Number>Arg(min = <value>, max = <value>) annotation you can add to optionally specify a max or min value:

FlySpeedCommand.java
/// The speed argument is limited to be 1 or 10 or in-between.
@Executes
void execute(@IntArg(min = 1, max = 10) int speed) {
// ...
}

(( You can find general information on String arguments on Paper’s documentation page ))

When you add a String parameter, StrokkCommands defaults to adding a word string argument type. You can change the type of the String argument by adding a @StringArg() annotation and specifying a StringArgType inside. The following types exist:

  • StringArgType.WORD - The default type. Only supports alphanumerical characters and +, -, _, and ..
  • StringArgType.STRING - Similar to the WORD one, but if you add quotes, you can enter any unicode characters.
  • StringargType.GREEDY - Accepts all inputs, but cannot have any arguments follow it.

You can statically import these types for better readability in source code.