API de baixo nível para comandos

As interfaces CommandCallable e Dispatcher podem ser utilizadas para definir comandos. Elas podem ser usadas como base para uma API de comandos customizados.

It is recommended to use the Command Builder API for simple command definitions.

Escrever um comando

O primeiro passo é criar uma classe para o comando. A classe tem de implementar a interface CommandCallable:

import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class MyBroadcastCommand implements CommandCallable {

    private final Optional<Text> desc = Optional.of(Text.of("Displays a message to all players"));
    private final Optional<Text> help = Optional.of(Text.of("Displays a message to all players. It has no color support!"));
    private final Text usage = Text.of("<message>");

    public CommandResult process(CommandSource source, String arguments) throws CommandException {
        Sponge.getServer().getBroadcastChannel().send(Text.of(arguments));
        return CommandResult.success();
    }

    public boolean testPermission(CommandSource source) {
        return source.hasPermission("myplugin.broadcast");
    }

    public Optional<Text> getShortDescription(CommandSource source) {
        return desc;
    }

    public Optional<Text> getHelp(CommandSource source) {
        return help;
    }

    public Text getUsage(CommandSource source) {
        return usage;
    }

    public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
        return Collections.emptyList();
    }
}

Dica

See the JavaDoc for CommandCallable for the purposes of each method in this example.

Registrar o comando

Now we can register the class in the CommandManager. The CommandManager stands as the manager for watching what commands get typed into chat, and redirecting them to the right command handler. To register your command, use the method CommandManager#register(Object, CommandCallable, String…), passing your plugin, an instance of the command, and any needed aliases as parameters.

import org.spongepowered.api.command.CommandManager;

CommandManager cmdService = Sponge.getCommandManager();
PluginContainer plugin = ...;
cmdService.register(plugin, new MyBroadcastCommand(), "message", "broadcast");

Nota

The arguments after the new instance of your command are the aliases to register for the command. You can add as many Strings as you want. The first alias that isn’t used by another command becomes the primary alias. This means aliases used by another command are ignored.

Despachantes de comandos

Despachantes de comandos podem ser utilizados para criar estruturas de comandos hierárquicas (subcomandos).

The default implementation of the Dispatcher interface is the SimpleDispatcher class.

A Dispatcher is also a CommandCallable, so it can be registered like any other command.

import org.spongepowered.api.command.dispatcher.SimpleDispatcher;

CommandCallable subCommand1 = ...;
CommandCallable subCommand2 = ...;

SimpleDispatcher rootCommand = new SimpleDispatcher();

rootCommand.register(subCommand1, "subcommand1", "sub1");
rootCommand.register(subCommand2, "subcommand2", "sub2");

Sponge.getCommandManager().register(this, rootCommand, "root");