Sponge Plugin 製作メモ [2. コマンドの作成]
随分と間があいたが,所用でSpongeプラグインを書くかもしれないのでドキュメントを読みつつメモ書き.
コマンドの作成方法
コマンドの作成にはCommandSpecビルダーメソッドを用いる.
CommandSpec myCommandSpec = CommandSpec.builder().description(Text.of("Hello World Command"))
.permission("myplugin.command.helloworld")
.executor(new Hello())
.build();
ここでHelloクラスではCommandExecutorを実装して処理を記述する.
public class Hello implements CommandExecutor {@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
src.sendMessage(Text.of("Hello World!"));
return CommandResult.success();
}
}
最後にCommandManagerにコマンドを通しておく.
public void onServerStart(GameStartedServerEvent event){Sponge.getCommandManager().register(this, hello, "hello");
}
CommandSpecビルダーには以下のようないくつかのメソッドが用意されている.
executor
コマンドが実行されたときの処理を定義する.さっきのコードはJava8のラムダ式を用いてこんなように記述も可能
CommandSpec hello = CommandSpec.builder().description(Text.of("Hello World Command"))
.permission("tuto.hello")
.executor((CommandSource src, CommandContext args) -> {
getLogger().info("Hello!");
return CommandResult.success();
})
.build();
後に記述する子コマンドをセットしない場合、 executorをセットする必要がある.
arguments
このコマンドの引数の仕様を設定する.(余裕があれば追記する)
permission
このコマンドのパーミッションを設定する.
description
このコマンドの説明を設定する.
children
このコマンドの子コマンドを追加する.
inputTokenizer
引数をパースする方法を記述する.(詳しくはargumentメソッドと合わせて追記するかも)
build
コマンドをビルドする.
CommandCallableなるものを使うとさらに低レイヤーの操作が可能っぽい.しばらくいじる気はない.(そのうちやるかも)