Milk Bar

Milk Bar 是适用于 Minecraft 26.2 的服务端 Fabric 模组。它添加了一个可配置的生存挑战:每位玩家都有一个私有的“牛奶槽”,在玩家在线时会持续消耗。饮用牛奶可以恢复该槽位。当槽位归零时,玩家将会死亡。
该模组支持单人游戏和多人游戏。所有挑战状态均由服务器权威判定,并使用 Fabric 的持久化数据附加 API(Data Attachment API)按玩家进行存储。
规则
只有当玩家的挑战状态首次初始化时,才会获得配置数量的初始牛奶桶奖励。由于死亡、重生、重新连接、维度切换或服务器重启等原因,此初始奖励不会再次发放。
仅当玩家处于存活且在线状态时,牛奶槽才会消耗。离线时间不会导致牛奶槽消耗。
当牛奶槽归零时,玩家会立即死亡。每次重生时,无论死因如何,牛奶槽都会重置为配置的最大值。不再额外发放初始牛奶。
Boss 栏对每位玩家都是私有的。一名玩家的数值绝不会发送给其他玩家的 Boss 栏。
正常的 Minecraft 牛奶饮用机制保持不变。当喝牛奶动作完成时,一个牛奶桶会恢复配置的量。如果槽位已满且启用了 consumeMilkWhenFull,桶仍会按照原版行为被消耗,但挑战数值不能超过最大值。
如果初始牛奶无法完全装入背包,剩余部分将在玩家所在位置掉落,而不是无声丢失。
命令
所有命令都需要权限等级 2 或更高。
/milkbar status
/milkbar status <player>
/milkbar set <player> <value>
/milkbar refill <player>
/milkbar reload
set 会将数值限制在配置的最大值内。refill 会直接将数值设置为配置的最大值。reload 会重新加载 config/milkbar.json 并刷新活动的 Boss 栏,无需重启服务器。
配置
配置文件位于 config/milkbar.json。
{
"enabled": true,
"maxValue": 100,
"drainAmount": 1,
"drainIntervalTicks": 20,
"milkRestoreAmount": 100,
"starterMilkBuckets": 1,
"giveStarterMilk": true,
"consumeMilkWhenFull": true,
"bossBarVisible": true,
"bossBarTitle": "Milk: %value% / %max%",
"bossBarColor": "GREEN",
"bossBarOverlay": "PROGRESS",
"bossBarDarkenScreen": false,
"bossBarPlayMusic": false,
"bossBarCreateFog": false
}
maxValue 是槽位的最大值,接受 1 到 1,000,000 之间的值。
drainAmount 每隔 drainIntervalTicks 被扣除一次。drainIntervalTicks 使用服务器刻(ticks);20 刻大约等于一秒。
milkRestoreAmount 控制一个牛奶桶能恢复多少数值。恢复量上限为 maxValue。
starterMilkBuckets 控制一次性初始奖励的数量。giveStarterMilk 用于禁用该奖励,但不禁用挑战本身。
consumeMilkWhenFull 控制当槽位已满时是否仍允许正常饮用牛奶。当前实现不会取消原版的物品使用;当此选项为 false 时,挑战数值在已满状态下不会恢复,但仍会执行原版的牛奶饮用逻辑。
bossBarTitle 支持 %value% 和 %max% 占位符。
bossBarColor 支持目标 Minecraft 版本中原版 Boss 栏可用的颜色。bossBarOverlay 支持原版的进度条(progress)和刻度线(notched)覆盖样式。
Boss 栏视觉效果由 bossBarDarkenScreen、bossBarPlayMusic 和 bossBarCreateFog 控制。
无效的数值将被限制在安全范围内。无效的 Boss 栏颜色和覆盖样式将回退至 GREEN 和 PROGRESS。
要求
- Minecraft 26.2
- Fabric Loader 0.19.5 或更高版本
- Fabric API 0.159.0+26.2 或更高版本
- Java 25
构建
./gradlew build
编译后的 JAR 文件将写入 build/libs/。
更新日志
1.0.0
- 修复了针对最新映射表的编译问题:
ServerBossEvent 现在需要显式传入 UUID(每个 Boss 栏使用一个新的随机 ID,与玩家自身的 UUID 无关),CommandSourceStack 权限检查现在使用 LevelBasedPermissionSet/PermissionLevel,并且 LivingEntity#kill 现在需要 ServerLevel 参数。
- Boss 栏更新不再在每个消耗间隔重新应用完整配置(颜色、覆盖样式、可见性、屏幕变暗、音乐、雾效)——现在每次更新仅更改进度和标题。完整配置仅在创建 Boss 栏和
/milkbar reload(这会重建 Boss 栏)时发生。
- Boss 栏颜色和覆盖样式的解析已从 Boss 栏管理器移至
MilkBarConfig.normalize(),因此字符串到枚举的转换(及其回退处理)仅在加载/重载时运行一次,而不是在每次 Boss 栏更新时运行。
- 移除了冗余的每刻状态规范化调用,该调用重复执行了玩家加入和
/milkbar reload 时已完成的工作。
MilkBarCommand 的命令树结构已重构以提高可读性;行为未发生变化。
架构
PlayerMilkState 是持久的按玩家领域状态。该附加数据在服务端重启后保持持久,并复制到重生后的玩家实例中。控制器故意仅在重生时重置当前的槽位数值。
MilkBarController 拥有游戏玩法状态转换、初始奖励、牛奶饮用检测、消耗、死亡处理、重生处理和管理操作。
MilkBossBarManager 拥有私有服务端 Boss 栏及其生命周期。Boss 栏会在重生期间被明确移除并重新创建,以确保新的 ServerPlayer 实例始终接收到正确的 Boss 栏。
MilkBarConfig 验证配置并支持就地重载,以确保控制器和 Boss 栏管理器始终引用相同的配置对象。
该实现不使用客户端代码、Mixin、自定义网络协议或数据库。
许可
Milk Bar 采用 MIT 许可证授权。
Milk Bar

Milk Bar is a server-side Fabric mod for Minecraft 26.2. It adds a configurable survival challenge: every player has a private milk bar that drains while they are online. Drinking milk restores the bar. When it reaches zero, the player dies.
The mod is designed for both singleplayer and multiplayer. All challenge state is server-authoritative and stored per player using Fabric's persistent Data Attachment API.
Rules
A player receives the configured number of starter milk buckets only when their challenge state is initialized for the first time. The starter reward is never granted again because of death, respawn, reconnects, dimension changes, or server restarts.
The milk bar drains only while the player is alive and online. Offline time does not drain the bar.
When the bar reaches zero, the player is killed immediately. On every respawn, regardless of the cause of death, the milk bar is reset to its configured maximum. No additional starter milk is granted.
The boss bar is private to each player. One player's value is never sent to another player's boss bar.
Normal Minecraft milk consumption remains intact. A milk bucket restores the configured amount when its drinking action completes. If the bar is already full and consumeMilkWhenFull is enabled, the bucket is still consumed according to vanilla behavior but the challenge value cannot exceed the maximum.
If starter milk does not fully fit in the inventory, the remainder is dropped at the player's position instead of being silently lost.
Commands
All commands require permission level 2 or higher.
/milkbar status
/milkbar status <player>
/milkbar set <player> <value>
/milkbar refill <player>
/milkbar reload
set clamps the value to the configured maximum. refill sets the value directly to the configured maximum. reload reloads config/milkbar.json and refreshes active boss bars without restarting the server.
Configuration
The file is created at config/milkbar.json.
{
"enabled": true,
"maxValue": 100,
"drainAmount": 1,
"drainIntervalTicks": 20,
"milkRestoreAmount": 100,
"starterMilkBuckets": 1,
"giveStarterMilk": true,
"consumeMilkWhenFull": true,
"bossBarVisible": true,
"bossBarTitle": "Milk: %value% / %max%",
"bossBarColor": "GREEN",
"bossBarOverlay": "PROGRESS",
"bossBarDarkenScreen": false,
"bossBarPlayMusic": false,
"bossBarCreateFog": false
}
maxValue is the maximum bar value and accepts values from 1 to 1,000,000.
drainAmount is removed every drainIntervalTicks. drainIntervalTicks uses server ticks; 20 ticks is approximately one second.
milkRestoreAmount controls how much one milk bucket restores. Restoration is capped at maxValue.
starterMilkBuckets controls the one-time starter reward. giveStarterMilk disables that reward without disabling the challenge itself.
consumeMilkWhenFull controls whether a full bar still allows normal milk consumption. The current implementation does not cancel vanilla item use; when this option is false, the challenge does not restore while already full, but vanilla milk consumption is still performed.
bossBarTitle supports %value% and %max% placeholders.
bossBarColor supports the vanilla boss bar colors available in the target Minecraft version. bossBarOverlay supports the vanilla progress and notched overlays.
Boss bar visual effects are controlled by bossBarDarkenScreen, bossBarPlayMusic, and bossBarCreateFog.
Invalid numeric values are clamped to safe ranges. Invalid boss bar color and overlay values fall back to GREEN and PROGRESS.
Requirements
- Minecraft 26.2
- Fabric Loader 0.19.5 or newer
- Fabric API 0.159.0+26.2 or newer
- Java 25
Build
./gradlew build
The compiled JAR is written to build/libs/.
Changelog
1.0.0
- Fixed compilation against current mappings:
ServerBossEvent now requires an explicit UUID (a fresh random id per bar, independent of the player's own UUID), CommandSourceStack permission checks now use LevelBasedPermissionSet/PermissionLevel, and LivingEntity#kill now requires the ServerLevel argument.
- Boss bar updates no longer re-apply the full configuration (color, overlay, visibility, darken-screen, music, fog) on every drain interval - only progress and title change per update now. Full reconfiguration still happens on bar creation and on
/milkbar reload (which recreates the bar).
- Boss bar color/overlay parsing moved from the boss bar manager into
MilkBarConfig.normalize(), so the string-to-enum conversion (with its fallback handling) runs once per load/reload instead of on every boss bar update.
- Removed a redundant per-tick state normalization call that duplicated work already done on player join and on
/milkbar reload.
MilkBarCommand's command tree was restructured for readability; behavior is unchanged.
Architecture
PlayerMilkState is the persistent per-player domain state. The attachment is persistent across server restarts and copied to the respawned player instance. The controller deliberately resets only the current bar value during respawn.
MilkBarController owns gameplay state transitions, starter rewards, milk consumption detection, draining, death handling, respawn handling, and administration operations.
MilkBossBarManager owns private server boss bars and their lifecycle. Boss bars are explicitly removed and recreated across respawns so the new ServerPlayer instance always receives the correct bar.
MilkBarConfig validates configuration and supports in-place reloads so the controller and boss bar manager always reference the same configuration object.
The implementation does not use client-side code, mixins, custom networking, or a database.
License
Milk Bar is licensed under the MIT License.
评论 0
登录后参与评分
还没有评论,来抢沙发吧