ConfigurationHandler

7 项配置 + 9 个对齐偏移

基本信息

属性 值
路径 com.github.lunatrius.ingameinfo.handler.ConfigurationHandler
行数 136
单例 public static final ConfigurationHandler INSTANCE(:20)
静态配置对象 public static Configuration configuration(:22)

7 个默认值常量(:24-30)

CONFIG_NAME_DEFAULT  = Names.Files.FILE_XML;   // "InGameInfo.xml"
SHOW_HUD_DEFAULT     = true;
REPLACE_DEBUG_DEFAULT = false;
SHOW_IN_CHAT_DEFAULT = true;
SHOW_ON_PLAYER_LIST_DEFAULT = true;
DEFAULT_SCALE        = 10;
FILE_INTERVAL_DEFAULT = 5;

⚠️ 这些 *_DEFAULT 常量与 Names.Config 里的键名常量分开定义, Names 只管键名与描述文本,本类管默认值。

7 个静态可变字段(:32-38)

字段 类型 初值 语义
configName String InGameInfo.xml 加载哪个配置文件
showHUD boolean true 渲染开关
replaceDebug boolean false 顶掉 F3
showInChat boolean true 聊天界面内显示
showOnPlayerList boolean true 多人列表打开时显示
Scale float (float) 10 缩放
fileInterval int 5 file value 重读间隔(秒)

⚠️ Scale 是 float 类型但配置里是 int(:84 的 configuration.get(..., DEFAULT_SCALE, ..., 1, 20)), 由 Scale = (float) propscale.getInt(DEFAULT_SCALE);(:86)转换。 float 而非 int 是为了让 Scale / 10 产生小数(如 7 / 10 = 0.7)—— 见 InGameInfoCore 的 :135、:161。

⚠️ Scale 是首字母大写的字段名,违反 Java 命名约定 (全仓唯一一处这样的静态字段),且被外部直接读写 (InGameInfoCore、Ticker)。

7 个 Property 句柄(:40-46)

propConfigName / propShowHUD / propReplaceDebug / propShowInChat / propShowOnPlayerList / propscale / propFileInterval —— 全部 static Property,供外部读 isChanged()、 set() 以及给 ClientProxy 设 GUI 控件类型。

外加 public static final Map<Alignment, Property> propAlignments(:47)。

loadConfiguration(:58-103)—— 主体

整段被 // spotless:off / // spotless:on 包住(:60、:100), 即刻意排除格式化(为了保持每行 configuration.get(...) 的可读性)。

7 项的固定写法

propX = configuration.get(Names.Config.Category.GENERAL, Names.Config.X, X_DEFAULT, Names.Config.X_DESC);
propX.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.X);
propX.setRequiresMcRestart(true|false);
x = propX.getBoolean/getInt/getString(X_DEFAULT);
配置项 分类 setRequiresMcRestart
filename general true
ShowHUD general false(:69 显式)
replaceDebug general 未调用
showInChat general 未调用
showOnPlayerList general 未调用
scale(new) general 未调用
fileInterval general 未调用

⚠️ 只有 2 项显式设了 setRequiresMcRestart, 其余 5 项依赖 Forge 默认值(false)。 ShowHUD 显式写 false(:69),而 replaceDebug 等 4 项不写 —— 说明作者只对需要重启的两项显式标注。

LANG_PREFIX = ingameinfoxml.config(见 Names), 所以 7 项的 lang key 是 ingameinfoxml.config.filename 等。

范围限制

项 范围 源码
scale(new) 1 – 20 :84
fileInterval 1 – 60 :88

⚠️ 这两项没有 @-注解,范围靠 configuration.get(category, key, def, comment, min, max) 的重载传入,且 ClientProxy 给了 NumberSliderEntry(滑块 UI)。所以范围是双保险(配置校验 + GUI 滑块)。

9 个对齐偏移(:92-98)

for (Alignment alignment : Alignment.values()) {
    Property property = configuration.get(Names.Config.Category.ALIGNMENT,
        alignment.toString().toLowerCase(), alignment.getDefaultXY(),
        String.format(Names.Config.ALIGNMENT_DESC, alignment));
    property.setLanguageKey(Names.Config.LANG_PREFIX + "." + alignment.toString().toLowerCase());
    property.setValidationPattern(Pattern.compile("-?\\d+ -?\\d+"));
    propAlignments.put(alignment, property);
    alignment.setXY(property.getString());
}
项 值
分类 alignment(与 7 项的 general 分开)
键名 枚举名小写(topleft / bottomcenter …)
默认值 alignment.getDefaultXY()(见 Alignment)
描述 "Offsets for %s (X<space>Y)."
校验正则 -?\d+ -?\d+

⚠️ 校验正则要求两个整数之间恰好一个空格 —— 两个空格或 tab 都会校验失败。配合 Alignment.setXY 的 split(" ") + catch (Exception ignored) {} 静默失败, 写错偏移时既不报错也不生效。

⚠️ String.format(ALIGNMENT_DESC, alignment) 把枚举对象而非名字传进 %s, 所以描述里出现的是 TOPLEFT 而非 topleft(toString() 未覆写)。

init / reload / save(:51-56、:105-124)

方法 行为
init(File) configuration == null 时新建 Configuration 并 loadConfiguration()
reload() loadConfiguration() + save()
save() 仅在 hasChanged() 时 configuration.save()
saveHUDsettingToFile() 重新取 ShowHUD 的 Property、set(showHUD)、save()

⚠️ init 的 configuration == null 判断让重复调用成为空操作 —— 所以 ServerProxy(服务端)与 ClientProxy(客户端)各调一次不冲突。

⚠️ saveHUDsettingToFile(:110-118)只处理 ShowHUD —— /igi enable / /igi disable 和切换键都走这个方法, 即只把 HUD 开关写回磁盘。其它 6 项的改动靠 save()。

onConfigurationChangedEvent(:130-135)

@SubscribeEvent
public void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) {
    if (event.modID.equalsIgnoreCase(Reference.MODID)) loadConfiguration();
}

订阅在 ClientProxy.init(:53)注册 ClientConfigurationHandler.INSTANCE 到 FML 总线。

⚠️ 服务端不订阅这个事件(ServerProxy 只注册了 PlayerHandler 和 WorldHandler), 所以服务端改配置需要重启。

相关