声明式 HUD:完整配置与下发
本页目录
声明式 HUD 通过 DeclarativeHudAPI 下发组件树,由客户端核心显示自定义面板,不要求预装某个服务器的业务界面。该 API 为 Experimental,调用方需封装并检查客户端支持,不假定旧客户端一定接受。托管模块的配置覆盖是另一组公共入口,见HUD 所有权。
不依赖贴图的完整例子
下面是一个 HUD JSON 对象,不是包裹了 cmd 的原始网络包,也不是放进 config.yml 就自动下发的服务端开关。
{
"id": "wiki_status",
"anchor": "top_left",
"x": 12,
"y": 12,
"width": 180,
"height": 30,
"scale": 1.0,
"components": [
{"id": "status", "type": "text", "x": 4, "y": 4, "width": 172, "height": 22, "text": "Wiki HUD 示例", "color": "#FFFFFF"}
]
}
提交、更新与清除
在已启用附属插件的 Bukkit 主线程,以在线玩家为目标,加载以上 JsonObject 后调用:
boolean submitted = DeclarativeHudAPI.upsert(player, hudJson);
if (!submitted) {
player.sendMessage("客户端不支持或提交条件不满足");
}
此片段需要 import com.heartlion.shadowui.api.DeclarativeHudAPI,player/hudJson 由附属插件提供;完整插件骨架见API 入门。没有一个内置 /shadowui hud open 命令会代替它。upsert 的 true 只表示检查通过并提交,不证明客户端已经显示。
随后可用 remove(player, "wiki_status") 解除单个 HUD;clear 会清除该入口管理的声明式 HUD,不能拿它当业务缓存或磁盘配置重置。组件动画、sync、增量更新与所有重载见签名参考。
带配套贴图的完整 Boss 样例
准备真实 hud/boss_bar 资源后才使用这份样例;未准备资源时优先使用上面的文字 HUD 区分网络和资源故障。
{
"id": "boss_status",
"anchor": "top_center",
"x": 0,
"y": 12,
"width": 220,
"height": 40,
"scale": 1.0,
"components": [
{
"id": "background",
"type": "image",
"x": 0,
"y": 0,
"width": 220,
"height": 40,
"texture": "hud/boss_bar",
"opacity": 0.9
},
{
"id": "title",
"type": "text",
"x": 10,
"y": 8,
"width": 200,
"text": "Skeleton King",
"align": "center",
"color": "#FFFFFF"
}
]
}
字段、动画与下发参考
声明式 HUD 与组件动画
能力边界
- HUD 复用 CustomGUI 的
text、image、slot/item和canvas组件。 - HUD 第一版不接受
button、textinput、dropdown、scroll或entity,也不接收鼠标和键盘事件。 - HUD 只在双方协商
declarative_hud后下发。 - 单玩家最多 32 个 HUD 面板,合计最多 256 个组件,容器深度最多 16 层。
HUD 配置
{
"id": "boss_status",
"anchor": "top_center",
"x": 0,
"y": 12,
"width": 220,
"height": 40,
"scale": 1.0,
"components": [
{
"id": "background",
"type": "image",
"x": 0,
"y": 0,
"width": 220,
"height": 40,
"texture": "hud/boss_bar"
},
{
"id": "title",
"type": "text",
"x": 10,
"y": 8,
"width": 200,
"text": "Skeleton King",
"align": "center",
"color": "#FFFFFF"
}
]
}
锚点支持 top_left、top_center、top_right、center、bottom_left、bottom_center 和 bottom_right。
Plugin API
DeclarativeHudAPI.upsert(player, hudJson);
DeclarativeHudAPI.sync(player, hudList);
DeclarativeHudAPI.remove(player, "boss_status");
DeclarativeHudAPI.clear(player);
内容或组件类型变化使用 upsert 替换整个面板。第一版局部 update_component 只用于动画,避免动态更新绕过 HUD 安全组件白名单。
组件动画
普通 CustomGUI 和声明式 HUD 共用动画格式:
{
"id": "title",
"animate": {
"x": 30,
"y": 8,
"width": 180,
"height": 16,
"opacity": 1.0,
"durationTicks": 20,
"delayTicks": 0,
"easing": "ease_in_out"
}
}
支持 linear、ease_in、ease_out 和 ease_in_out。持续时间和延迟最多 1200 tick。
当前动画属性:
x/ywidth/heightopacity,完整作用于 image、text 和组件背景;其他组件仍以背景透明度为主
CustomGUI 继续使用原有 customui/update_component:包含 animate 或 animation 时原地播放动画;不包含时保持旧行为并替换组件。
HUD 动画 API:
JsonObject animation = new JsonObject();
animation.addProperty("x", 30);
animation.addProperty("opacity", 1.0F);
animation.addProperty("durationTicks", 20);
animation.addProperty("easing", "ease_out");
DeclarativeHudAPI.animate(player, "boss_status", "title", animation);
网络类型
顶级 cmd:customhud。
syncupsertremoveupdate_componentremove_componentclear
该协议第一版为单向 server → client。