コマンドの作成

最初の段階は、新しい CommandSpec builderを取得することです。Builderはコマンドのヘルプメッセージやコマンドの引数、コマンドの構成の編集を行うメソッドを提供します。これらのメソッドはチェーンで呼び出すことができます。

最後にコマンドをビルドするために、 CommandSpec.Builder#build() メソッドを呼んでください。

その後、コマンドを登録する必要があります。

例:シンプルなコマンドの作成

import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.spec.CommandSpec;

CommandSpec myCommandSpec = CommandSpec.builder()
    .description(Text.of("Hello World Command"))
    .permission("myplugin.command.helloworld")
    .executor(new HelloWorldCommand())
    .build();

Sponge.getCommandManager().register(plugin, myCommandSpec, "helloworld", "hello", "test");

CommandSpec builder にあるメソッドの概要

メソッド

説明

executor

コマンドのロジックを定義します (Writing a Command Executor をみてください)。

子コマンドをセットしない場合、 Executorをセットする必要があります

arguments

このコマンドの引数の仕様を設定します。( 引数のパース を参照してください。)

permission

コマンドを使用する前に確認されるパーミッションを設定します。

description

ヘルプシステムによって表示されるこのコマンドの目的の短い1行の説明です。

extendedDescription

Sets an extended description to use in longer help listings. Will be appended to the short description.

child

Adds a child command to this command with its aliases (See 子コマンド).

children

Sets the child commands of this command with their aliases (See 子コマンド).

inputTokenizer

引数をパースする方法を定義します。デフォルトでは、パーサーはスペースでコマンド入力を区切ります。引用符はひとつの引数としてカウントされます。

例: /tpworld Notch "My World"NotchMy World の2つの引数があることになります。

build

コマンドをビルドします。そのあと、コマンドを登録する必要があります。

Writing a Command Executor

The only required component to build a simple command is the command executor class, which contains the logic of the command.

The class has to implement the CommandExecutor interface, which defines the CommandExecutor#execute(CommandSource, CommandContext) method. The method is called on command execution and has two arguments:

  • コマンドを呼び出した元 (例: コンソール、コマンドブロックもしくはプレイヤー)

  • The command context object, which contains the parsed arguments (See 引数のパース)

Example: Simple Command Executor

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;

public class HelloWorldCommand implements CommandExecutor {

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        src.sendMessage(Text.of("Hello World!"));
        return CommandResult.success();
    }
}

ちなみに

You can use anonymous classes to define the command executor in the command build process (See the example in the 引数のパース page).

プレイヤーオンリーコマンド

Sometimes it is neccessary that only players can execute a command (e.g. a /suicide command).

Perform an instanceof check to determine the type of the CommandSource:

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.command.source.CommandBlockSource;
import org.spongepowered.api.command.source.ConsoleSource;

if(src instanceof Player) {
    Player player = (Player) src;
    player.sendMessage(Text.of("Hello " + player.getName() + "!"));
}
else if(src instanceof ConsoleSource) {
    src.sendMessage(Text.of("Hello GLaDOS!"));
    // The Cake Is a Lie
}
else if(src instanceof CommandBlockSource) {
    src.sendMessage(Text.of("Hello Companion Cube!"));
    // <3
}

注釈

We recommend you to add an optional [player] argument to make the command console-friendly (e.g. /suicide [player]).

The easiest solution for this is to append a playerOrSource command element (See 引数のパース).

コマンドの結果

The CommandExecutor#execute() method must always return a CommandResult. In most cases it is sufficient to return CommandResult#success() if the command was successful or CommandResult#empty() if it wasn’t. In cases where more information needs to be conveyed, a CommandResult#builder() should be used. The builder provides the several various methods that accepts an integer and will set the attribute of the same name. All attributes that are not set by the builder will be empty.

Command blocks can use those values to modify scoreboard stats, which then can be used for elaborate constructions consisting of multiple command blocks. A tutorial how the data is accessed can be found here.

CommandResult の作成

CommandResult result = CommandResult.builder()
    .affectedEntities(42)
    .successCount(1)
    .build();

This example uses a builder to create a CommandResult for a command which affected 42 entities and was successful.

エラー処理

The execute() method may also throw a CommandException, signaling that an error occured while trying to execute the command. If such an Exception is thrown, its message will be displayed to the command source, formatted as an error. Also, the commands usage message will be displayed. An ArgumentParseException, a subtype of CommandException is automatically thrown if the commands arguments could not be parsed.