Raw Command

The raw command interface can be used to define commands. The interface can be used as a base for custom command APIs.

It is recommended to use the コマンドの作成 for simple command definitions.

Writing a Command

The first step is to create a class for the command. The class has to implement the interface Command.Raw:

public class MyBroadcastCommand implements Command.Raw {

    private final Component usage = Component.text("<message>");
    private final Component description = Component.text("Display a message to all players");

    @Override
    public CommandResult process(CommandCause cause, ArgumentReader.Mutable arguments) throws CommandException {
        String message = arguments.remaining();
        Sponge.server().broadcastAudience().sendMessage(Component.text(message));
        return CommandResult.success();
    }

    @Override
    public List<CommandCompletion> complete(CommandCause cause, ArgumentReader.Mutable arguments) throws CommandException {
        return Collections.emptyList();
    }

    @Override
    public boolean canExecute(CommandCause cause) {
        return cause.hasPermission("myplugin.broadcast");
    }

    @Override
    public Optional<Component> shortDescription(CommandCause cause) {
        return Optional.of(this.description);
    }

    @Override
    public Optional<Component> extendedDescription(CommandCause cause) {
        return Optional.empty();
    }

    @Override
    public Component usage(CommandCause cause) {
        return this.usage;
    }
}

ちなみに

See the JavaDoc for Raw for the purposes of each method in the example

Registering the Command

Now we can register the class in the RegisterCommandEvent event. You can register the command using the RegisterCommandEvent#register(PluginContainer, Command, String, String…) passing your plugin’s container, an instance of the raw command and the main command as well as any aliases.

@Inject
PluginContainer container;

@Listener
public void onRegisterRawCommands(final RegisterCommandEvent<Command.Raw> event){
    event.register(this.container, new MyBroadcastCommand(), "broadcast");
}