Низкоуровневый API команд

The CommandCallable and Dispatcher interfaces can be used to define commands. The interfaces can be used as a base for custom command APIs.

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

Написание команды

Первым шагом является создание класса для команды. Класс нуждается в внедрении интерфейса 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();
    }
}

Совет

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

Регистрация команды

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");

Примечание

Аргументы после указания экземпляра команды являются дополнительными вариантами для регистрации команды. Вы можете добавить столько, сколько вы хотите. Первый вариант, который не используется другой командой становится основным. Это означает, что варианты используемые другой командой игнорируются.

Command Dispatchers

Command dispatchers can be used to create hierarchical command structures (subcommands).

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");