实体睡眠机制
intermediate
本教程将详细说明如何实现实体的睡眠功能。
在床上睡眠
该行为机制灵感来源于村民。
功能特性
- 实体在夜晚入睡,白天自动醒来
- 与实体互动会将其唤醒,稍后会重新入睡
- 实体受到伤害时会立即醒来
行为包配置
本节将讨论行为包的相关组件配置。
基础组件
首先为实体添加以下基础组件:
BP/entities/sleeping_entity.json#components
json
"minecraft:dweller": {
"dwelling_type": "village",
"dweller_role": "inhabitant",
"can_find_poi": true
}(未在官方文档中记载,这是实体能够睡眠的必要组件)
BP/entities/sleeping_entity.json#components
json
"minecraft:environment_sensor": {
"triggers": [
{
"filters": {
"test": "is_daytime",
"value": false
},
"event": "sleep"
}
]
}该组件使实体能够感知昼夜变化,在非白天时段触发睡眠事件。
WARNING
注意:实体需要具备基本的导航组件才能移动到床边。
组件组配置
接下来配置包含多个组件的组件组:
BP/entities/sleeping_entity.json#component_groups
json
"sleeping": {
"minecraft:behavior.sleep": {
"priority": 0,
"goal_radius": 1.5,
"speed_multiplier": 1.25,
"sleep_collider_height": 0.3,
"sleep_collider_width": 1,
"sleep_y_offset": 0.6,
"timeout_cooldown": 10
},
"minecraft:damage_sensor": {
"triggers": {
"on_damage": {
"event": "wake_up"
}
}
},
"minecraft:environment_sensor": {
"triggers": [
{
"filters": {
"test": "is_daytime",
"value": true
},
"event": "wake_up"
}
]
},
"minecraft:interact": {
"interactions": [
{
"on_interact": {
"filters": {
"all_of": [
{
"test": "is_family",
"subject": "other",
"value": "player"
}
]
},
"event": "woken_up"
}
}
]
}
}minecraft:behavior.sleep:设置睡眠参数,优先级必须设为0(最高权重)minecraft:damage_sensor:使实体受到攻击时醒来minecraft:environment_sensor:白天时触发唤醒事件minecraft:interact:允许玩家在不伤害实体的情况下将其唤醒
BP/entities/sleeping_entity.json#component_groups
json
"sleep_timer": {
"minecraft:timer": {
"time": 15,
"time_down_event": {
"event": "sleep_again"
}
}
}该组件组实现实体被唤醒后延迟重新入睡的功能。
事件配置
以下是需要配置的所有事件:
BP/entities/sleeping_entity.json#events
json
"sleep": {
"add": {
"component_groups": [
"sleeping"
]
}
},
"wake_up": {
"remove": {
"component_groups": [
"sleeping"
]
}
},
"woken_up": {
"remove": {
"component_groups": [
"sleeping"
]
},
"add": {
"component_groups": [
"sleep_timer"
]
}
},
"sleep_again": {
"add": {
"component_groups": [
"sleeping"
]
},
"remove": {
"component_groups": [
"sleep_timer"
]
}
}资源包配置
别忘了为实体添加睡眠动画和对应的控制器!
动画配置
直接复制以下内容:
RP/animations/sleeping_entity.animation.json
json
{
"format_version": "1.8.0",
"animations": {
"animation.sleeping_entity.sleep": {
"loop": "hold_on_last_frame",
"animation_length": 0.5,
"bones": {
"body": {
"rotation": {
"0.0": [0, 0, 0],
"0.5": [-90, 0, 0]
},
"position": [0, 2, -15]
}
}
}
}
}动画控制器
同样可以直接复制:
RP/animations_controllers/ac.sleeping_entity.sleep.json
json
{
"format_version": "1.10.0",
"animation_controllers": {
"controller.animation.sleeping_entity.sleep": {
"initial_state": "default",
"states": {
"default": {
"transitions": [
{
"sleep": "q.is_sleeping"
}
]
},
"sleep": {
"animations": ["sleeping"],
"transitions": [
{
"default": "!q.is_sleeping"
}
]
}
}
}
}
}注意:需要在客户端实体定义中添加动画引用: "sleeping": "animation.sleeping_entity.sleep"
效果展示

小憩行为
该行为机制灵感来源于狐狸。
功能特性
- 当实体感到安全(远离敌对生物且非雷暴天气)时会小憩
- 除非是受信任/潜行的玩家或同属
sleeping_entity家族的实体靠近,否则会被惊醒 - 受到伤害时会立即醒来
行为包配置
基础组件
只需配置一个组件:
BP/entities/sleeping_entity.json#components
json
"minecraft:behavior.nap": {
"priority": 8,
"cooldown_min": 2.0,
"cooldown_max": 7.0,
"mob_detect_dist": 12.0,
"mob_detect_height": 6.0,
"can_nap_filters": {
"all_of": [
{
"test": "in_water",
"subject": "self",
"operator": "==",
"value": false
},
{
"test": "on_ground",
"subject": "self",
"operator": "==",
"value": true
},
{
"test": "is_underground",
"subject": "self",
"operator": "==",
"value": true
},
{
"test": "weather_at_position",
"subject": "self",
"operator": "!=",
"value": "thunderstorm"
}
]
},
"wake_mob_exceptions": {
"any_of": [
{
"test": "trusts",
"subject": "other",
"operator": "==",
"value": true
},
{
"test": "is_family",
"subject": "other",
"operator": "==",
"value": "sleeping_entity"
},
{
"test": "is_sneaking",
"subject": "other",
"operator": "==",
"value": true
}
]
}
}如需使用信任机制,还需添加:
BP/entities/sleeping_entity.json#components
json
"minecraft:trust": {}资源包配置
可以在资源包中配置实体入睡时的动画:
RP/animations_controllers/ac.sleeping_entity.sleep.json
json
{
"format_version": "1.10.0",
"animation_controllers": {
"controller.animation.sleeping_entity.sleep": {
"initial_state": "default",
"states": {
"default": {
"transitions": [
{
"sleep": "q.is_sleeping"
}
]
},
"sleep": {
"animations": ["sleeping"],
"transitions": [
{
"default": "!q.is_sleeping"
}
]
}
}
}
}
}最后需要为实体创建并注册睡眠动画。如需了解具体操作,请参考BlockBench指南。

