添加世界逻辑 type
自 SDK 1.2 起,插件可以为世界逻辑添加新的触发器 · 条件 · 动作 type。通过 host.AddSceneEvent 注册的 type 与内置 type 出现在相同位置。编辑器中的注册用于表单 · 检查 · 预览;要在游戏服务器上实际运行,还要在服务器插件 NPBridge 端注册相同 type 的处理器(见下方“服务器端”)。
| 出现位置 | 显示内容 |
|---|---|
| 世界逻辑 面板 | 触发器 · 条件 · 动作菜单中的 Category 分组、字段输入 |
| 逻辑图 | 节点与输入值引脚 |
| 检查 | 必填字段 · Actor kind · 数值范围 · enum 值 |
| 规则一行说明 | Summary 模板 |
| 运行预览 | Simulate 处理 · 触发 按钮 |
| AI 工作室 | 在 AI 使用的工具 scene_rules 的 type 列表(section=schema · types)中带 [<插件 id>] 显示 |
在 plugin.json 中写 "sdk": "^1.2" 或更高。使用字段类型 ActorList 时为 "^1.3"。
-
在
Register中创建NpSceneEvent并传给host.AddSceneEvent。下例添加一个触发器和一个动作。host.AddSceneEvent(new NpSceneEvent(NpSceneEventRole.Trigger, "myquest_npc_talk", "与 NPC 对话时", "任务", new[]{new NpSceneField("npc", NpSceneFieldKind.Actor, Required: true, ActorKinds: new[] { "display", "text", "spawn" }, Label: "NPC"),new NpSceneField("line", NpSceneFieldKind.Text, Label: "台词 id"),},Help: "玩家开始与 NPC 对话时", Icon: "interact",Summary: "玩家与 NPC {npc} 对话时")); // 无 Simulate = 只能用“触发”按钮触发host.AddSceneEvent(new NpSceneEvent(NpSceneEventRole.Action, "myquest_start", "开始任务", "任务", new[]{new NpSceneField("quest", NpSceneFieldKind.Text, Required: true, Label: "任务 id"),},Icon: "star", Summary: "开始任务 {quest}",Simulate: s => { s.Log($"任务 {s.Str("quest")} 开始 → {s.Player}"); return true; })); -
构建后,在插件管理器中单击 重新加载 (所选) 。注册 列显示
场景事件 2。 -
单击 工具 > 世界逻辑 打开面板。触发器菜单的
任务分组中有与 NPC 对话时。 -
创建规则并单击 运行测试 ,然后单击该规则的 触发 。面板下方的记录列表中留下
[<插件 id>] 任务 … 开始 → …一行。
使用这些 type 的规则在 scene.json 中这样保存(规则的一部分)。
{ "trigger": { "type": "myquest_npc_talk", "npc": "npc_guard" }, "actions": [ { "type": "myquest_start", "quest": "q1" } ]}- 没有
Simulate的触发器在预览中不会自行发生。由服务器事件(NPC 对话等)引发的触发器不写Simulate,用 触发 按钮测试。 - 在预览中要记住状态的条件 · 动作,把状态放在插件字段中,并在
host.WorldChanged中清空(见下例)。 - 在逻辑图的代码视图中,插件 type 显示为
MyquestStart(quest: "q1")形式。含有无法读回的字段时,以Node("myquest_start", …)形式保留。 - AI 工作室的 AI 也能看到插件 type。
Label·Help· 字段Help写得越详细,AI 越能正确使用该 type。 - 为避开内置 type 名称,先在世界逻辑 type 列表中查找。0.11.0 的内置 type 共 200 个(触发器 75 · 条件 16 · 动作 109)。
NpSceneEvent
Section titled “NpSceneEvent”public sealed record NpSceneEvent(NpSceneEventRole Role, string Type, string Label, string Category, IReadOnlyList<NpSceneField> Fields, string Help = "", string Icon = "event", string Summary = "", Func<NpSceneSim, bool>? Simulate = null);public enum NpSceneEventRole { Trigger, Condition, Action }| 字段 | 必填 | 说明 |
|---|---|---|
Role | 是 | 角色:Trigger(触发器) · Condition(条件) · Action(动作) |
Type | 是 | 保存名称(JSON "type")。前后不留空格,在前面加上自己的名称(见下方“命名规则”)。 |
Label | 是 | 在表单 · 图中显示的名称。为空时会引发注册异常。 |
Category | 否 | 菜单分组名称。留空时为插件名称(plugin.json 的 name) |
Fields | 是 | 字段列表(NpSceneField)。没有字段时为空数组。字段名为 type,或同一名称出现两次时会引发注册异常。 |
Help | 否 | 说明(工具提示) |
Icon | 否 | 图标名称(trigger · event · signal · interact · logic · star …)。默认 event,未知名称时使用默认图案 |
Summary | 否 | 一行说明模板(见下方“Summary 模板”) |
Simulate | 否 | 运行预览处理(见下方“Simulate”) |
插件 type 在同一分组中排在内置 type 之后。
NpSceneField
Section titled “NpSceneField”public sealed record NpSceneField(string Name, NpSceneFieldKind Kind, bool Required = false, string Default = "", string[]? Options = null, string[]? ActorKinds = null, string Label = "", string Help = "", double? Min = null, double? Max = null);| 字段 | 说明 |
|---|---|
Name | JSON 键 |
Kind | 字段类型(见下表) |
Required | 为必填时,留空会出现检查错误 |
Default | 默认值字符串。格式见下表的“Default 写法” |
Options | Enum 的选项 |
ActorKinds | Actor 字段接受的 Actor kind(trigger · blocker · display · text · spawn · camera · path …)。留空时接受任意 kind |
Label · Help | 表单中显示的名称 · 说明 |
Min · Max | Number 的范围(检查) |
字段类型 NpSceneFieldKind
Section titled “字段类型 NpSceneFieldKind”| 类型 | 表单输入 | JSON 值 | Default 写法 | 图输入引脚 | SDK |
|---|---|---|---|---|---|
Text | 文本 | 字符串 | "你好" | 文本 | 1.2 |
Number | 数值 | 数值 | "1.5" | 数值 | 1.2 |
Bool | 勾选 | true / false | "true" | 布尔 | 1.2 |
Enum | 选项之一(Options) | 字符串 | "none" | 文本 | 1.2 |
Actor | 选择 Actor(仅 ActorKinds) | Actor id 字符串 | Actor id | Actor | 1.2 |
Anim | 动画片段 | 片段 id 字符串 | 片段 id | 文本 | 1.2 |
SoundEvent | 声音事件 | "ns:path" 字符串 | "minecraft:block.note_block.bell" | 声音 | 1.2 |
Block | 方块状态 | 字符串(minecraft:stone) | 方块状态字符串 | 方块 | 1.2 |
Item | 物品 | 字符串(minecraft:diamond_sword) | 物品 id | 物品 | 1.2 |
Particle | 粒子 | 字符串(minecraft:happy_villager) | 粒子 id | 粒子 | 1.2 |
Effect | 药水效果 | 字符串(minecraft:speed) | 效果 id | 效果 | 1.2 |
Vec3 | x · y · z | [x, y, z] | "[0,1,0]" | 位置 | 1.2 |
Signal | 信号名称 | 字符串 | 信号名称 | 无(仅字段) | 1.2 |
Flag | 场景标志名称 | 字符串 | 标志名称 | 无(仅字段) | 1.2 |
ActorList | 多个 Actor | Actor id 数组 | "cam_a,cam_b" | 无(仅字段) | 1.3 |
- 仅内置 type 使用的字段类型(标志值 · 调用函数 · 方块格列表)和字段选项(显示条件 · 仅限整数 · 允许
#标签· 选择列表)不在 SDK 中。 - 在逻辑图中,插件触发器节点会自动带有触发者输出引脚
玩家({player})。其他输出引脚(事件数据 · 结果值)无法用 SDK 创建。 - 在图中把 Actor 连接到
Actor字段引脚时,会检查ActorKinds。
Summary 模板
Section titled “Summary 模板”用于规则列表中一行说明的模板。{字段名} 处填入该字段的值。
| 模板 | 字段值 | 显示文字 |
|---|---|---|
开始任务 {quest} | quest = q1 | 开始任务 q1 |
玩家与 NPC {npc} 对话时 | npc = npc_guard(名称“守卫”) | 玩家与 NPC 「守卫」 对话时 |
任务 {quest} 为 {state} 时 | state 为空,Default = none | 任务 q1 为 none 时 |
Actor字段不显示 Actor id,而是显示加上「」的 Actor 名称。- 没有值的字段用
Default填充。 Summary留空时,使用编辑器默认的一行说明。
Simulate — 运行预览
Section titled “Simulate — 运行预览”Simulate 是在编辑器运行 · 模拟期间预先执行规则的函数。游戏服务器不使用它。
| 角色 | Simulate 的作用 | 返回值 | 没有时 |
|---|---|---|---|
| Trigger | 预览的每个 tick 中,对该 type 的每条规则各调用一次。要触发时调用 s.Fire(player) | 不使用 | 只能通过 世界逻辑 · 运行面板的 触发 按钮触发 |
| Condition | 判定真 · 假 | true = 真 | 始终视为真 |
| Action | 执行动作(记录 · 更改自身状态) | 不使用 | 只留下记录 |
Simulate 中发生异常时,该插件被隔离。该插件的所有 type 都会被移除,预览继续运行。
Simulate 接收的 NpSceneSim 如下。
| 成员 | 类型 | 说明 |
|---|---|---|
Role · Type | NpSceneEventRole · string | 当前处理的角色 · type |
RuleId | string | 规则 id |
Player | string? | 触发者。在触发器处理中为 null |
Players | IReadOnlyList<string> | 当前的玩家 |
Now | double | 预览时刻(秒) |
Args | IReadOnlyDictionary<string, string> | 节点字段值(全部为字符串) |
Str(string key, string fallback = "") | string | 字段值字符串 |
Num(string key, double fallback = 0) | double | 字段值数值(点作小数点) |
Bool(string key, bool fallback = false) | bool | 字段值 "true" / "false" |
PositionOf(string player) | NpVec3? | 玩家脚下位置。没有时为 null |
Log(string text) | void | 一行预览记录([<插件 id>] …) |
Fire(string? player) | void | (仅 Trigger 处理)触发此规则。在其他角色中不起作用 |
| 字段值 | Args 字符串 |
|---|---|
| 字符串 | 原样 |
| 数值 | "1.5"(点作小数点,与区域设置无关) |
| 真 · 假 | "true" / "false" |
数组 · 对象(Vec3 · ActorList) | JSON 字符串("[0,1,0]") |
下面是自行触发的触发器示例。它在每个预览 tick 调用,因此要写得轻量。
host.AddSceneEvent(new NpSceneEvent(NpSceneEventRole.Trigger, "myquest_near_spawn", "接近出生点时", "任务", new[] { new NpSceneField("r", NpSceneFieldKind.Number, Default: "5", Label: "半径", Min: 1, Max: 64), }, Summary: "进入出生点 {r} 格范围内时", Simulate: s => { foreach (var p in s.Players) if (s.PositionOf(p) is { } pos && Math.Abs(pos.X) + Math.Abs(pos.Z) < s.Num("r", 5)) s.Fire(p); return true; }));下面是记住状态的条件 · 动作示例。
var quests = new Dictionary<string, string>(); // 玩家:任务 → 状态string Get(string? player, string quest) => quests.TryGetValue((player ?? "") + ":" + quest, out var v) ? v : "none";
host.AddSceneEvent(new NpSceneEvent(NpSceneEventRole.Condition, "myquest_state", "任务状态", "任务", new[] { new NpSceneField("quest", NpSceneFieldKind.Text, Required: true, Label: "任务 id"), new NpSceneField("state", NpSceneFieldKind.Enum, Default: "none", Options: new[] { "none", "active", "done" }, Label: "状态"), }, Summary: "任务 {quest} 为 {state} 时", Simulate: s => Get(s.Player, s.Str("quest")) == s.Str("state", "none")));
host.AddSceneEvent(new NpSceneEvent(NpSceneEventRole.Action, "myquest_activate", "设为任务进行中", "任务", new[] { new NpSceneField("quest", NpSceneFieldKind.Text, Required: true, Label: "任务 id"), }, Simulate: s => { quests[(s.Player ?? "") + ":" + s.Str("quest")] = "active"; s.Log("开始"); return true; }));
host.WorldChanged += _ => quests.Clear(); // 新世界中清空完整代码见示例: event_example。
| 规则 | 原因 |
|---|---|
在 Type 前加上自己的名称(myquest_start)。 | 同一角色中已有相同 type 时(含内置 · 其他插件)会引发注册异常,该插件不会加载,状态显示为“版本不匹配”(“与内置功能重复,未加载 — …”)。 |
使用小写字母 · 数字 · _。 | 便于与服务器插件 · JSON 保持一致。 |
用过的 Type · 字段 Name 不再更改。 | 已保存的规则(scene.json)使用该名称。 |
不使用内置 type 名称(enter · chat · sound · math · if · cinematic …)。 | 与内置重名时插件无法启用。 |
- 该插件的 type 从菜单 · 图的调色板中移除。
- 使用该 type 的规则不会被删除。这些规则会出现“未知 type”注意项,字段值原样保留。
- 在预览中,未知触发器跳过该规则,未知条件为假,未知动作只跳过该动作(每个 type 警告一次)。
在游戏中执行规则的是服务器插件 NPBridge(Paper)。自 NPBridge 2.0 起,NP Scene 包含在 NPBridge 中,且 NPBridge 声明了 provides: [NPScene],因此以前 depend 于 NPScene 的 Paper 插件也能照常启用。编辑器插件不在服务器上运行。
- 将相同 type 的处理器制作为 Paper 插件。
- 从 Bukkit
ServicesManager获取SceneEventsApi(包dev.npeditor.scene.api)并注册 type。 - 让字段名和值格式(JSON)与编辑器端的
NpSceneField完全一致。
SceneEventsApi api = Bukkit.getServicesManager().load(SceneEventsApi.class);api.registry().plugin("myquest") .trigger(TriggerType.of("myquest_npc_talk", TriggerType.ActorUse.REQUIRED, Set.of("display", "text", "spawn"))) .action("myquest_start", (a, ctx) -> { String quest = a.str("quest", ""); // … 为 ctx.player() 开始任务 … });// 在自己的事件中触发触发器:api.dispatch(SceneEvent.of("myquest_npc_talk", world.getName()).player(p.getUniqueId()).actor("npc_guard"));// 禁用时:api.registry().unregisterPlugin("myquest");- 在主线程中注册。注册后,使用该 type 的场景会在下一个 tick 重新读取,因此因 NPBridge 先启用而被跳过的规则也会恢复。
- 服务器不认识的 type 会伴随一次警告被跳过(触发器 = 跳过该规则,动作 = 只跳过该动作)。因此即使没有服务器处理器,地图也能打开,但该规则不会运行。
非 Minecraft 官方产品,与 Mojang 及 Microsoft 无关。