🏛️ 架构解析与 Mixin 注入剖析
📌 代码仓库源码声明:本 Wiki 中的文档反映了代码仓库当前的最新源码状态,可能包含尚未在 CurseForge 和 Modrinth 上发布的开发中功能或最新提交。
🔬 顶层架构与生命周期流转
True Sleep 在服务端拦截原版睡眠生命周期,将瞬间时间跳转替换为受 TimeWarpManager 严格管辖的同步 Tick 加速循环。
[ Player Enters Bed ]
│
▼
[ BedRule.Rule.test() Intercept ]
(Check truesleep:sleep_threshold)
│
▼
[ ServerLevel.tick() Sleep Check ]
(SleepStatus.areEnoughDeepSleeping())
│
+------------+------------+
│ │
[ Not All Sleeping ] [ All Sleeping (Enough) ]
│ │
▼ ▼
[ Initiate Wind-Down ] [ Engage Time Warp Engine ]
(15-tick Deceleration) │
├─► Set Physical Rate (ENGINE_TPS: 50 TPS)
├─► Compute Stride: round(VIRTUAL_TPS / ENGINE_TPS)
├─► Scale RANDOM_TICK_SPEED by Stride
├─► Multi-Tick Machines & Coupled Hoppers
├─► Freeze Mob AI / Age Farm Biology
├─► Apply Sleep Hunger Exhaustion
└─► Smooth Celestial Sun/Moon (Client Lerp)
│
▼
[ Destination Arrival ]
(Cycle Dist < 20 Ticks)
│
▼
[ Snap Monotonic Clock ]
(Set Time to WAKE_TIME: 0)
│
▼
[ Wake Up All Players ]🧩 完整 Mixin 注入点清单与描述符对照表
| Mixin 类 | 目标 Minecraft 原版类 | 注入点位置 | 挂钩方法 | 注入用途与功能说明 |
|---|---|---|---|---|
BedRuleMixin | net.minecraft.world.attribute.BedRule$Rule | @At("HEAD") (Cancellable) | test(Level) | 覆盖原版 WHEN_DARK 入睡时间检查,对接 truesleep:sleep_threshold。 |
ServerLevelMixin | net.minecraft.server.level.ServerLevel | @At("INVOKE") (Redirect) | areEnoughSleeping / areEnoughDeepSleeping | 重定向返回 false,彻底压制原版瞬间跳夜逻辑。 |
ServerLevelMixin | net.minecraft.server.level.ServerLevel | @At("TAIL") | tick(BooleanSupplier) | 侦测入睡条件并驱动 TimeWarpManager.tick() 核心引擎。 |
LevelMixin | net.minecraft.world.level.Level | @At("INVOKE") (Redirect) | TickingBlockEntity.tick() | 对受加速机器与相连漏斗按 stride 跨步执行多重 Tick 推进。 |
MobMixin | net.minecraft.world.entity.Mob | @At("HEAD") (Cancellable) | tick() | 在冻结状态下取消生物移动与 AI,并执行生物学生理时钟演进。 |
EntityMixin | net.minecraft.world.entity.Entity | @At("HEAD") | baseTick() | 激活 drown_immunity 时持续注满氧气,实现水下呼吸停滞。 |
LivingEntityMixin | net.minecraft.world.entity.LivingEntity | @At("HEAD") | baseTick() | 将生效中的药水状态效果持续时间按 stride - 1 刻快速老化。 |
MobEffectInstanceMixin | net.minecraft.world.effect.MobEffectInstance | Interface Injection | truesleep$ageEffect(int) | 递归扣减级联隐藏药水效果时间,杜绝递归死循环异常。 |
VibrationSystemListenerMixin | net.minecraft.world.level.gameevent.vibrations.VibrationSystem$Listener | @At("HEAD") (Cancellable) | handleGameEvent(...) | 在时间加速期间全自动抑制并静音监守者声波震动事件。 |
client.HudMixin | net.minecraft.client.gui.Hud | @At("HEAD") (Cancellable) | extractSleepOverlay(...) | 取消原版就寝时的全屏死黑遮罩画面。 |
client.SkyRendererMixin | net.minecraft.client.renderer.SkyRenderer | @At("TAIL") | extractRenderState(...) | 以 0.35 系数对天体角度执行平滑角插值,呈现电影级夜空。 |
⚡ 零分配热路径缓存技术 (Zero-Allocation)
在超高速时间推进期间($50\text{ 物理 TPS} \times 50\text{ 跨步} = 2{,}500\text{ 次有效迭代/秒}$),实体与方块循环中的内存对象分配极易引发剧烈的垃圾回收 (GC) 停顿。
True Sleep 在所有热路径上全面采用 静态并发无锁缓存结构:
truesleep$unfreezeCache:针对每个EntityType<?>缓存动态 GameRule 实例,彻底消除字符串拼接与哈希查找开销。truesleep$WORKER_CACHE:缓存#truesleep:worker_mobs标签包含性判定结果。truesleep$PRODUCTION_MACHINE_CACHE:缓存#truesleep:accelerated_machines方块实体类型匹配结果。TimeWarpManager字段级规则快照:每物理刻仅获取一次规则布尔状态,而非在成千上万次实体循环内反复查询注册表。
