-
-
Notifications
You must be signed in to change notification settings - Fork 1
Create Commands
Eric Lam edited this page Jun 13, 2022
·
5 revisions
Create A single Command is simple:
create a command:
@Commander(
name = "test",
description = "test command",
alias = {"tes", "te"}
)
public class TestCommand implements CommandNode {
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage("test!");
}
}
and register:
public class TesterRegistry implements ComponentsRegistry {
@Override
public void registerCommand(CommandRegistry<CommandSender> commandRegistry) {
commandRegistry.command(TestCommand.class);
}
@Override
public void registerListeners(ListenerRegistry<Listener> listenerRegistry) {
}
}
root command
@Commander(
name = "test",
description = "test command",
alias = {"tes", "te"}
)
public class TestCommand implements CommandNode {
@Override
public void execute(CommandSender commandSender) {
}
}
subcommand 1
@Commander(
name = "one",
description = "test one command"
)
public class TestOneCommand implements CommandNode {
// here is the command arguments, which we will talk later
@CommandArg(order = 0, labels = {"string"})
private String value;
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage("this is one command with value "+value);
}
}
subcommand 2
@Commander(
name = "two",
description = "two command"
)
public class TestTwoCommand implements CommandNode {
@CommandArg(order = 0)
private int number;
@Override
public void execute(CommandSender commandSender) {
commandSender.sendMessage("this is two command with number "+number);
}
}
and define their relationship:
public class TesterRegistry implements ComponentsRegistry {
@Override
public void registerCommand(CommandRegistry<CommandSender> commandRegistry) {
commandRegistry.command(TestCommand.class, c -> {
c.command(TestOneCommand.class);
c.command(TestTwoCommand.class);
});
}
@Override
public void registerListeners(ListenerRegistry<Listener> listenerRegistry) {
}
}
After that, the command will be registered as below:
/test one <string>
/test two <number>
The Commander Annotation Reference:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Commander {
String name();
String description();
boolean playerOnly() default false; // still need to cast yourself
String permission() default "";
String[] alias() default {};
}
Here are the calculation commands
@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));
}
}
the usage is shown below:
/calculate add <one> <two>
/calculate minus <one> <two>
The command API has parsed your string argument into the type that you have declared from @CommandArg
automatically.
Now, let's do some changes for those two @CommandArg
@CommandArg(order = 0, labels = {"first value"})
private int one;
@CommandArg(order = 1, labels = {"second value"}, optional = true)
private int two = 22;
After that, the usage will become
/calculate add <first value> [second value]
/calculate minus <first value> [second value]
What's changed?
- after setting the label, the usage display label changed.
- the second argument becomes optional, if you don't input it, it will use the default value 22
Getting remain args is simple:
@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());
}
}
so when you execute /calculate add 1 1 a b c d e
the remaining args will be [a, b, c, d, e]