Hudifine

模组

· 作者 realpuffle

使用简单的脚本语言构建自定义 HUD 覆盖层。无需模组编程知识。

0 本站下载 0 关注 更新于 5 个月前

最新 2.0.1 · 26.2 · Fabric

一键开服(送7天)

Hudifine 是一款 HUD 模组,允许任何人通过 HUD Script(HUD 脚本)轻松制作 HUD,以显示你选择的各项指标。

打开聊天栏即可添加和配置它们。

以下是 Hudifine 的脚本语言 HUDScript 的文档。

HUDScript 文档

HUD Script 开发者文档

HUD Script 是一种安全、沙盒化的脚本格式,用于在 Minecraft(Fabric)中创建完全可定制的游戏内 HUD 覆盖层。脚本即贴即用;无需模组开发环境。


目录

  1. 概述
  2. 脚本结构
  3. 屏幕预算(30% 规则)
  4. 数据源
    • 玩家 (Player)
    • 世界 (World)
    • 性能 (Performance)
    • 战斗 (Combat)
    • 物品栏 (Inventory)
    • 输入 (Input)
    • 环境 (Environment)
    • 游戏状态 (Game State)
    • 多人游戏 (Multiplayer)
    • 时间 (Time)
  5. 布局系统
    • 排序与层级
  6. 样式
  7. 元素
  8. 条件判断与逻辑
  9. 事件
  10. 无障碍切换
  11. 玩家设置 API
  12. 动画
  13. CPS 实施指南
  14. 完整示例

概述

HUD Script 是一种声明式 + 事件驱动的脚本格式。脚本定义:

  • 要显示哪些数据(来自一组固定的允许数据源)
  • 如何显示这些数据(在屏幕预算范围内拥有完全的视觉自由)
  • 玩家可配置的设置(无障碍选项、开关、颜色等)

脚本设计上采用沙盒机制。它们只能:

  • ✅ 读取允许的数据源
  • ✅ 渲染到 HUD 覆盖层
  • ✅ 对玩家输入做出视觉反应
  • ❌ 发起网络请求
  • ❌ 访问文件系统
  • ❌ 与组件之外的任何事物交互

脚本结构

每个脚本包含三个可选的顶层块:

meta {
  name: "My Widget"
  author: "yourname"
  version: "1.0.0"
  description: "A short description of what this widget does."
}

settings {
  // 右键菜单中暴露的玩家可配置选项
}

widget {
  // 布局、元素、样式、逻辑
}

只有 widget 块是必需的。


屏幕预算(30% 规则)

组件在任何时候占据的面积不得超过玩家屏幕面积的 30%。

  • 屏幕面积计算为 screenWidth × screenHeight
  • 你的组件的边界框(所有元素的组合)不得超过 screenWidth × screenHeight × 0.30
  • 此限制在运行时强制执行;如果你的组件超过 30%,它将被自动缩放,并在组件编辑器中出现警告
  • 已关闭或完全透明的组件不计入预算
  • 多个组件各自拥有独立的 30% 预算

提示: 明智地使用 size 属性,并在不同分辨率下进行测试。将 1920×1080 作为你的设计基准。


数据源

所有数据源均为只读。在元素值中使用 get() 调用它们。

玩家 (Player)

数据源 返回值 描述
player.health float 当前生命值 (0.0-20.0)
player.maxHealth float 最大生命值(包括吸收伤害)
player.absorption float 吸收心数量
player.hunger int 饥饿值 (0-20)
player.saturation float 饱和度等级 (0.0-20.0)
player.exhaustion float 疲劳值 (0.0-4.0)
player.air int 氧气剩余刻数 (0-300)
player.xp int 总经验点数
player.xpLevel int 当前经验等级
player.xpProgress float 下一级经验的进度 (0.0-1.0)
player.speed float 当前移动速度(方块/秒)
player.isSprinting bool 玩家是否正在疾跑
player.isSneaking bool 玩家是否正在潜行
player.isSwimming bool 玩家是否正在游泳
player.isFlying bool 玩家是否正在飞行(创造模式/鞘翅)
player.isFalling bool 玩家是否正在掉落
player.isOnGround bool 玩家是否在地面上
player.isInWater bool 玩家是否在水中
player.isInLava bool 玩家是否在岩浆中
player.fallDistance float 当前掉落距离(方块数)
player.reachDistance float 当前攻击距离
player.name string 玩家的用户名
player.uuid string 玩家 UUID
player.gamemode string survival(生存)、creative(创造)、adventure(冒险)、spectator(旁观者)
player.score int 玩家的当前计分板分数
player.ping int 延迟(毫秒),仅限多人游戏

世界

来源 返回值 描述
world.x float 玩家 X 坐标
world.y float 玩家 Y 坐标
world.z float 玩家 Z 坐标
world.blockX int 玩家所在方块 X 坐标(向下取整)
world.blockY int 玩家所在方块 Y 坐标(向下取整)
world.blockZ int 玩家所在方块 Z 坐标(向下取整)
world.chunkX int 当前区块 X 坐标
world.chunkZ int 当前区块 Z 坐标
world.facing string 基本朝向:N、NE、E、SE、S、SW、W、NW
world.facingDegrees float 偏航角(角度,0-360)
world.pitch float 俯仰角(-90 到 90)
world.yaw float 偏航角(0-360)
world.dimension string 维度:overworld、nether、end 或自定义命名空间
world.biome string 当前生物群系名称(例如 minecraft:plains)
world.lightLevel int 玩家脚部位置的方块光照等级(0-15)
world.skyLightLevel int 玩家位置的天空光照等级(0-15)
world.moonPhase int 月相(0-7)
world.isRaining bool 是否正在下雨
world.isThundering bool 是否有雷暴天气
world.rainStrength float 降雨强度(0.0-1.0)
world.difficulty string 难度:peaceful、easy、normal、hard
world.name string 世界/服务器名称
world.seed string 世界种子(仅单人游戏有效;在服务器上返回 "hidden")
world.spawnX int 世界重生点 X 坐标
world.spawnZ int 世界重生点 Z 坐标
world.distanceToSpawn float 到世界重生点的距离(以方块为单位)

性能

来源 返回值 描述
perf.fps int 当前每秒帧数 (FPS)
perf.fpsAvg float 过去 5 秒内的平均 FPS
perf.fpsMin int 过去 5 秒内的最低 FPS
perf.fpsMax int 过去 5 秒内的最高 FPS
perf.frameTime float 上一帧耗时(毫秒)
perf.frameTimeAvg float 过去 5 秒内的平均帧耗时
perf.tps float 服务器每秒刻数(仅多人游戏)
perf.mspt float 每刻所需毫秒数(服务器端,仅多人游戏)
perf.chunkUpdates int 当前帧的区块更新次数
perf.renderedChunks int 已渲染的区块数量
perf.entities int 渲染距离内的实体数量
perf.blockEntities int 渲染距离内的方块实体数量
perf.particles int 活跃的粒子数量

战斗

来源 返回值 描述
combat.attackCooldown float 攻击冷却进度(0.0-1.0)
combat.attackCooldownMs int 距离完全攻击冷却所需的毫秒数
combat.isAttacking bool 玩家是否正在攻击
combat.lastDamage float 上次受到的伤害值
combat.lastDamageSource string 上次伤害的来源(例如 fire、fall、player)
combat.killCount int 本次会话中的击杀数
combat.deathCount int 本次会话中的死亡次数
combat.kdr float 本次会话的击杀/死亡比率
combat.targetName string 玩家注视实体的名称(如有)
combat.targetHealth float 玩家注视实体的生命值
combat.targetMaxHealth float 玩家注视实体的最大生命值
combat.targetDistance float 到目标实体的距离
combat.targetType string 玩家注视实体的类型
combat.isInCombat bool 玩家最近是否受到攻击或攻击了其他实体
combat.combatTimer int 距离上次战斗动作经过的秒数

物品栏

来源 返回值 描述
inventory.hotbarSlot int 当前快捷栏槽位(0-8)
inventory.mainHandItem string 主手物品 ID
inventory.mainHandCount int 主手中物品的堆叠数量
inventory.mainHandDurability int 主手物品的耐久度
inventory.mainHandMaxDurability int 主手物品的最大耐久度
inventory.mainHandDurabilityPct float 主手物品的耐久百分比(0.0-1.0)
inventory.offHandItem string 副手物品 ID
inventory.offHandCount int 副手中物品的堆叠数量
inventory.offHandDurability int 副手物品的耐久度
inventory.helmetItem string 头盔的物品 ID
inventory.helmetDurability int 头盔的耐久度
inventory.helmetDurabilityPct float 头盔的耐久百分比(0.0-1.0)
inventory.chestplateItem string 胸甲的物品 ID
inventory.chestplateDurability int 胸甲的耐久度
inventory.chestplateDurabilityPct float 胸甲的耐久百分比(0.0-1.0)
inventory.leggingsItem string 护腿的物品 ID
inventory.legginsDurability int 护腿的耐久度
inventory.leggingsDurabilityPct float 护腿的耐久百分比(0.0-1.0)
inventory.bootsItem string 靴子的物品 ID
inventory.bootsDurability int 靴子的耐久度
inventory.bootsDurabilityPct float 靴子的耐久百分比(0.0-1.0)
inventory.arrowCount int 物品栏中箭的数量
inventory.totalSlots int 总物品栏槽位数(36)
inventory.usedSlots int 已占用的物品栏槽位数
inventory.emptySlots int 空的物品栏槽位数
inventory.xpBottleCount int 物品栏中的经验瓶数量

输入

输入源反映的是玩家物理上按下的状态。它们仅用于显示;您无法拦截、阻止或重定向输入。

来源 返回值 描述
input.forward bool W 键按住
input.backward bool S 键按住
input.left bool A 键按住
input.right bool D 键按住
input.jump bool 空格键按住
input.sneak bool Shift 键按住
input.sprint bool 疾跑键按住
input.attack bool 鼠标左键按住
input.use bool 鼠标右键按住
input.drop bool Q 键按住
input.inventory bool 物品栏键按住
input.swap bool F 键(切换手持)按住
input.hotbar1-input.hotbar9 bool 快捷栏按键按住
input.cps int 左键点击频率(每秒点击次数)
input.rcps int 右键点击频率
input.mouseX int 屏幕上鼠标 X 坐标
input.mouseY int 屏幕上鼠标 Y 坐标
input.mouseDeltaX float 当前帧鼠标 X 移动量
input.mouseDeltaY float 当前帧鼠标 Y 移动量
input.sensitivity float 当前鼠标灵敏度 (0.0-1.0)

环境信息

来源 返回值 描述
env.timeOfDay int 世界刻数 (0-24000)
env.timeString string 格式化时间 (例如 6:00 AM)
env.dayCount int 游戏内经过的天数
env.temperature float 玩家所在位置的生物群系温度
env.humidity float 玩家所在位置的生物群系湿度
env.canSeeSky bool 玩家是否能看到天空
env.isUnderground bool 玩家是否处于地下(无法看到天空)
env.nearestVillageDistance float 到最近检测到的村庄的距离(方块单位)
env.isInVillage bool 玩家是否在村庄内部
env.nearestPlayerDistance float 到最近的其他玩家的距离(多人游戏)
env.nearestPlayerName string 最近的其他玩家名称

游戏状态

来源 返回值 描述
game.isPaused bool 游戏是否暂停
game.isInGui bool 是否有 GUI 打开
game.isInventoryOpen bool 物品栏是否打开
game.isInBed bool 玩家是否在睡觉
game.isRiding bool 玩家是否在骑乘实体
game.ridingEntityType string 正在骑乘的实体类型
game.ridingEntityHealth float 正在骑乘的实体的生命值
game.isElytraFlying bool 玩家是否在鞘翅飞行
game.elytraHealth int 当前鞘翅耐久度
game.elytraSpeed float 当前鞘翅速度(方块/秒)
game.potionEffects list<string> 激活的药水效果 ID
game.potionEffectAmplifiers map<string, int> 每个激活效果的等级增幅
game.potionEffectDurations map<string, int> 每个激活效果的持续刻数
game.scoreboardObjective string 当前侧边计分板目标名称
game.scoreboardScore int 玩家在当前侧边目标上的得分
game.bossBarName string 激活的Boss条名称(如果有)
game.bossBarProgress float Boss条进度 (0.0-1.0)
game.isSpectatingEntity bool 玩家是否正在旁观实体
game.spectatingEntityType string 正在旁观的实体类型

多人游戏

来源 返回值 描述
server.name string 服务器名称 / IP
server.motd string 服务器 MOTD
server.playerCount int 当前在线玩家数量
server.maxPlayers int 服务器最大玩家容量
server.ping int 到服务器的延迟(毫秒)
server.tps float 服务器 TPS
server.isLAN bool 是否连接到局域网服务器
server.isSingleplayer bool 是否在单人游戏中
server.version string 服务器 Minecraft 版本

时间

来源 返回值 描述
clock.hour int 现实时间小时 (0-23)
clock.minute int 现实时间分钟 (0-59)
clock.second int 现实时间秒 (0-59)
clock.timeString string 格式化现实时间 (例如 14:32)
clock.timeString12 string 12小时制格式 (例如 2:32 PM)
clock.sessionTime int 当前游戏会话已进行的秒数
clock.sessionTimeString string 格式化会话时间 (例如 1h 23m)
clock.date string 现实日期 (例如 2025-04-26)
clock.unixTimestamp int Unix 时间戳(秒)

布局系统

小部件使用基于盒子的简单布局,锚定到屏幕位置。

锚点

widget {
  anchor: top-left       // top-left, top-center, top-right
                         // middle-left, center, middle-right
                         // bottom-left, bottom-center, bottom-right
  offsetX: 10            // 从锚点 X 的像素偏移(可为负数)
  offsetY: 10            // 从锚点 Y 的像素偏移(可为负数)
}

尺寸

widget {
  width: 120             // 固定像素宽度
  height: auto           // "auto" 根据内容调整大小,或固定像素
  padding: 8             // 均匀内边距(或使用 padding: 8 4 分别设置垂直/水平内边距)
  margin: 4              // 外边距
}

方向

widget {
  direction: column      // "column"(垂直堆叠)或 "row"(水平堆叠)
  gap: 4                 // 子元素之间的间隙(像素)
  align: start           // "start", "center", "end" - 交叉轴对齐
  justify: start         // "start", "center", "end", "space-between" - 主轴对齐
}

Z-Index

widget {
  zIndex: 10             // 较高的值使小部件渲染在其他小部件之上
}

排序与分层

默认情况下,元素按照它们在脚本中编写的顺序从上到下渲染。你可以使用任何元素或组上的 order 属性来覆盖此行为。

widget {
  direction: column

  text { value: "I appear second" order: 2 color: #ffffff fontSize: 12 }
  text { value: "I appear first"  order: 1 color: #aaaaaa fontSize: 10 }
  text { value: "I appear third"  order: 3 color: #555555 fontSize: 9 }
}

order 的工作原理类似于 CSS 中的 order;数值越小渲染越靠前(根据 direction 的不同,位置更靠上或更靠左)。没有设置 order 值的元素默认为 order: 0,并会在显式设置了 order 的元素之前渲染。

你也可以在 group 内部使用 order:

group {
  direction: row

  text { value: "CPS" order: 2 color: #aaaaaa fontSize: 9 }
  keyIndicator { key: input.attack label: "LMB" order: 1 size: 20 borderRadius: 8 activeColor: #ffffff inactiveColor: #ffffff33 }
}

提示: 当你希望脚本逻辑保持在一起但视觉布局不同时,请使用 order。如果所有元素都设置了显式的 order,那么直接在正确的顺序下重写脚本会更简洁。


样式

背景

widget {
  background: #00000088        // 十六进制颜色,可选最后两位表示透明度 (alpha)
  background: transparent      // 无背景
  borderRadius: 6              // 像素单位的圆角半径
  border: 1 #ffffff44          // 边框:宽度 颜色
  backdropBlur: 4              //  widget 后方的模糊像素数(取决于 GPU)
}

文本样式

text {
  value: get(player.health)
  color: #ffffff
  fontSize: 12               // 单位为像素(推荐 8-32)
  fontWeight: bold           // "normal", "bold"
  fontStyle: normal          // "normal", "italic"
  shadow: true               // Minecraft 风格的文本阴影
  shadowColor: #00000066
  letterSpacing: 0           // 字符之间的像素间距
  lineHeight: 1.2            // 行高倍数
  align: left                // "left", "center", "right"
  truncate: 20               // 截断前的最大字符数(可选)
  uppercase: false           // 强制大写
}

颜色

颜色为十六进制字符串:#RRGGBB 或 #RRGGBBAA(带 alpha 透明度)。

color: #ff0000             // 红色,完全不透明
color: #ff000088           // 红色,50% 透明
color: #fff                // 简写形式(扩展为 #ffffff)

不透明度

widget {
  opacity: 0.8             // 0.0(不可见)到 1.0(完全不透明)
}

元素

元素是 widget 块内的视觉构建块。

text

显示字符串值。

text {
  value: "FPS: {get(perf.fps)}"    // 内联表达式使用 {}
  color: #ffffff
  fontSize: 12
  shadow: true
}

bar

填充条(用于生命值、经验值、冷却时间等)。

bar {
  value: get(player.health)
  max: get(player.maxHealth)
  width: 100
  height: 8
  fillColor: #ff4444
  backgroundColor: #00000066
  borderRadius: 4
  direction: left-to-right    // "left-to-right", "right-to-left", "bottom-to-top", "top-to-bottom"
}

icon

渲染 Minecraft 物品或方块的图标。

icon {
  item: get(inventory.mainHandItem)    // 物品 ID 字符串
  size: 16                             // 图标大小(像素)
}

circle

圆形进度指示器。

circle {
  value: get(combat.attackCooldown)    // 0.0-1.0
  radius: 12
  strokeWidth: 3
  color: #ffffff
  backgroundColor: #ffffff33
  startAngle: -90                      // 角度 - 0 = 右侧, -90 = 顶部
  direction: clockwise                 // "clockwise" 或 "counterclockwise"
}

image

从资源包中渲染静态纹理。

image {
  texture: "hud:textures/my_icon.png"    // 资源包内的命名空间:路径
  width: 16
  height: 16
  tint: #ffffff                          // 可选的颜色 tint
}

keyIndicator

显示一个按下时发光的可视化按键。

keyIndicator {
  key: input.forward
  label: "W"
  activeColor: #ffffff
  inactiveColor: #ffffff44
  size: 20              // 按键的宽度和高度(像素)
  width: 20             // 仅覆盖宽度(对 SPACE, LMB, RMB 很有用)
  borderRadius: 8       // 数值越大越圆润。使用 999 可获得完整的药丸形状
  fontSize: 9           // 按键内标签的字体大小
  shadow: true          // 标签的文本阴影
}

提示: 对于像 SPACE、LMB 和 RMB 这样的宽键,将 width 设置为大于 size 的值,可以在保持高度不变的情况下将其水平拉伸。borderRadius 设为 8-12 可匹配 Lunar Client 的外观。

group

用于将元素分组和嵌套的容器。

group {
  direction: row
  gap: 4
  background: #00000066
  padding: 6
  borderRadius: 4

  text { value: "Health: " color: #aaaaaa fontSize: 11 }
  text { value: "{get(player.health)}" color: #ff4444 fontSize: 11 }
}

separator

水平或垂直的分隔线。

separator {
  direction: horizontal    // "horizontal" 或 "vertical"
  color: #ffffff22
  thickness: 1
  length: 80               // 像素,或使用 "auto" 填满可用空间
  margin: 4
}

spacer

元素之间的空白空间。

spacer {
  size: 8
}

条件与逻辑

if / else

根据条件显示或隐藏元素。

if get(player.health) < 6 {
  text {
    value: "⚠ LOW HEALTH"
    color: #ff0000
    fontSize: 14
    shadow: true
  }
} else if get(player.health) < 10 {
  text {
    value: "Health is getting low"
    color: #ffaa00
    fontSize: 12
  }
} else {
  // 无操作
}

内联表达式

在字符串值中使用 {} 进行内联表达式:

text { value: "You are in the {get(world.dimension)}" }
text { value: "XP Level: {get(player.xpLevel)} ({round(get(player.xpProgress) * 100)}%)" }

数学辅助函数

函数 描述
round(x) 四舍五入到最接近的整数
floor(x) 向下取整
ceil(x) 向上取整
abs(x) 绝对值
min(x, y) 两个值中的最小值
max(x, y) 两个值中的最大值
clamp(x, min, max) 将值限制在 min 和 max 之间
lerp(a, b, t) 线性插值
pct(value, max) 返回 (value / max) * 100
format(x, decimals) 将数字格式化为 N 位小数

字符串辅助函数

函数 描述
upper(s) 转换为大写字符串
lower(s) 转换为小写字符串
concat(a, b) 拼接两个字符串
trim(s, n) 截取前 N 个字符的字符串
replace(s, from, to) 替换子字符串

比较运算符

==, !=, <, >, <=, >=, &&, ||, !

if get(world.dimension) == "nether" && get(player.health) < 10 {
  text { value: "危险!" color: #ff0000 }
}

事件

响应事件以在视觉上更新小部件。

on keypress(input.attack) {
  // 当按键首次按下时触发一次
  text {
    value: "正在攻击!"
    color: #ff4444
    fadeOut: 500    // 500毫秒后淡出
  }
}

on keyhold(input.sneak) {
  // 在按住按键期间每个游戏刻触发一次
}

on keyrelease(input.jump) {
  // 当按键释放时触发一次
}

on event(player.health < 5) {
  // 当条件变为真时触发
  // 如果条件先变假再变真,则会重新触发
}

on tick {
  // 每个游戏刻运行一次(每秒 20 次)
}

on frame {
  // 每个渲染帧运行一次
}

无障碍开关

开发人员可以为有特殊无障碍需求的玩家提供开关和设置选项。这些选项会出现在小部件的右键点击设置面板中。

settings {
  toggle "高对比度模式" {
    id: highContrast
    default: false
    description: "增加颜色对比度以提高可见性。"
  }

  toggle "减少动态效果" {
    id: reduceMotion
    default: false
    description: "禁用动画和过渡效果。"
  }

  toggle "大号文本" {
    id: largeText
    default: false
    description: "将所有文本大小增加 1.5 倍。"
  }

  toggle "色盲模式" {
    id: colorblindMode
    default: false
    description: "用形状/图案替换基于颜色的指示器。"
  }

  toggle "屏幕阅读器提示" {
    id: screenReaderHints
    default: false
    description: "在仅包含图标的元素上显示额外的文本标签。"
  }
}

然后在你的小部件中使用这些设置值:

widget {
  text {
    value: "HP: {get(player.health)}"
    color: if setting(highContrast) then #ffff00 else #ffffff
    fontSize: if setting(largeText) then 18 else 12
    shadow: true
  }
}

玩家设置 API

除了无障碍开关外,你还可以暴露完全由玩家自定义的配置设置。

设置类型

settings {

  // 颜色选择器
  color "生命值条颜色" {
    id: healthColor
    default: #ff4444
    description: "生命值条填充的颜色。"
  }

  // 滑块(数值范围)
  slider "小部件不透明度" {
    id: widgetOpacity
    min: 0.1
    max: 1.0
    step: 0.05
    default: 0.85
    description: "小部件背景的透明度。"
  }

  // 下拉选择
  select "显示模式" {
    id: displayMode
    options: ["紧凑", "正常", "扩展"]
    default: "normal"
    description: "显示多少信息。"
  }

  // 文本输入(仅显示标签)
  text "自定义标签" {
    id: customLabel
    default: "我的小部件"
    maxLength: 20
    description: "顶部显示的自定义标题。"
  }

  // 开关(布尔值)
  toggle "显示小部件" {
    id: showWidget
    default: true
    description: "开启或关闭整个小部件。"
  }

  // 键绑定(仅显示输入 - 显示按下了哪个键)
  keybind "高亮按键" {
    id: highlightKey
    default: input.forward
    description: "在按键显示中高亮的按键。"
  }

}

在小部件中使用设置

widget {
  opacity: setting(widgetOpacity)
  visible: setting(showWidget)

  text {
    value: setting(customLabel)
    fontSize: 11
    color: #aaaaaa
  }

  bar {
    value: get(player.health)
    max: get(player.maxHealth)
    fillColor: setting(healthColor)
    width: 100
    height: 8
  }
}

动画

当玩家启用了 reduceMotion 时,动画会自动禁用(如果你定义了该开关)。在使用注重无障碍的小部件动画之前,请务必进行检查。

text {
  value: "⚠ 低生命值"
  color: #ff0000

  animate {
    property: opacity
    from: 1.0
    to: 0.3
    duration: 600         // 毫秒
    easing: ease-in-out   // "linear", "ease-in", "ease-out", "ease-in-out"
    loop: true
    pingpong: true        // 每次循环反向播放
    paused: setting(reduceMotion)
  }
}

过渡(状态变化时)

text {
  value: "{get(perf.fps)}"
  color: #ffffff

  transition {
    property: color
    duration: 200
    easing: ease-out
  }
}

CPS 实现指南

本节面向在 Java 端实现 input.cps 和 input.rcps 数据源的 Hudifine 模组开发者。

CPS(每秒点击次数)不是 Minecraft 原生跟踪的值。你需要使用滑动时间窗口来实现一个点击计数器。

工作原理

记录每次左键单击(攻击)和右键单击(使用)的时间戳。要获取当前的 CPS,请计算过去 1000ms 内发生了多少次点击。

推荐的 Java 实现(Fabric + Mixin)

步骤 1 - 存储点击时间戳

// 在你的 HudifineDataProvider 或等效类中
private static final List<Long> leftClickTimes = new ArrayList<>();
private static final List<Long> rightClickTimes = new ArrayList<>();

public static void registerLeftClick() {
    long now = System.currentTimeMillis();
    leftClickTimes.add(now);
    // 移除超过 1 秒前的旧条目
    leftClickTimes.removeIf(t -> now - t > 1000);
}
public static void registerRightClick() {
    long now = System.currentTimeMillis();
    rightClickTimes.add(now);
    rightClickTimes.removeIf(t -> now - t > 1000);
}

public static int getLeftCPS() {
    long now = System.currentTimeMillis();
    leftClickTimes.removeIf(t -> now - t > 1000);
    return leftClickTimes.size();
}

public static int getRightCPS() {
    long now = System.currentTimeMillis();
    rightClickTimes.removeIf(t -> now - t > 1000);
    return rightClickTimes.size();
}

步骤 2 - 通过 Mixin 挂钩鼠标点击事件

@Mixin(MinecraftClient.class)
public class MouseClickMixin {

    @Inject(method = "doAttack", at = @At("HEAD"))
    private void onLeftClick(CallbackInfo ci) {
        HudifineDataProvider.registerLeftClick();
    }

    @Inject(method = "doItemUse", at = @At("HEAD"))
    private void onRightClick(CallbackInfo ci) {
        HudifineDataProvider.registerRightClick();
    }
}

步骤 3 - 向脚本引擎暴露接口

在数据源解析器中,将 input.cps 映射到 HudifineDataProvider.getLeftCPS(),将 input.rcps 映射到 HudifineDataProvider.getRightCPS()。

注意事项

  • 这 1000 毫秒的滑动窗口是 Lunar Client、Badlion 以及大多数 PvP 客户端所使用的标准
  • doAttack 会在每次挥击尝试时触发,包括未命中情况。这与 Lunar 计算 CPS 的方式一致
  • doItemUse 无论手持何物,都会在右键点击时触发,这也符合预期行为
  • 两个列表都在读取时被修剪(pruned),因此在长时间运行期间也能保持较小的内存占用

完整示例

最小化 FPS 计数器

meta {
  name: "FPS Counter"
  author: "example"
  version: "1.0.0"
}

widget {
  anchor: top-right
  offsetX: -10
  offsetY: 10
  background: #00000066
  padding: 6
  borderRadius: 4

  text {
    value: "{get(perf.fps)} FPS"
    color: #ffffff
    fontSize: 12
    shadow: true
  }
}

按键显示

meta {
  name: "Keystrokes"
  author: "example"
  version: "1.0.0"
}

settings {
  color "Active Key Color" { id: activeColor default: #ffffff }
  color "Inactive Key Color" { id: inactiveColor default: #ffffff33 }
  toggle "Show CPS" { id: showCps default: true }
}

widget {
  anchor: bottom-right
  offsetX: -10
  offsetY: -10
  direction: column
  gap: 3

  group {
    direction: row
    justify: center
    gap: 3

    keyIndicator { key: input.forward label: "W" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 borderRadius: 8 }
  }

  group {
    direction: row
    gap: 3

    keyIndicator { key: input.left label: "A" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 borderRadius: 8 }
    keyIndicator { key: input.backward label: "S" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 borderRadius: 8 }
    keyIndicator { key: input.right label: "D" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 borderRadius: 8 }
  }

  group {
    direction: row
    justify: center

    keyIndicator { key: input.jump label: "SPACE" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 width: 66 borderRadius: 8 }
  }

  group {
    direction: row
    gap: 3

    keyIndicator { key: input.attack label: "LMB" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 width: 40 borderRadius: 8 }
    keyIndicator { key: input.use label: "RMB" activeColor: setting(activeColor) inactiveColor: setting(inactiveColor) size: 20 width: 40 borderRadius: 8 }
  }

  if setting(showCps) {
    group {
      direction: row
      gap: 6
      justify: center

      text { value: "{get(input.cps)} CPS" color: #aaaaaa fontSize: 10 }
      text { value: "{get(input.rcps)} CPS" color: #aaaaaa fontSize: 10 }
    }
  }
}

完整状态 HUD

meta {
  name: "Status HUD"
  author: "example"
  version: "1.0.0"
  description: "Health, hunger, armor, XP and coordinates."
}

settings {
  toggle "Show Coordinates" { id: showCoords default: true }
  toggle "Show Armor" { id: showArmor default: true }
  toggle "Show XP" { id: showXp default: true }
  toggle "High Contrast Mode" { id: highContrast default: false description: "Higher contrast colors." }
  toggle "Large Text" { id: largeText default: false }
  color "Health Color" { id: healthColor default: #ff4444 }
  slider "Opacity" { id: opacity min: 0.2 max: 1.0 step: 0.05 default: 0.85 }
}

widget {
  anchor: bottom-left
  offsetX: 10
  offsetY: -60
  background: #00000066
  padding: 8
  borderRadius: 6
  opacity: setting(opacity)
  direction: column
  gap: 5

  // Health
  group {
    direction: row
    gap: 6
    align: center

    text { value: "❤" color: setting(healthColor) fontSize: if setting(largeText) then 16 else 11 }
    bar {
      value: get(player.health)
      max: get(player.maxHealth)
      width: 80
      height: if setting(largeText) then 10 else 7
      fillColor: setting(healthColor)
      backgroundColor: #ffffff22
      borderRadius: 3
    }
    text {
      value: "{floor(get(player.health))}/{floor(get(player.maxHealth))}"
      color: if setting(highContrast) then #ffff00 else #ffffff
      fontSize: if setting(largeText) then 14 else 10
    }
  }

  // Hunger
  group {
    direction: row
    gap: 6
    align: center

    text { value: "?" color: #ffaa44 fontSize: if setting(largeText) then 16 else 11 }
    bar {
      value: get(player.hunger)
      max: 20
      width: 80
      height: if setting(largeText) then 10 else 7
      fillColor: #ffaa44
      backgroundColor: #ffffff22
      borderRadius: 3
    }
    text {
      value: "{get(player.hunger)}/20"
      color: if setting(highContrast) then #ffff00 else #ffffff
      fontSize: if setting(largeText) then 14 else 10
    }
  }

  // XP
  if setting(showXp) {
    group {
      direction: row
      gap: 6
      align: center

text { value: "✦" color: #55ff55 fontSize: if setting(largeText) then 16 else 11 }
      bar {
        value: get(player.xpProgress)
        max: 1.0
        width: 80
        height: if setting(largeText) then 10 else 7
        fillColor: #55ff55
        backgroundColor: #ffffff22
        borderRadius: 3
      }
      text {
        value: "等级 {get(player.xpLevel)}"
        color: if setting(highContrast) then #ffff00 else #55ff55
        fontSize: if setting(largeText) then 14 else 10
      }
    }
  }

  // 护甲
  if setting(showArmor) {
    separator { direction: horizontal color: #ffffff22 thickness: 1 length: auto margin: 2 }

    group {
      direction: row
      gap: 4
      align: center

      icon { item: get(inventory.helmetItem) size: 14 }
      icon { item: get(inventory.chestplateItem) size: 14 }
      icon { item: get(inventory.leggingsItem) size: 14 }
      icon { item: get(inventory.bootsItem) size: 14 }
    }
  }

  // 坐标
  if setting(showCoords) {
    separator { direction: horizontal color: #ffffff22 thickness: 1 length: auto margin: 2 }

    text {
      value: "X {floor(get(world.x))}  Y {floor(get(world.y))}  Z {floor(get(world.z))}"
      color: if setting(highContrast) then #ffff00 else #aaaaaa
      fontSize: if setting(largeText) then 13 else 10
    }

    text {
      value: "朝向 {get(world.facing)} · {get(world.biome)}"
      color: if setting(highContrast) then #ffff00 else #777777
      fontSize: if setting(largeText) then 12 else 9
    }
  }
}

本文档涵盖 HUD Script v1.0。如有任何问题或贡献,欢迎在社区分享您的脚本。

模组集成指南

Hudifine - 模组集成指南

Hudifine 提供了一个数据提供程序 API,允许任何 Fabric 模组注册自定义数据源。注册后,您的数据即可在 HUDScript 中通过 get() 访问,这与内置数据源(如 get(perf.fps) 或 get(player.health))的使用方式完全相同。


要求

  • Fabric Loader >=0.15.0
  • Minecraft >=26.1.2
  • 已安装 Hudifine 作为依赖项

1. 将 Hudifine 添加为依赖项

在您的 build.gradle 中:

repositories {
    maven { url = uri("https://cdn.jsdelivr.net/gh/Pacsy1/hudifine@main/public-maven/") }
}

dependencies {
    modImplementation "dev.hudifine:hudifine-api:1.0.0"
}

在您的 fabric.mod.json 中,将其声明为必需依赖项:

{
  "depends": {
    "hudifine": ">=1.0.0"
  }
}

2. 实现数据提供程序

Hudifine 提供了类型化的提供程序接口。请选择与您数据类型匹配的那个。

可用的提供程序类型

接口 返回类型 示例用例
IntDataProvider int FPS、延迟、实体数量
FloatDataProvider float TPS、速度、温度
StringDataProvider String 生物群系名称、维度、状态
BooleanDataProvider boolean 是否正在冲刺、是否在地面上

示例:FPS 提供程序

import dev.hudifine.api.provider.IntDataProvider;

public class FpsProvider implements IntDataProvider {

    @Override
    public String getId() {
        return "mymod.fps"; // 点分命名法,与 HUDScript 中的 get() 调用匹配
    }

    @Override
    public String getDisplayName() {
        return "FPS 计数器";
    }

    @Override
    public int getValue() {
        return Minecraft.getInstance().getFps();
    }
}

ID 命名约定

提供程序 ID 使用点分命名法以匹配 HUDScript 数据源的调用方式:

mymod.fps        → get(mymod.fps)
mymod.ping       → get(mymod.ping)
mymod.something  → get(mymod.something)

ID 必须是唯一的。重复的 ID 将在加载时被拒绝,并在 Hudifine 日志中发出警告。


3. 通过入口点注册提供程序

在您的 fabric.mod.json 中,在 hudifine:provider 入口点下注册您的提供程序类:

{
  "entrypoints": {
    "hudifine:provider": [
      "com.yourmod.FpsProvider"
    ]
  }
}

同时注册多个提供程序:

{
  "entrypoints": {
    "hudifine:provider": [
      "com.yourmod.FpsProvider",
      "com.yourmod.PingProvider",
      "com.yourmod.BiomeProvider"
    ]
  }
}

Hudifine 会在启动时自动发现并加载所有注册的提供程序。您无需编写额外的初始化代码。


4. 在 HUDScript 中使用您的数据

注册后,您的提供程序即可在任何 HUDScript 中通过 get() 访问,与内置数据源完全相同。

基本用法

widget {
  anchor: top-right
  offsetX: -10
  offsetY: 10
  background: #00000066
  padding: 6
  borderRadius: 4

  text {
    value: "{get(mymod.fps)} FPS"
    color: #ffffff
    fontSize: 12
    shadow: true
  }
}

带条件判断

widget {
  anchor: top-right
  offsetX: -10
  offsetY: 10
  background: #00000066
  padding: 6
  borderRadius: 4

  text {
    value: "{get(mymod.fps)} FPS"
    color: if get(mymod.fps) < 30 then #ff4444 else if get(mymod.fps) < 60 then #ffaa00 else #ffffff
    fontSize: 12
    shadow: true
  }
}

带进度条

widget {
  anchor: bottom-left
  offsetX: 10
  offsetY: -10
  background: #00000066
  padding: 8
  borderRadius: 6
  direction: column
  gap: 4

  text {
    value: "我的模组统计信息"
    color: #aaaaaa
    fontSize: 10
  }

  group {
    direction: row
    gap: 6
    align: center

    text { value: "FPS" color: #aaaaaa fontSize: 10 }
    bar {
      value: get(mymod.fps)
      max: 260
      width: 80
      height: 7
      fillColor: #55ff55
      backgroundColor: #ffffff22
      borderRadius: 3
    }
    text {
      value: "{get(mymod.fps)}"
      color: #ffffff
      fontSize: 10
    }
  }
}

配合玩家设置

您的提供程序数据可与所有 HUDScript 功能配合使用,包括玩家设置 API:

settings {
  toggle "显示我的模组统计信息" { id: showStats default: true }
  color "统计颜色" { id: statColor default: #55ff55 }
}

```yaml
widget {
  visible: setting(showStats)

  text {
    value: "{get(mymod.fps)} FPS"
    color: setting(statColor)
    fontSize: 12
    shadow: true
  }
}

5. 可选:提供程序元数据

实现 HudifineProviderMeta 以在 Hudifine 的游戏内提供程序浏览器中显示有关来源的更丰富信息:

import dev.hudifine.api.provider.IntDataProvider;
import dev.hudifine.api.provider.HudifineProviderMeta;

public class FpsProvider implements IntDataProvider, HudifineProviderMeta {

    @Override
    public String getId() { return "mymod.fps"; }

    @Override
    public String getDisplayName() { return "FPS Counter"; }

    @Override
    public int getValue() { return Minecraft.getInstance().getFps(); }

    // 可选元数据
    @Override
    public String getDescription() {
        return "Current rendered frames per second.";
    }

    @Override
    public String getCategory() {
        return "performance"; // 将其与 perf.fps、perf.tps 等分组在浏览器中
    }

    @Override
    public String getUnit() {
        return "fps";
    }
}

6. 性能指南

getValue() 会被每一帧调用。请保持其快速执行:

  • 缓存昂贵的计算,并在每个刻(tick)更新它们,而不是每帧更新
  • 避免在 getValue() 内部进行对象分配,尽可能使用基本数据类型
  • 如果数据仅在特定事件时更改,请在该事件上更新字段并返回缓存的字段

良好模式:

public class EntityCountProvider implements IntDataProvider {

    private int cachedCount = 0;

    @SubscribeEvent
    public void onTick(TickEvent event) {
        // 每秒运行 20 次,而不是每帧运行
        this.cachedCount = world.getEntities().size();
    }

    @Override
    public String getId() { return "mymod.entityCount"; }

    @Override
    public String getDisplayName() { return "Nearby Entity Count"; }

    @Override
    public int getValue() {
        return cachedCount; // 仅读取字段 - 即时
    }
}

7. 市场发布

你可能应该使用 Hudifine 作为模组标题的一部分,为此,只需在模组名称前加上 'Hudifine' 即可提高可发现性。


8. 使用你的数据构建 HUD

本指南仅涵盖模组集成部分。要了解如何实际编写使用你提供程序数据的 HUDScript 小部件,包括元素、布局、样式、条件判断、动画、设置等,请参阅上方的 HUDScript 开发者文档。


总结

步骤 操作内容
1 添加 Hudifine API 作为依赖项
2 实现带有点符号 ID 的类型化提供程序接口
3 在 fabric.mod.json 下的 hudifine:provider 中注册它
4 用户通过 HUDScript 中的 get(yourid) 访问你的数据
5 (可选) 实现 HudifineProviderMeta 以支持游戏内浏览器
6 保持 getValue() 快速执行,因为它会在每一帧运行
7 (可选) 在 Modrinth 上发布包含 Hudifine 名称的模组
8 查看 HUDScript 开发者文档以构建实际的小部件

Hudifine 模组集成指南 - v1.0

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源