Skip to content

Create a basic command

PXAV edited this page Feb 4, 2021 · 2 revisions

This page will teach you how to create your first and simple command using Kelp.

The @CreateCommand annotation

For every new command, you have to create a new class, which has to be annotated with the @CreateCommand annotation like this:

@CreateCommand(name = "testcommand")
public class YourCommandClass {

}

Within the annotation, you can provide essential information about the command including:

  • name: The name of the command, which a player has to type in the chat to access it.
  • executorType: An ExecutorType enum saying who is allowed to execute the command.
    • ExecutorType.PLAYER_ONLY: Only players are allowed to execute the command. If a console tries to execute a default or custom error message will appear
    • ExecutorType.CONSOLE_ONLY: Only the console is allowed to execute the command. If any player (even with OP- or *-permissions) tries to execute it, they will receive an error message.
    • ExecutorType.PLAYER_AND_CONSOLE: Both players and consoles are allowed to execute the command. A permission check will only be executed for players. If a player executes the command, the onCommand(KelpPlayer, String[]) method will be called, otherwise onCommand(KelpConsoleSender, String[]) is called. If you enable delegatePlayerToConsole, the player method will execute the console method automatically.

The KelpCommand class

Every command class also has to inherit from the KelpCommand class, which provides essential methods.

@CreateCommand(name = "testcommand", executorType.PLAYER_ONLY)
public class YourCommandClass extends KelpCommand {

}

Tip: Your command class should always be a @Singleton so that the properties defined in onCommandRegister are saved correctly over the server runtime. More information about that later.

The most important ones are the onCommand() methods. They are called when the command is executed. Note that there are two onCommand() methods with different parameters.

  • onCommand(KelpPlayer player, String[] args): Use this method whenever you pass ExecutorType.PLAYER_ONLY in the command annotation.
  • onCommand(KelpConsoleSender sender, String[] args): Use this method for any other purpose (so CONSOLE_ONLY and PLAYER_AND_CONSOLE)

Tip: If you use an IDE generating the method for you, make sure to always remove the super() call, otherwise your command won't be executed correctly.

Inside the command method you can write whatever you want.

@CreateCommand(name = "testcommand", executorType = ExecutorType.PLAYER_ONLY)
public class YourCommandClass extends KelpCommand {

  @Override
  public void onCommand(KelpPlayer player, String[] args) {
    player.sendActionbar("You have executed a test command");
  }

}

The args parameter acts exactly like the arguments in normal spigot/bukkit commands. But if you have complex command-structures and many subcommands, it is recommended to use separate subcommand classes.

Clone this wiki locally