Skip to content

Commands System

Traqueur edited this page Jul 4, 2025 · 6 revisions

📘 CommandsAPI – Command Registration & Usage Guide

This guide explains how to register and configure commands using CommandsAPI v4.


✅ Basic Command Registration

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));
    }
}

🧭 Platform-Specific Command and CommandManager

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> and CommandManager<T>
  • Already define the correct sender type (S)
  • Are located in the same package

✅ Why use them?

Using the platform-specific classes:

  • Simplifies code (no need to specify sender type)
  • Ensures type safety
  • Provides platform-specific behavior automatically

📦 Example (Spigot)

import fr.traqueur.platform.spigot.command.Command;
import fr.traqueur.platform.spigot.command.CommandManager;

This is equivalent to:

Command<MyPlugin, CommandSender>
CommandManager<MyPlugin, CommandSender>

🔧 Command Configuration Methods

setUsage(String usage)

Sets the usage string. If not set, a dynamic usage is generated.

setUsage("/hello <name>");

setPermission(String permission)

Defines the required permission to run the command.

setPermission("plugin.hello.use");

addAlias(String... aliases)

Adds alternative labels for the command.

addAlias("hi", "hey");

setGameOnly(boolean gameOnly)

Restricts the command to in-game players.

setGameOnly(true);

setDescription(String description)

Sets a help description for the command.

setDescription("Sends a greeting.");

📂 Subcommands

🔁 Classic Subcommand Setup

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.");
    }
}

☑️ Dotted Notation (hello.sub)

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));

🛡 Permission & Alias Behavior in Subcommands

  • Subcommands enforce their own permission.
  • If the main command has aliases, subcommands inherit those:
/myalias sub

🗑️ Unregistering Commands

You can unregister by name or command instance:

From CommandManager:

commandManager.unregisterCommand("hello");
commandManager.unregisterCommand("hello", true); // includes subcommands

From the Command instance:

helloCommand.unregister();
helloCommand.unregister(true);

✅ Removes all aliases
✅ Supports full unregistration


🔄 Registering Multiple Commands

commandManager.registerCommand(new HelloCommand(this));
commandManager.registerCommand(new SubWithDotCommand(this));

Use registerCommand for each command individually.


🧠 Advanced: Generic Core Classes

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!