-
Notifications
You must be signed in to change notification settings - Fork 8
Create a basic command
This page will teach you how to create your first and simple command using Kelp.
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
: AnExecutorType
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, theonCommand(KelpPlayer, String[])
method will be called, otherwiseonCommand(KelpConsoleSender, String[])
is called. If you enabledelegatePlayerToConsole
, the player method will execute the console method automatically.
-
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 inonCommandRegister
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 (soCONSOLE_ONLY
andPLAYER_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.
(c) 2019-2021 pxav.
Kelp is an open-source project maintained by multiple developers. If you have additions/questions/problems to report about the wiki, feel free to create an issue here on GitHub or join the Discord
- SQL Module coming soon
- Documentation in progress