Skip to content

CraftTweaker API

mc_Edwin edited this page Jul 30, 2026 · 1 revision

CraftTweaker API

本模组扩展 MMCE 的 RecipePrimerIMachineController

配方模块条件

.withModule(string[] ids) as RecipePrimer
.withoutModule(string[] ids) as RecipePrimer
  • withModule:数组内模块必须全部生效(AND)。
  • withoutModule:数组内任意模块生效都会禁止配方。
  • 两个方法都返回原 RecipePrimer,可继续链式调用。
  • 可重复调用,条件会累加并去重。
  • ID 不能空白,不能使用保留 ID main
  • 同一个 ID 不能同时出现在需要与禁止集合中。

示例:

mods.modularmachinery.RecipeBuilder.newBuilder(
    "distillation_with_cooling",
    "distillation_tower",
    200
)
    .withModule(["cooling_compressor", "spacetime_pipe"])
    .withoutModule(["low_pressure_pipe"])
    .build();

模块条件会在配方开始、重启以及运行过程中持续检查。运行中失去必需模块或出现禁止模块时,配方会返回相应失败状态。

控制器查询

controller.hasModule(string id) as bool
controller.moduleList as string[]

hasModule

判断模块当前是否生效。未声明、结构不完整、依赖不满足、冲突失效以及 main 都返回 false。

val upgraded as bool = controller.hasModule("advanced");

moduleList

返回当前已识别机器 JSON 中声明的全部附属模块 ID,保持 JSON 顺序,不含 main。注意它是“可用/已声明列表”,不是“当前生效列表”。控制器尚未识别机器时返回空数组。

需要所有当前生效模块时,遍历 moduleList 并逐个调用 hasModule

GUI 事件示例

MMEvents.onControllerGUIRender("elysia_reactor", function(event as ControllerGUIRenderEvent) {
    var info as string[] = [
        "§e///大型反应单元控制面板///",
        "§a机器名称:§eELYSIA单元 - 大型反应单元",
        "§a附属模块:" +
            (event.controller.hasModule("advanced") ? "§e升级模块" : "§c无")
    ];
    event.extraInfo = info;
});

如果始终显示“无”,依次确认:主结构最近执行过检测、模块结构与旋转坐标正确、父模块生效、没有冲突模块、脚本 ID 与 JSON 大小写完全一致。可用 /data get block 或 NBT 工具检查控制器的 mmceComplementModules;有效模块应出现在该列表中。

脚本设计建议

  • 模块 ID 使用稳定的 ASCII snake_case,发布后不要随意重命名。
  • moduleList 适合动态 GUI/诊断,配方判定直接使用 withModule/withoutModule
  • 不要把 as-upgradehasModule 混为一谈:as-upgrade=false 只关闭同名升级,不会阻止模块本身生效。

Clone this wiki locally