-
Notifications
You must be signed in to change notification settings - Fork 2
Arguments Usage
You can add arguments to your commands using the methods addArgs and addOptionalArgs. The addArgs method is for required arguments, while the addOptionalArgs method is for optional arguments. Each argument must have a name and a type, and the framework will automatically convert the argument to the specified type using the syntax name:type.
public class GreetCommand extends Command<JavaPlugin> {
public GreetCommand(JavaPlugin plugin) {
super(plugin, "greet");
setDescription("A greeting command");
setUsage("/greet <name>");
addArgs("name:string");
}
@Override
public void execute(CommandSender sender, Arguments args) {
String name = args.get("name");
sender.sendMessage("Hello, " + name + "!");
}
}Here are all the available types you can use for arguments:
| Type | Identifier | Description |
|---|---|---|
String |
string |
Accepts any string without conversion. |
Integer |
int |
Converts input to an integer. |
Double |
double |
Converts input to a double. |
Long |
long |
Converts input to a long. |
Player |
player |
Converts input to a Player object if the player is online. |
OfflinePlayer |
offlineplayer |
Converts input to an OfflinePlayer object, allowing access to players not currently online. |
String |
infinite |
Accepts an infinite amount of strings as a single argument. |
You can create custom argument types by implementing the ArgumentConverter class and overriding the apply method. Then, register the custom argument type with the CommandManager instance.
public class CustomArgument implements ArgumentConverter<CustomType> {
@Override
public CustomType apply(String input) {
return /*custom conversion logic*/;
}
}public class MyPlugin extends JavaPlugin {
private CommandManager commandManager;
@Override
public void onEnable() {
commandManager = new CommandManager(this);
commandManager.registerArgumentType(CustomType.class, "custom", new CustomArgument());
}
}You can add auto-completion for commands and their arguments by implementing the TabConverter interface in your custom ArgumentConverter and overriding the onCompletion method. Additionally, you can specify auto-completion for specific arguments by using the methods addArgs(String name, TabConverter converter) or addOptionalArgs(String name, TabConverter converter) when you register your arguments.
Completion work with permissions, if the sender doesn't have the permission of a command, he doesn't see the completion of the command.
public class CustomArgument implements ArgumentConverter<CustomType>, TabConverter {
@Override
public CustomType apply(String input) {
return /*custom conversion logic*/;
}
@Override
public List<String> onCompletion(CommandSender sender) {
return /*list of completion suggestions*/;
}
}public class GreetCommand extends Command<JavaPlugin> {
public GreetCommand(JavaPlugin plugin) {
super(plugin, "greet");
setDescription("A greeting command");
setUsage("/greet <name>");
addArgs("name:custom");
addArgs("number:int", (sender) -> Arrays.asList("1", "2", "3"));
}
@Override
public void execute(CommandSender sender, Arguments args) {
CustomType name = args.get("name");
int number = args.get("number");
for (int i = 0; i < number; i++) {
sender.sendMessage("Hello, " + name + "!");
}
}
}