Command Tutorial

MCCore v1.16 and beyond

While you can still use the command API for versions 1.15 and earlier, the new one added in v1.16 allows you to make much more flexible and configurable command setups while still being simple. First, you should make a function for your command like this:

// You need to implement the interface IFunction
public class MyFunction implements IFunction {

    // Executes your function when it's called
    public void execute(ConfigurableCommand command, Plugin plugin, CommandSender sender, String[] args) {

        // This is just an example function, nothing special
        if (sender instanceof Player) {
            sender.sendMessage("Hi!");
        }

        // This is how you can display the command usage to something
        // The '1' is just the page to display
        else command.dispayHelp(sender, 1);
    }
}

Once you have the functions you want, it's time to register them! You will be doing this using the ConfigurableCommand class. There are a few ways you can do this.

Making your function a root command

If you want your command to be a root command (e.g. /command [args]), then you can simply create a ConfigurableCommand with it and register it like this:

ConfigurableCommand myCommand = new ConfigurableCommand(
    plugin, // Your plugin reference
    "command", // The name of your command
    SenderType.ANYONE,  // Who can use the command
    new MyFunction() // The function of the command
); 
CommandManager.registerCommand(myCommand);
Making your function a sub command

If you want your command to be a sub command (e.g. /root subcommand [args]), then you will add it to the root command like this:

ConfigurableCommand myRoot = new ConfigurableCommand(plugin, "root", SenderType.ANYONE);
ConfigurableCommand myCommand = new ConfigurableCommand(
    plugin, 
    "command", 
    SenderType.ANYONE, 
    new MyFunction(),
    "A sample command", // A description for the command usage
    "<arg1> [arg2]", // Arguments for the command usage
    "my.permission" // The required permission for the command
);
myRoot.addSubCommand(myCommand);
CommandManager.registercommand(myRoot);

This will make your command be "/root command" where typing only "/root" will display the usage of "/root command". You can continue adding sub commands to other commands to make your command structure as many levels deep as you want (e.g. you can have /root sub1 sub2 sub3 [args]) by adding sub1 to root, sub2 to sub1, and sub3 to sub2).

Making multiple sub commands at once

You may want to set up many sub commands for one root all at once. You can do this using the addSubCommands method in ConfigurableCommand. An example is this:

ConfigurableCommand myRoot = new ConfigurableCommand(plugin, "root", SenderType.ANYONE);
myRoot.addSubCommands( 
    new ConfigurableCommand(
        plugin, 
        "one", 
        SenderType.ANYONE, 
        new MyFunction(), 
        "A sample command"
    ),
    new ConfigurableCommand(
        plugin, 
        "two", 
        SenderType.ANYONE, 
        new OtherFunction(), 
        "Another command"
    )
);
CommandManager.registercommand(myRoot);

Comments

Posts Quoted:
Reply
Clear All Quotes