はじめに
コマンドをTab補完できるようにする機能について。説明はしません。覚え書きです。
私なりの解釈で自由にメモ程度に置いておくだけです。
参考にならないかもしれませんが悪しからず。
免責事項
基本的に解説は Kotlin をもちいておこなっています。IDEによっては Kotlin とJavaのコンバートを行えることがあると思うので、利用の際は変換してからご利用ください。
骨組み
Kotlin:
commandParameters.clear()
commandParameters["parameterName"] = listOf<CommandParameter>()
commandParameters.clear()は必須
CommandParameterの要素
Kotlin:
// 以下 newType() 関数
CommandParameter.newType(
"anonymous", // 任意の文字。 <anonymous : int> こんなかんじに表示される
false, // 任意の値かどうか。コマンドの囲み記号が変わります。 <必須> [任意]
CommandParamType.INT // パラメーターの値
) // Int値
CommandParameter.newType("float", false, CommandParamType.FLOAT) // Float値
CommandParameter.newType("value", false, CommandParamType.VALUE) // 値
CommandParameter.newType("wildCardInt", false, CommandParamType.WILDCARD_INT) // 乱数?
CommandParameter.newType("player", false, CommandParamType.TARGET) // プレイヤー
CommandParameter.newType("wildCardPlayer", false, CommandParamType.WILDCARD_TARGET) // ランダムなプレイヤー?
CommandParameter.newType("string", false, CommandParamType.STRING) // 文字列
CommandParameter.newType("blockPosition", false, CommandParamType.BLOCK_POSITION) // x, y, z ?
CommandParameter.newType("position", false, CommandParamType.POSITION) // x, y, z
CommandParameter.newType("message", false, CommandParamType.MESSAGE) // 文字列
CommandParameter.newType("rawtext", false, CommandParamType.RAWTEXT) // 文字列
CommandParameter.newType("json", false, CommandParamType.JSON) // Json
CommandParameter.newType("command", false, CommandParamType.COMMAND) // コマンド
CommandParameter.newType("filePath", false, CommandParamType.FILE_PATH) // ?
CommandParameter.newType("operator", false, CommandParamType.OPERATOR) // Opのセレクター?
// 以下 newEnum() 関数
CommandParameter.newEnum(
"selectable", //任意
false, // 任意の値かどうか
CommandEnum("CamelCase", // 絶対にキャメルケースの文字列
"lowercase", "lowercase", "lowercase" // 絶対にローワーケースの文字列
)
)
// 以下 newPostfix() 関数
CommandParameter.newPostfix(
"postfix", //任意
false, // 任意の値かどうか
"string-data" // デフォルト値?
)
座標のワイルドカード「~」はプラグイン側で別途処理を必要とする。
newEnumを使うとき必ず
- CommandEnumの第一引数はキャメルケース
- CommandEnumの第二引数以降 (文字列リスト) はローワーケース。
仮に、以下の複数の例のように定義した場合、すべて連結した選択可能引数となり、セレクタが機能しなくなる。
Kotlin:
// ダメパターン1
CommandParameter.newEnum("fruit", CommandEnum("fruit", "apple", "strawberry", "melon", "orange"))
CommandParameter.newEnum("vegetable", CommandEnum("vegetable", "tomato", "potato", "cabbage"))
// ダメパターン2
CommandParameter.newEnum("fruit", CommandEnum("Fruit", "Apple", "Strawberry", "Melon", "Orange"))
CommandParameter.newEnum("vegetable", CommandEnum("Vegetable", "Tomato", "Potato", "Cabbage"))
TextCommand.kt
以下使用サンプル。各種形態のメッセージを送るコマンド。
デバッグしてません。
Kotlin:
import cn.nukkit.Server
import cn.nukkit.command.Command
import cn.nukkit.command.CommandSender
import cn.nukkit.command.data.CommandEnum
import cn.nukkit.command.data.CommandParamType
import cn.nukkit.command.data.CommandParameter
class TextCommand : Command("text") {
init {
// コンストラクタで実行
usage = "/text <player> <text> <title|subtitle|actionbar|tip|popup|message> [stayCount]"
commandParameters.clear() // コマンド部分のパラメーターを消す(たぶん)
commandParameters["parameterType1"] = arrayOf(
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newType("text", CommandParamType.MESSAGE),
CommandParameter.newEnum("location", CommandEnum("TextSet", listOf("title", "subtitle", "actionbar", "tip", "popup", "message"))),
CommandParameter.newType("stayTime", true, CommandParamType.INT)
)
}
override fun execute(sender: CommandSender?, commandLabel: String?, args: Array<out String>?): Boolean {
args ?: return false
if (args.size < 3 || args.size > 4) return false
var stay: Int = 20
if (args.size == 4) stay = args[3].toInt()
val player = Server.getInstance().getPlayer(args[0]) ?: return false
when(args[2]) {
"title" -> player.sendTitle(args[1], "", 10, stay, 10)
"subtitle" -> player.sendTitle("", args[1], 10, stay, 10)
"actionbar" -> player.sendActionBar(args[1], 10, stay, 10)
"tip" -> player.sendTip(args[1])
"popup" -> player.sendPopup(args[1])
"message" -> player.sendMessage(args[1])
}
return true
}
}