Skip to content

实体命令

本页面镜像自
BedrockWiki

根据原始项目协议授权。本文经过AI翻译处理,如有内容遗漏,可以提交PR进行补充。

WARNING

通过run_command事件响应来执行实体命令是更简单的方法,但目前该功能仍处于实验性阶段。

动画控制器

要触发斜杠命令,我们将使用行为包动画控制器。动画控制器应放置在animation_controllers/some_controller.json路径下。你可以在bedrock.dev的实体事件章节了解更多关于动画控制器的知识。

简而言之,动画控制器允许我们从行为包中触发事件:

  • 斜杠命令(如/say
  • Molang表达式(v.foo += 1;
  • 实体事件(如@s wiki:my_event

以下是一个动画控制器示例:

json
{
	"format_version": "1.10.0",
	"animation_controllers": {
		"controller.animation.sirlich_entity_commands": {
			"states": {
				"default": {
					"transitions": [
						{
							"on_summon": "1" // 1表示真值
						}
					]
				},
				"on_summon": {
					"on_entry": ["/say I have been summoned"]
				}
			}
		}
	}
}

当实体被召唤到世界时,这个动画控制器会立即执行/say I have been summoned命令。如果对工作原理有疑问,请复习Molang、动画和实体事件相关内容。

简单来说,states可以通过on_entry子句触发事件。我们使用查询条件在不同状态间切换。除非定义了initial_state值,否则实体默认处于default状态。

WARNING

当世界/区块重新加载时,查询会重新运行。这意味着"/say I have been summoned"命令实际上会在每次实体"加载"时执行——而不仅在被召唤时。

如果需要避免这种情况,需要添加额外查询条件,例如使用skin_id查询。首次生成实体时检查skin_id = 0,然后将其设置为更高的值如skin_id = 1。这样当实体重新加载时就不会再次触发命令。下文将展示具体实现方法。

使用动画控制器

要将动画控制器添加到实体中,可以在实体定义描述中使用以下代码:

json
"description": {
    "identifier": "wiki:entity_commands",
    "scripts": {
        "animate": [
            "wiki:entity_commands"
        ]
    },
    "animations": {
        "wiki:entity_commands": "controller.animation.wiki_entity_commands"
    }
}

如果对步骤有疑问,请再次查阅实体事件文档

通过事件触发命令

动画状态过渡使用查询条件实现。可查阅实体查询列表。在第一个示例中,查询条件简化为true,因此命令会自动执行。我们可以使用更复杂的查询条件实现更精细的控制,其中通过组件作为Molang过滤器来触发命令是便捷的方法。

推荐使用skin_id组件。

更新后的动画控制器基于skin_id触发:

json
{
	"format_version": "1.10.0",
	"animation_controllers": {
		"controller.animation.sirlich_entity_commands": {
			"states": {
				"default": {
					"transitions": [
						{
							"command_example": "q.skin_id == 1"
						},
						{
							"zombies": "q.skin_id == 2"
						}
					]
				},
				"command_example": {
					"transitions": [
						{
							"default": "q.skin_id != 1"
						}
					],
					"on_entry": ["/say Command One!", "@s execute_no_commands"]
				},
				"zombies": {
					"transitions": [
						{
							"default": "q.skin_id != 2"
						}
					],
					"on_entry": [
						"/say AHH! Zombies everywhere!",
						"/summon minecraft:zombie",
						"/summon minecraft:zombie",
						"/summon minecraft:zombie",
						"/summon minecraft:zombie",
						"@s execute_no_commands"
					]
				}
			}
		}
	}
}

现在这个动画控制器有两个命令状态:skin_id = 1触发第一个,skin_id = 2触发第二个。注意使用==!=运算符(不要使用单个=)。@s execute_no_commands语法用于在命令列表末尾重置skin_id,下文将创建这个事件。

设置组件组

在实体文件中,可以通过skin_id组件设置值:

json
"component_groups": {
    "execute_no_commands": {
        "minecraft:skin_id": {
            "value": 0
        }
    },
    "command_example": {
        "minecraft:skin_id": {
            "value": 1
        }
    },
    "command_zombies": {
        "minecraft:skin_id": {
            "value": 2
        }
    }
}

添加事件

创建事件以便管理组件组:

json
"events": {
    "minecraft:entity_spawned": {
        "add": {
            "component_groups": [
                "execute_no_commands"
            ]
        }
    },
    "execute_no_commands": {
        "add": {
            "component_groups": [
                "execute_no_commands"
            ]
        }
    },
    "command_example": {
        "add": {
            "component_groups": [
                "command_example"
            ]
        }
    },
    "command_zombies": {
        "add": {
            "component_groups": [
                "command_zombies"
            ]
        }
    }
}

触发事件

Minecraft中有多种触发事件的方式,以下是两个典型示例:

交互组件

此组件会在点击实体时生成僵尸:

json
"minecraft:interact": {
    "interactions": [{
        "on_interact": {
            "filters": {
                "all_of": [{
                        "test": "is_family",
                        "subject": "other",
                        "value": "player"
                    }
                ]
            },
            "event": "command_zombies"
        }
    }]
}

定时器

此组件每10秒触发示例命令:

json
"minecraft:timer": {
    "looping": true,
    "time": 10,
    "time_down_event": {
        "event": "example_command"
    }
}

通过添加这些组件,我们可以控制skin_id的变化时机,从而决定哪些事件会被触发。

流程总结

完整工作流程如下:

  1. 通过交互或定时器等组件触发example_command事件
  2. 事件添加example_command组件组
  3. 组件组设置实体的skin_id
  4. 动画控制器检测到skin_id变化,切换到对应状态
  5. 执行状态中的/say等命令
  6. 执行@s execute_no_command事件重置skin_id
  7. 动画控制器检测重置,返回默认状态
  8. 等待新的skin_id触发新事件

通过这种机制,可以实现精准的实体行为控制。