-
Notifications
You must be signed in to change notification settings - Fork 2
Commands System
This guide explains how to register and configure commands using CommandsAPI v4.
import fr.traqueur.platform.spigot.command.Command;
import fr.traqueur.platform.spigot.command.CommandManager;
public class HelloCommand extends Command<MyPlugin> {
public HelloCommand(MyPlugin plugin) {
super(plugin, "hello");
}
@Override
public void execute(CommandSender sender, Arguments args) {
sender.sendMessage("Hello World!");
}
}Register the command in your plugin:
public class MyPlugin extends JavaPlugin {
private CommandManager<MyPlugin> commandManager;
@Override
public void onEnable() {
commandManager = new CommandManager<>(this);
commandManager.registerCommand(new HelloCommand(this));
}
}Each platform module (e.g., platform-spigot, platform-velocity, etc.) provides its own version of the Command and CommandManager classes, located in a platform-specific package.
These platform versions:
- Keep the class names
Command<T>andCommandManager<T> - Already define the correct sender type (
S) - Are located in the same package
Using the platform-specific classes:
- Simplifies code (no need to specify sender type)
- Ensures type safety
- Provides platform-specific behavior automatically
import fr.traqueur.platform.spigot.command.Command;
import fr.traqueur.platform.spigot.command.CommandManager;This is equivalent to:
Command<MyPlugin, CommandSender>
CommandManager<MyPlugin, CommandSender>Sets the usage string. If not set, a dynamic usage is generated.
setUsage("/hello <name>");Defines the required permission to run the command.
setPermission("plugin.hello.use");Adds alternative labels for the command.
addAlias("hi", "hey");Restricts the command to in-game players.
setGameOnly(true);Sets a help description for the command.
setDescription("Sends a greeting.");public class SubCommand extends Command<MyPlugin> {
public SubCommand(MyPlugin plugin) {
super(plugin, "sub");
}
@Override
public void execute(CommandSender sender, Arguments args) {
sender.sendMessage("Subcommand executed!");
}
}
public class HelloCommand extends Command<MyPlugin> {
public HelloCommand(MyPlugin plugin) {
super(plugin, "hello");
addSubcommand(new SubCommand(plugin));
}
@Override
public void execute(CommandSender sender, Arguments args) {
sender.sendMessage("Main command executed.");
}
}public class SubWithDotCommand extends Command<MyPlugin> {
public SubWithDotCommand(MyPlugin plugin) {
super(plugin, "hello.sub");
setDescription("A subcommand");
setUsage("/hello sub");
}
@Override
public void execute(CommandSender sender, Arguments args) {
sender.sendMessage("This is a subcommand!");
}
}Then register it:
commandManager.registerCommand(new SubWithDotCommand(this));- Subcommands enforce their own permission.
- If the main command has aliases, subcommands inherit those:
/myalias subYou can unregister by name or command instance:
commandManager.unregisterCommand("hello");
commandManager.unregisterCommand("hello", true); // includes subcommandshelloCommand.unregister();
helloCommand.unregister(true);✅ Removes all aliases
✅ Supports full unregistration
commandManager.registerCommand(new HelloCommand(this));
commandManager.registerCommand(new SubWithDotCommand(this));Use registerCommand for each command individually.
If you're building a new adapter or working cross-platform, you may use the generic classes from the core:
import fr.traqueur.commandsapi.core.command.Command;
import fr.traqueur.commandsapi.core.manager.CommandManager;
CommandManager<MyPlugin, CommandSender> manager = new CommandManager<>(...);Otherwise, prefer platform-specific classes for simplicity.
Happy coding with CommandsAPI!