Command System
The command system is simple, but you can create complex commands and use advanced features.
It's for creating commands like /yourplg
which should maybe show the help page, or even complex ones like /yourplg help 3
to show the third help page.
Commands
The structure of a command is simple:
/command label argument1 argument2 ...
Of course, you don't have to provide all elements for calling a command. These inputs are also valid:
/command /command label
In the first case, the command would be called without a label or arguments ("default command"). In the second case, there is a label, but there are no arguments.
CommandExecutor
The command system is using the CommandExecutor
interface from Bukkit, so it's easy to integrate with the plugin.yml
command list. You can create as many CommandExecutor
s as you want (they're independent objects) for every command.
Let's start with two examples how to register the CommandExecutor
from your onEnable()
method in your main plugin class. Of course you have to define a command in the plugin.yml
before:
commands: testcommand: usage: /<command> description: Here's some command description ...
You can create a normal Bukkit command, so you can use aliases etc.
After that you have to insert some code into your onEnable()
method. There is this way to do it:
CommandExecutor commandExecutor = new CommandExecutor(this, "testcommand");
The second argument is a vararg. You can define unlimited commands. The code above is basically equivalent to this:
CommandExecutor commandExecutor = new CommandExecutor(this);
Bukkit.getPluginCommand("testcommand").setExecutor(commandExecutor);
Bukkit.getPluginCommand("testcommand").setTabCompleter(commandExecutor);
See the JavaDocs for more information about it.
CommandHandler
The CommandHandler
is a simple interface that you have to implement. A CommandHandler
represents a command after /testcommand
(for example) and can only execute that one command.
It is recommended to create a new class for every command.
If you implemented a CommandHandler
, you have to create the following methods:
public CommandInfo getInfo()
public void execute(Command command)
The getInfo()
method should return a CommandInfo
object. Here's the constructor:
public CommandInfo(boolean ignoreCase, String parameterUsage, String description, String permission, String... labels)
Parameter | Description |
---|---|
ignoreCase | Should the CommandExecutor ignore the case of the label? |
parameterUsage | Human readable description on how to use the parameters of the command (e.g. <radius> <strength> ; null if you can't use parameters).
|
description | A short description on what the command does. |
permission | The permission you need to execute the command. |
labels | All labels a user can use for the command (use CommandExecutor.DEFAULT_COMMAND_LABEL or <empty> if you want the command to be executed when no label is supplied, e.g. /command ). You can define unlimited labels via vararg.
|
The Command
object while executing can return the following informations (they should be self-explanatory):
public CommandSender getSender()
public String getGlobalLabel()
public String getLabel()
public String[] getArguments()
You can manage the CommandHandler
s of a CommandExecutor
with the following methods (in CommandExecutor
):
public List<CommandHandler> getCommandHandlers()
public CommandHandler getCommandHandler(String label)
public boolean containsCommandHandler(CommandHandler commandHandler)
public boolean containsCommandHandler(String label)
public void addCommandHandler(CommandHandler commandHandler)
public void removeCommandHandler(CommandHandler commandHandler)
public void removeCommandHandler(String label)
For more information use the JavaDocs.