fix: 外部から使うとエラーの原因になる関数を修正
This commit is contained in:
parent
db6362444c
commit
07b18d16f7
23
README.md
23
README.md
|
|
@ -34,7 +34,7 @@ dependencies {
|
|||
|
||||
1. プラグインの `onEnable` などで `kommand(plugin) { ... }` DSL を呼び出します。
|
||||
2. `command("root", "alias") { ... }` でコマンドを宣言し、`literal` や `string`/`integer` 引数を追加します。
|
||||
3. `executes { ... }` は必ず対象のノード (`literal`, `player`, `integer` など) の中にネストします。これにより、そのノードまでに宣言した引数を `string("player")` や `int("amount")` のように取得できます。
|
||||
3. `executes { ... }` は必ず対象のノード (`literal`, `player`, `integer` など) の中にネストします。これにより、そのノードまでに宣言した引数を `argument<String>("player")` や `argument<Int>("amount")` のように取得できます。
|
||||
|
||||
### サンプル: 経済コマンド
|
||||
|
||||
|
|
@ -52,8 +52,8 @@ class EconomyPlugin : JavaPlugin() {
|
|||
player("target") { // プレイヤー名 or セレクター (@p 等)
|
||||
integer("amount", min = 1) {
|
||||
executes {
|
||||
val target = player("target")
|
||||
val amount = int("amount")
|
||||
val target: Player = argument("target")
|
||||
val amount = argument<Int>("amount")
|
||||
sender.sendMessage("Giving $amount to ${target.name}")
|
||||
}
|
||||
}
|
||||
|
|
@ -64,8 +64,8 @@ class EconomyPlugin : JavaPlugin() {
|
|||
players("targets") { // @a, プレイヤー名, などをまとめて取得
|
||||
float("value", min = 0.1, max = 5.0) {
|
||||
executes {
|
||||
val targets = players("targets")
|
||||
val speed = float("value")
|
||||
val targets: List<Player> = argument("targets")
|
||||
val speed = argument<Double>("value")
|
||||
targets.forEach { it.walkSpeed = speed.toFloat() / 5.0f }
|
||||
sender.sendMessage("Updated ${targets.size} players")
|
||||
}
|
||||
|
|
@ -77,7 +77,8 @@ class EconomyPlugin : JavaPlugin() {
|
|||
coordinates("point") { // "~ ~1 ~-2" のような入力を受け付ける
|
||||
executes {
|
||||
val base = (sender as? Player)?.location ?: return@executes
|
||||
val target = location("point", base)
|
||||
val coords = argument<Coordinates3>("point")
|
||||
val target = coords.resolve(base)
|
||||
plugin.server.worlds.first().setSpawnLocation(target)
|
||||
sender.sendMessage("Spawn set to ${target.x}, ${target.y}, ${target.z}")
|
||||
}
|
||||
|
|
@ -87,7 +88,7 @@ class EconomyPlugin : JavaPlugin() {
|
|||
literal("inspect") {
|
||||
selector("entities") {
|
||||
executes {
|
||||
val entities = selector("entities")
|
||||
val entities: List<Entity> = argument("entities")
|
||||
sender.sendMessage("Selector resolved ${entities.size} entities")
|
||||
}
|
||||
}
|
||||
|
|
@ -105,10 +106,10 @@ class EconomyPlugin : JavaPlugin() {
|
|||
### DSL 構文のポイント
|
||||
|
||||
- `literal("sub") { ... }` は固定語句を表すノードです。`requires("permission.node")` でその枝のみにパーミッションを設定できます。
|
||||
- `string("name")` や `integer("value", min = 0)` は値をパースし、成功すると `KommandContext` に記憶されます。
|
||||
- `float("speed")` は倍精度を、`player("target")`/`players("targets")`/`selector("entities")` は Minecraft の標準セレクター (`@p`, `@a`, `@s` など) やプレイヤー名を型付きで扱えます。
|
||||
- `string("name")` や `integer("value", min = 0)` は値をパースし、成功すると `KommandContext` に記憶されます。取得時は `argument<String>("name")` や `argument<Int>("value")` を呼び出してください。
|
||||
- `float("speed")` や `player("target")`/`players("targets")`/`selector("entities")` は Minecraft の標準セレクター (`@p`, `@a`, `@s` など) やプレイヤー名を型付きで扱えます。実行時は `argument<Double>("speed")`、`argument<Player>("target")`、`argument<List<Player>>("targets")` のように取得できます。
|
||||
- `suggests { prefix -> ... }` を指定すると、タブ補完時に任意の候補リストを返せます。
|
||||
- `coordinates("pos")` は `x y z` をまとめて 1 つの引数として受け取り、`location("pos", player.location)` で現在位置を基準に解決できます (`~` を使用した相対座標に対応)。
|
||||
- `coordinates("pos")` は `x y z` をまとめて 1 つの引数として受け取り、`argument<Coordinates3>("pos").resolve(player.location)` で現在位置を基準に解決できます (`~` を使用した相対座標に対応)。
|
||||
- `command` や各ノードの `condition { sender -> ... }` で実行条件 (例: コンソール禁止) を追加できます。
|
||||
- ルートレベルで `executes { ... }` を指定すると、引数なしで `/eco` を実行した場合に呼び出されます。
|
||||
|
||||
|
|
@ -159,7 +160,7 @@ commands = kommand(this) {
|
|||
| `selector("entities")` | `List<Entity>` | Bukkit の `Bukkit.selectEntities` をそのまま利用 |
|
||||
| `coordinates("pos")` | `Coordinates3` | `~` 相対座標を含む 3 軸をまとめて扱う |
|
||||
|
||||
`Coordinates3` は `coordinates("pos") { ... }` 直後のコンテキストで `coordinates("pos")` もしくは `location("pos", baseLocation)` として取得できます。
|
||||
`Coordinates3` は `coordinates("pos") { ... }` 直後のコンテキストで `argument<Coordinates3>("pos")` として取得でき、`resolve(baseLocation)` で基準座標に対して実座標を求められます。
|
||||
|
||||
## ビルドとテスト
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
package net.hareworks.kommand_lib.context
|
||||
|
||||
import net.hareworks.kommand_lib.arguments.Coordinates3
|
||||
import org.bukkit.Location
|
||||
import org.bukkit.command.CommandSender
|
||||
import org.bukkit.entity.Entity
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
import net.hareworks.kommand_lib.execution.ParseMode
|
||||
|
||||
|
|
@ -35,21 +31,3 @@ open class KommandContext internal constructor(
|
|||
|
||||
fun arguments(): Map<String, Any?> = parsedArguments.toMap()
|
||||
}
|
||||
|
||||
fun KommandContext.string(name: String): String = argument(name)
|
||||
|
||||
fun KommandContext.int(name: String): Int = argument(name)
|
||||
|
||||
fun KommandContext.double(name: String): Double = argument(name)
|
||||
|
||||
fun KommandContext.float(name: String): Double = argument(name)
|
||||
|
||||
fun KommandContext.player(name: String): Player = argument(name)
|
||||
|
||||
fun KommandContext.players(name: String): List<Player> = argument(name)
|
||||
|
||||
fun KommandContext.selector(name: String): List<Entity> = argument(name)
|
||||
|
||||
fun KommandContext.coordinates(name: String): Coordinates3 = argument(name)
|
||||
|
||||
fun KommandContext.location(name: String, origin: Location): Location = coordinates(name).resolve(origin)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user