托管 HUD、声明式面板与恢复所有权
本页目录
本章介绍 ManagedHudAPI 与 DeclarativeHudAPI 的公共配置和状态范围。两组 API 都是 Experimental;模块覆盖和面板显示不是同一份状态,不应相互当作配置重置。
两组入口的状态范围
| 入口 | 实际目标 | 恢复方式 |
|---|---|---|
| ManagedHudAPI.configure | 已注册且实现 ServerConfigurableHudComponent 的客户端模块 | reset 清该模块的服务端覆盖,模块自行实现本地回退 |
| DeclarativeHudAPI.upsert | 以 id 标识的声明式面板 | remove 自己的面板 id |
| DeclarativeHudAPI.sync | 整份声明式面板集合,客户端 clear 后替换 | 不应由独立附属拿自己的子集覆盖别人 |
| DeclarativeHudAPI.clear | 全部声明式面板 | 不是清已注册托管模块或全部游戏 UI |
configure 返回 true 只说明服务端玩家/能力/id/config 条件通过。客户端缺该模块、不实现配置 SPI 或 applyServerConfig 失败时不会因此凭空创建模块;客户端处理器也没有为这个模块操作向服务端提供最终成功回执。reset 同理不等于画面已经恢复。
托管 API 只配置已有模块,不附送某个服务器的业务界面。调用方需要自己注册实现 ServerConfigurableHudComponent 的组件、约定模块 ID 与配置,并检查自己所需的能力。核心能力存在不等于该组件存在;模块的覆盖验证、本地回退和重载策略由组件所有者负责。
声明式面板的真实边界
sync 在服务端提交最多前 32 个非 null 面板,但 huds=null 在兼容目标下会进入循环抛错,而不是统一 false。客户端按有效面板解析构建替换集合,重复 id 覆盖;非法 id 的项不创建面板,直接协议中非对象项会提前停止遍历,而不是保证处理所有后续项。
upsert 对已存在 id 替换,对新 id 仅在客户端容量尚有空间时添加;服务器 true 不保证容量许可或解析成功。update_component/animate 需要客户端已存在面板与目标组件,提交没有创建这两者的保证。remove/clear/reset 操作应明确各自所有权,不把自己卸载作为清理全体插件的许可。
返回 JSON 会被组包复制;id 需小写 1–64 位 [a-z0-9_.:-]。客户端处理在 Minecraft 任务中执行:提交时尚未必画到当前帧。原版 HUD 隐藏策略还取决于替代组件是否就绪,不应简单等同于 managed 配置 enabled=true。
| 声明式方法 | 参数与提交边界 |
|---|---|
| sync(player, huds) | 列表中的 null 项跳过,空列表替换为空;不是追加,也没有服务端最终显示回执 |
| upsert(player, hud) | null 对象返回 false;面板 id/组件解析主要在客户端,不把服务端 true 当作校验通过 |
| animate(player, hudId, componentId, animation) | 两个 id 校验与 animation 非 null 后提交 update_component;不创建面板,也不创建缺失目标组件 |
| removeComponent(player, hudId, componentId) | 校验两个 id 后提交;目标不存在时客户端无移除效果,不影响其他面板 |
| remove(player, hudId) | 只提交指定面板移除;客户端不存在也不是业务错误确认 |
| clear(player) | 提交整份声明式面板清理;只应由明确拥有全局协调权的宿主使用 |
所有这些入口先检查玩家非 null、在线与 declarative_hud 能力;ManagedHudAPI 改查 managed_hud_config_v1。面板组件白名单只有 text/image/slot/item/canvas,每面板最多 256 个接受的组件、深度最多 16;配置不允许借这些入口执行任意业务脚本。API 不替调用方做权限或业务恢复。
完整的无贴图面板演示
此类在已启用的 Bukkit 附属插件主线程使用,只 upsert/remove 自己的 wiki_status_owned,不接管全套 HUD,也不读取未安装模块的实现类。
package example.wiki;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.heartlion.shadowui.api.DeclarativeHudAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public final class WikiOwnedHud {
private static final String ID = "wiki_status_owned";
public boolean show(Player player, String text) {
requireMainThread();
JsonObject label = new JsonObject();
label.addProperty("id", "status");
label.addProperty("type", "text");
label.addProperty("x", 4);
label.addProperty("y", 4);
label.addProperty("width", 172);
label.addProperty("height", 22);
label.addProperty("text", text == null ? "" : text);
label.addProperty("color", "#FFFFFF");
JsonArray components = new JsonArray();
components.add(label);
JsonObject hud = new JsonObject();
hud.addProperty("id", ID);
hud.addProperty("anchor", "top_left");
hud.addProperty("x", 12);
hud.addProperty("y", 12);
hud.addProperty("width", 180);
hud.addProperty("height", 30);
hud.add("components", components);
return DeclarativeHudAPI.upsert(player, hud);
}
public boolean hide(Player player) {
requireMainThread();
return DeclarativeHudAPI.remove(player, ID);
}
private static void requireMainThread() {
if (!Bukkit.isPrimaryThread()) {
throw new IllegalStateException("需要 Bukkit 主线程");
}
}
}
完整插件注册与 depend 见开发入门;此封装没有内置命令、生命周期注册或代你遍历玩家。业务宿主在自己目标玩家卸载/结束时调用 hide,不改成全局 clear。客户端显示、覆盖后回退、32 面板容量、组件不存在、缺模块与原版恢复仍需声明式 HUD与客户端 HUD SPI所述的实机验证。全部签名见HUD API。