API

命令与权限

命令支持类型化参数、子命令、别名、补全建议和权限。有人用错时,会看到正确的用法,不需要你自己写这条消息。

CraftScript 中

每条命令一个文件,放在 commands/ 中,文件名与命令名相同:

commands/pay.craftCraftScript
command pay(to: player, amount: int) requires "eco.pay" alias "send" describe "发送金币" {
    if player == null { return "仅限玩家使用" }
    if amount <= 0 { return "&c数额必须为正数" }
    return "已向 {to.name} 发送 {amount} 枚金币"
}
commands/announce.craftCraftScript
command announce(message: text) requires "eco.admin" describe "向所有人发布公告" {
    broadcast("&6[公告] {message}")
}

如果缺少参数或参数类型不符,玩家会看到诸如 Missing <amount>. Usage: /pay <to> <amount> 或 'abc' is not a whole number 的提示。

类型 接受
player 一名在线玩家,带补全建议
int、number 整数、数字
bool true/false、yes/no、on/off
string 一个词,或用引号括起的多个词
text 该行的剩余部分
id 游戏 id(minecraft:stone)

加上 ? 可使参数变为可选(target: player?)。

Kotlin 中

commands/HomeCommand.ktKotlin
package com.example.homes.commands

object HomeCommand {
    private val homes = mutableMapOf<UUID, Location>()

    fun register() {
        Crafting.commands.command("home") {
            description = "传送回家"
            alias("h")
            executes { c -> c.player.teleport(homes[c.player.uuid] ?: c.fail("&c尚未设置家")) }
            sub("set") {
                executes { c -> homes[c.player.uuid] = c.player.location; c.reply("&a已设置家") }
            }
            sub("of") {
                permission = "homes.others"
                player("target")
                executes { c -> c.player.teleport(homes[c.player("target").uuid] ?: c.fail("&c没有家")) }
            }
        }
    }
}

参数:string、greedy、int、double、bool、player、identifier,每种都支持 optional。在 executes 中可以使用:c.player(执行者)、c.player("target")、c.int("n")、c.getOrNull<Int>("n")、c.reply(…),以及以错误消息终止执行的 c.fail(…)。

权限

Crafting 自带基于权限组的权限系统,配置在 config/crafting/permissions.json 中。服务器管理员(OP)拥有全部权限。

config/crafting/permissions.jsonJSON
{
  "groups": {
    "default": ["homes.use", "eco.pay"],
    "mod": ["homes.*", "chatguard.*", "-homes.others"],
    "admin": ["*"]
  },
  "players": {
    "Steve": { "groups": ["mod"], "permissions": ["homes.others"] }
  }
}
  • * 授予全部权限;homes.* 授予所有以 homes. 开头的权限。
  • -权限 表示拒绝该权限。最具体的规则优先;同样具体时,拒绝优先。
  • 所有玩家都属于 default 组。
  • 给你的权限加上模组 id 作为前缀(rubies.give),以免与其他模组冲突。

Crafting 命令

这些命令在游戏内和服务器控制台中均可使用。

命令 作用
/crafting(或 /cf) 版本与帮助。
/crafting mods 已加载的模组及其状态。
/crafting reload <id> 重新加载脚本或插件。需要 crafting.admin。
/crafting profile 第一次执行开始统计;第二次执行显示每个模组的耗时。
/crafting info 启动时间、监听器、任务、内存和内容。