Skip to content

Add a subcommand to a main command

PXAV edited this page Jun 5, 2020 · 1 revision

In this tutorial, you will learn how to avoid extremely long command classes handling multiple subcommands and other nested commands and parameters by using Kelp-subcommands.

It is recommended to read the previous tutorial on how to create a basic command to have a better understanding of this tutorial.

The @CreateSubCommand annotation

For every subcommand, you have to create a new class with the idea to split your command into multiple classes in order to avoid cluttered classes. This new class also has to have an annotation:

@CreateSubCommand(name = "test1", executorType = ExecutorType.PLAYER_ONLY, parentCommand = YourCommandClass.class)
public class YourSubCommandClass {

}

The parameters name and executorType both act the same as in the normal command annotation, where name only is the name of the subcommand (in this case the entire command would be /testcommand test1). The only difference is the parentCommand parameter. As the name indicates, you have to pass the class of the parent command class here by typing the name of the class and adding .class at the end.

The KelpCommand class

As in a normal command, your class has to inherit from KelpCommand and include the correct onCommand method. Your command class should now look like this:

@CreateSubCommand(name = "test1", executorType = ExecutorType.PLAYER_ONLY, parentCommand = YourCommandClass.class)
public class YourSubCommandClass extends KelpCommand {

  @Override
  public void onCommand(KelpPlayer player, String[] args) {
    player.sendActionbar("You have executed the subcommand test1 of testcommand.");
  }
}

For sub-commands KelpCommand contains additional methods to allow/forbid custom parameters or administrate arguments. To learn more about these features look at the detailed documentation for the KelpCommand class.

Clone this wiki locally