Skip to content

Commands

Amaury Carrade edited this page Dec 30, 2015 · 6 revisions

This component simplifies the management of commands with sub-commands and aliases.

Documentation JavaDoc
Loader class
(for loadComponents)
fr.zcraft.zlib.components.commands.Commands

 

Writing commands

Each sub-command of a command is a class extending Command, with a @CommandInfo annotation to describe the command (name, parameters help, aliases).

In the Command sub-class, you have to override the run() (and optionally complete()) methods, respectively to execute the command and to auto-complete it.

package fr.zcraft.ztoaster.commands;

import fr.zcraft.zlib.components.commands.Command;
import fr.zcraft.zlib.components.commands.CommandException;
import fr.zcraft.zlib.components.commands.CommandInfo;

@CommandInfo(name = "awesomeness")
public class AwesomeCommand extends Command
{
    @Override
    protected void run() throws CommandException
    {
        // Your awesome command here
        sender.sendMessage(ChatColor.GOLD + "Such awesomeness");
        sender.sendMessage(ChatColor.GREEN + "Very grassy");
        sender.sendMessage(ChatColor.GRAY + "Wow.");
    }
}

Alongside, a few methods are available to easily retrieve and parse parameters, or to send answers. All the attributes (like sender in the example code above) and methods are described in the documentation of the Command class.

Errors are managed with exceptions: if something bad happens, throw a CommandException with the good reason. Methods are here to do that for you, too.

Method Description
error(String message) To be called if something bad happens.
throwInvalidArgument(String reason) To be called if an argument is invalid (explain why with reason).
playerSender() Returns the sender casted to Player or throws a CommandException with the reason COMMANDSENDER_EXPECTED_PLAYER.
getBooleanParameter(int index)
getDoubleParameter(int index)
getFloatParameter(int index)
getIntegerParameter(int index)
getLongParameter(int index)
getEnumParameter(int index, Class<T> enumType)
Returns the parameter at the given index casted to the required type, and throws a CommandException with the reason INVALID_PARAMETERS if it's not possible.
 

Registering commands

When the subcommands are ready, you'll have to register them like this.

// "toaster" is the name of the root command: /toaster ...
Commands.register("toaster", AwesomeCommand.class, AnotherCommand.class);

 

Writing documentation

This component auto-generates the user documentation of the commands, through a description when the command is executed without subcommand and an auto-generated help subcommand.

This said, the documentation does not comes from no-where: the help texts are wrote in a help subdirectory of your resources directory (next to the plugin.yml file).

Clone this wiki locally