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.
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.
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
CommandExecutors 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
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 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)
getInfo() method should return a
CommandInfo object. Here's the constructor:
public CommandInfo(boolean ignoreCase, String parameterUsage, String description, String permission, String... labels)
| ||Should the CommandExecutor ignore the case of the label?|
| || Human readable description on how to use the parameters of the command (e.g. |
| ||A short description on what the command does.|
| ||The permission you need to execute the command.|
| || All labels a user can use for the command (use |
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
CommandHandlers of a
CommandExecutor with the following methods (in
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.