ZetaPlugins logo

Command Registration

Learn how to use the AutoCommandRegistrar to register commands automatically in your ZetaCore plugin.

For command registration, ZetaCore offers the AutoCommandRegistrar class which makes it easy to register commands by simply annotating your command classes with the @AutoRegisterCommand annotation.

Here is an example of how to use the AutoCommandRegistrar to register commands automatically:

import com.zetaplugins.zetacore.ZetaCorePlugin;
import com.zetaplugins.zetacore.services.commands.AutoCommandRegistrar;

public final class MyPlugin extends ZetaCorePlugin {
    private static final String PACKAGE_PREFIX = "com.example.myplugin";// Replace with your actual package prefix

    @Override
    public void onEnable() {
        // ... other initialization code ...
        var cmdRegistrar = new AutoCommandRegistrar.Builder()
                .setPlugin(this)
                .setPackagePrefix(PACKAGE_PREFIX)
                //.setManagerRegistry(managerRegistry) // Optional: if you have a manager registry. This will be covered in a later tutorial.
                .build();
        var commands = cmdRegistrar.registerAllCommands();
        getLogger().info("Registered commands: " + String.join(", ", commands));
        // ... other initialization code ...
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}

In this example, we create an instance of AutoCommandRegistrar in the onEnable method of our plugin. We specify the plugin instance and the package prefix where our command classes are located. The registerAllCommands method scans the specified package for classes annotated with @AutoRegisterCommand and registers them automatically.

To use the @AutoRegisterCommand annotation, simply annotate your command classes like this:

import com.zetaplugins.zetacore.commands.PluginCommand;
import com.zetaplugins.zetacore.annotations.AutoRegisterCommand;

@AutoRegisterCommand(
    commands = "greet",
    description = "Greets a player",
    usage = "/greet <player>",
    permission = "myplugin.command.greet",
    aliases = {"hello"}
)
public class GreetCommand extends PluginCommand<MyPlugin> {
    public GreetCommand(MyPlugin plugin) {
        super(plugin);
    }

    @Override
    public boolean execute(CommandSender sender, Command command, String label, ArgumentList args) throws CommandException {
        // Command execution logic
        return true;
    }
}

In this example, the GreetCommand class is annotated with @AutoRegisterCommand, specifying the command name, description, usage, permission, and aliases. When the AutoCommandRegistrar scans the package, it will find this class and register the command automatically.

Make sure to check the API documentation for more details on how to use the AutoCommandRegistrar and the @AutoRegisterCommand annotation.

When using the AutoCommandRegistrar, you don't need to manually register your commands in the plugin.yml file. The registrar takes care of that for you.