村庄机制实现指南
本文适用于想要为自己的实体模仿村庄机制的开发者。
导航行为
首先从基础导航行为开始。
BP/entities/custom_villager.json#components
json
"minecraft:preferred_path":{
"max_fall_blocks":1,
"jump_cost":5,
"default_block_cost":1.5,
"preferred_path_blocks":[
{
"cost":0,
"blocks":[
"grass_path"
]
},
{
"cost":1,
"blocks":[
"cobblestone",
"stone"
]
},
{
"cost":50,
"blocks":[
"bed",
"lectern"
]
}
]
}允许实体随机行走。
BP/entities/custom_villager.json#components
json
"minecraft:behavior.random_stroll":{
"priority":9,
"speed_multiplier":0.55,
"xz_dist":10,
"y_dist":5
}使实体返回居住边界内(此处指村庄范围)。需要配合下文将解释的minecraft:dweller组件。
BP/entities/custom_villager.json#components
json
"minecraft:behavior.move_towards_dwelling_restriction": {
"priority": 4,
"speed_multiplier": 1.0
}通过创建巡逻路径使实体在村庄周围移动。铁傀儡使用此行为。
BP/entities/custom_villager.json#components
json
"minecraft:behavior.move_through_village": {
"priority": 3,
"speed_multiplier": 0.6,
"only_at_night": true
}允许实体进入建筑并在下雨时避雨。需要开门能力。
BP/entities/custom_villager.json#components
json
"minecraft:behavior.move_indoors":{
"priority":5
}使实体在日落时留在室内。
BP/entities/custom_villager.json#components
json
"minecraft:behavior.restrict_open_door":{
"priority": 5
}需配合以下组件使用:
BP/entities/custom_villager.json#components
json
"minecraft:annotation.open_door":{
"priority": 5
}BP/entities/custom_villager.json#components
json
"minecraft:navigation.walk":{
"can_pass_doors":true,
"can_open_doors":true
}BP/entities/custom_villager.json#components
json
"minecraft:behavior.open_door":{
"priority":6,
"close_door_after":true
}核心行为
BP/entities/custom_villager.json#components
json
"minecraft:dweller": {
"dwelling_type": "village",
"dweller_role": "inhabitant",
"preferred_profession": "farmer",
"update_interval_base": 60,
"update_interval_variant": 40,
"can_find_poi": true,
"can_migrate": true,
"first_founding_reward": 5
}dweller_role: inhabitant允许实体认领床和钟。需要配合minecraft:behavior.sleeppreferred_profession: farmer为minecraft:behavior.work的可选项can_find_poi使实体能够寻找兴趣点。已知POI类型:
bed
jobsite
meeting_areacan_migrate定义实体是否可以在村庄间迁移
睡眠
关于如何让实体睡眠,可参考此文档。
工作
需要设置"dweller_role"为"inhabitant",若未设置"preferred_profession",实体将尝试移动到最近的任意工作站点。
json
"minecraft:behavior.work": {
"priority": 4,
"active_time": 250,
"speed_multiplier": 0.5,
"goal_cooldown": 200,
"sound_delay_min": 100,
"sound_delay_max": 200,
"can_work_in_rain": false,
"work_in_rain_tolerance": 1000,
"on_arrival": {
"event": "minecraft:resupply_trades",
"target": "self"
}
}社交
允许实体进行社交活动。 需要设置"dweller_role"为"inhabitant"。
json
"minecraft:behavior.mingle": {
"priority": 4,
"speed_multiplier": 0.5,
"duration": 30,
"cooldown_time": 10,
"mingle_partner_type": "my:custom_entity",
"mingle_distance": 2.0
}行为调度
现在您已了解所有必要机制,让我们尝试通过"minecraft:scheduler"整合这些功能。 首先创建简单配置。 将工作行为放入work组件组:
json
"component_groups":{
"work_schedule":{
"minecraft:behavior.work":{
"priority":4,
"active_time":250,
"speed_multiplier":0.5,
"goal_cooldown":200,
"sound_delay_min":100,
"sound_delay_max":200,
"can_work_in_rain":true,
"work_in_rain_tolerance":1000,
"on_arrival":{
"event":"minecraft:resupply_trades",
"target":"self"
}
}
},
"gather_schedule":{
"minecraft:behavior.mingle":{
"priority": 5,
"speed_multiplier": 0.8,
"cooldown_time":10.0,
"duration": 30.0,
"mingle_dist": 1.5,
"mingle_partner_type": "my:custom_entity"
}
}
}接着设置实体工作时段。
json
"minecraft:scheduler":{
"min_delay_secs":0,
"max_delay_secs":10,
"scheduled_events":[
{
"filters":{
"all_of":[
{
"test":"hourly_clock_time",
"operator":">=",
"value":0 //清晨
},
{
"test":"hourly_clock_time",
"operator":"<",
"value":12000 //傍晚
}
]
},
"event":"work"
},
{
"filters":{
"all_of":[
{
"test":"hourly_clock_time",
"operator":">=",
"value":21000
},
{
"test":"hourly_clock_time",
"operator":"<",
"value":24000
}
]
},
"event":"gather"
}
]
}事件部分配置如下:
json
"events":{
"work":{
"remove":{
"component_groups":[
"gather_schedule"
]
},
"add":{
"component_groups":[
"work_schedule"
]
}
},
"gather":{
"remove":{
"component_groups":[
"work_schedule"
]
},
"add":{
"component_groups":[
"gather_schedule"
]
}
}
}进入游戏生成实体后放置床,您应该能看到绿色粒子效果。
其他行为
以下行为均可用于自定义实体:
minecraft:behavior.move_to_village掠夺者使用此行为保持停留在村庄内minecraft:behavior.stroll_towards_village狐狸用于寻找并前往村庄minecraft:behavior.inspect_bookshelf图书管理员村民用于查看书架minecraft:behavior.explore_outskirts允许实体探索村庄边界外(需配合调度器和组件组确保实体返回村庄)minecraft:behavior.defend_village_target用于近战攻击。远程攻击可能误伤具有inhabitant居住角色的实体
以下是与村民或村庄相关的可用行为:
| 行为 | 用途 | 备注 |
|---|---|---|
minecraft:behavior.defend_village_target | 允许实体攻击伤害"dweller_role": "inhabitant"实体的其他实体 | 建议仅用于具有近战攻击的实体 |
minecraft:behavior.hide | 村民用于躲藏并停留在指定POI | 当前没有POI类型的文档说明,建议不要修改"poi_type": "bed" |
minecraft:behavior.move_to_village | 灾厄村民和女巫使用。允许实体前往村庄内的随机坐标 | - |
"minecraft:behavior.nap" | 狐狸用于小憩 | 与睡眠类似但更灵活,内置通过检测特定实体唤醒的系统 |






