Command Arguments
Please make sure you have read the Quick Start before the continuous reading.
Below is a calculator command.
@Commander(
name = "calculate",
description = "test calculate command",
alias = {"cal", "c"}
)
public class TestCalculateCommand implements CommandNode {
@Override
public void execute(CommandSender commandSender) {
}
}
@Commander(
name = "add",
description = "calculate add command",
alias = {"ad", "plus", "a"}
)
public class TestCalculateAddCommand implements CommandNode {
@CommandArg(order = 0)
private int one;
@CommandArg(order = 1)
private int two;
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage(one+" + "+two+" = "+(one + two));
}
}
@Commander(
name = "minus",
description = "minus command",
alias = {"reduce", "m"}
)
public class TestCalculateMinusCommand implements CommandNode {
@CommandArg(order = 0)
private int one;
@CommandArg(order = 1)
private int two;
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage(one+" - "+two+" = "+(one - two));
}
}
Well you should know they are plus calculation and minus calculation commands, accept with two integer arguments. The help message will show as below:
/calculate add <one> <two>
/calculate minus <one> <two>
Now, we try edit something in @CommandArg
@CommandArg(order = 0, labels = {"first value"})
private int one;
@CommandArg(order = 1, labels = {"second value"}, optional = true)
private int two = 22;
The help message will now show as below:
/calculate add <first value> [second value]
/calculate minus <first value> [second value]
Remain Arguments
After parsing declared command arguments, the remaining arguments will be ignored. If you want to take them, just declare a variable with type List<String> and annotating with @RemainArgs
@Commander(
name = "add",
description = "calculate add command",
alias = {"ad", "plus", "a"}
)
public class TestCalculateAddCommand implements CommandNode {
@CommandArg(order = 0)
private int one;
@CommandArg(order = 1)
private int two;
@RemainArgs
private List<String> args;
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage(one+" + "+two+" = "+(one + two));
commandSender.sendMessage("remainArgs: "+args.toString());
}
}
when you input /calculate add 1 1 a b c d e
the output result will be
1 + 1 = 2
remainArgs: [a, b, c, d, e]
the property type with annotated @RemainArgs must be List<String>, or else it will throw an error.
最后更新于