New Command Handling System

New Command Handling System

TacoAPI v2.5 brings a new command handling system that makes it easier before to add commands.

Create a CommandManager

A CommandManager object is what registers every command for your plugin. You will need one in order to register new commands

public class Plugin extends JavaPlugin{
    //...
    public CommandManager commands = new CommandManager();
    //...

In a separate class, you will want to create your commands. Normally every class acts a category, and you add commands to the appropriate class. The names of the methods don't matter, as long as they take a CommandContext variable and have certain Annotations.

@SimpleCommand("simple")
public static void simple(CommandContext context) {

}

@ParentCommand("parent")
@Command(name = "test", desc = "Test this command")
public static void test(CommandContext context) {

}

The SimpleCommand annotation is used for a command that will be parsed by you as the plugin's developer. It can also be used for things like chat plugins, were the command "/help I need help" would display a message in the help channel.

The other two annotations are used to register commands similarly to how the original command system registered commands. The ParentCommand annotation is used to define which command label the sub command belongs to (e.g. /help). The Command annotation is used to define other information about the command.

  • name - The name of the subcommand. If the ParentCommand is "parent" and the "name" variable is "test" then the command /parent test would cause the method to execute.
  • aliases - A string array holding alternate values for "name". For instance, if the name is "test" but there is an alias, "t", then /parent t would also cause this method to run
  • args - A string representing the arguments to be shown in the help pages
  • onlyPlayer - A boolean indicating whether the command should only be run by a player (default true)
  • onlyConsole - A boolean indicating whether the command should only be run by console (default false)

Take a look at the CommandContext javadoc page to see how you can use the CommandContext class appropriately

Register Your Commands

In order to register your commands, you need to use the .reg() method of your CommandManager.

public CommandManager commands = new CommandManager();

public void onEnable() {
    commands.reg(Commands.class);
}

Comments

Posts Quoted:
Reply
Clear All Quotes