配置与 GUI

配置文件

<configDir>/neiintegration.cfg(由 Config.preInit 用 evt.getModConfigurationDirectory() 拼出):

config = new Configuration(new File(evt.getModConfigurationDirectory(), NEIIntegration.MODID + ".cfg"));

Config 类在构造时把自身注册到 FML 总线(FMLCommonHandler.instance().bus().register(new Config())),以监听 ConfigChangedEvent.OnConfigChangedEvent——在游戏内改配置即时生效,无需重启(与很多 mod 不同)。

3 个配置段

Config 定义 3 个 Section:

常量 名称 key
sectionIntegrations Integrations integrations
sectionHandlers Handlers handlers
sectionTooltips Tooltips tooltips

Section(config/Section.java)只有 name 与 id 两个字段,且其构造器会自动把自己注册进 Config.configSections:

public Section(String name, String id) {
    this.name = name;
    this.id = id;
    Config.configSections.add(this);   // ← 构造即注册
}

Integrations 段:每个集成一个动态开关

Config.processConfig()(:76-92)会遍历 6 个集成,为每个 isValid() 为真的集成动态生成一个布尔配置项:

for (Iterator<IntegrationBase> itr = NEIIntegration.integrations.iterator(); itr.hasNext();) {
    IntegrationBase integration = itr.next();
    String name = integration.getName();
    boolean enabledByDefault = integration.isEnabledByDefault();
    if (!integration.isValid()) {
        itr.remove();                                    // 目标 mod 未安装 → 直接移除
    } else if (!config.get(sectionIntegrations.name, name, enabledByDefault,
            String.format("Whether to enable %s integration. Disable if this part of the mod causes crashes.", name))
            .setRequiresMcRestart(true).getBoolean(enabledByDefault)) {
        itr.remove();                                    // 用户关闭 → 从列表移除
    }
}

要点:

  • 配置项的名字就是集成的显示名(getName(),如 "Railcraft"、"Minecraft / Forge")
  • 默认值取 isEnabledByDefault(),IntegrationBase 中恒为 true
  • 描述文本固定为 "Whether to enable %s integration. Disable if this part of the mod causes crashes."
  • setRequiresMcRestart(true) —— 改这个开关需要重启游戏
  • isValid() 为假的集成根本不写配置(目标 mod 没装就没有这个选项)
  • 关闭开关的实质是 itr.remove(),该集成永不调用 loadConfig(),即 handler 完全不注册

20 个固定开关

全部默认值均为 false(config/Defaults.java 中 20 个 public static final boolean 全部为 false)。这与上面 6 个集成开关(默认 true)形成对比。

Handlers 段(2 个)

字段 默认 作用
handlerFluidRegistry false 启用 RecipeHandlerFluidRegistry 的配方内容
handlerOreDictionary false 启用 RecipeHandlerOreDictionary 的配方内容

两个 handler 始终会被注册,开关只包裹其配方加载逻辑。默认状态下它们在 NEI 里可见但产出 0 条配方。见 Minecraft / Forge 集成。

Tooltips 段(18 个)

6 类信息 × 3 个显示条件:

信息 普通 Shift Advanced
未本地化名 tooltipUnlocalizedName tooltipUnlocalizedNameShift tooltipUnlocalizedNameAdvanced
内部名 tooltipInternalName tooltipInternalNameShift tooltipInternalNameAdvanced
最大堆叠数 tooltipMaxStack tooltipMaxStackShift tooltipMaxStackAdvanced
燃料燃烧时间 tooltipBurnTime tooltipBurnTimeShift tooltipBurnTimeAdvanced
矿物词典名 tooltipOreDictNames tooltipOreDictNamesShift tooltipOreDictNamesAdvanced
流体注册信息 tooltipFluidRegInfo tooltipFluidRegInfoShift tooltipFluidRegInfoAdvanced

判定式(MCForgeTooltipHandler.java:23):

if (Config.tooltipUnlocalizedName
        && (!Config.tooltipUnlocalizedNameShift || Utils.isShiftKeyDown())
        && (!Config.tooltipUnlocalizedNameAdvanced || evt.showAdvancedItemTooltips)) { ... }

含义:

  • 主开关必须开,否则该类信息一律不显示
  • Shift 变体开了就要求按住 Shift
  • Advanced 变体开了就要求开启高级 tooltip(F3+H)
  • 三者可叠加,例如主开关 + Shift + Advanced 全开 = 只在「按住 Shift 且开启高级 tooltip」时显示

配置 GUI

@Mod(guiFactory = "tonius.neiintegration.config.ConfigGuiFactory") 声明了 Forge 标准配置界面:

类 职责
ConfigGuiFactory 实现 IGuiFactory,createConfigGui 返回 ConfigGui
ConfigGui extends GuiConfig,构造时调 getConfigElements() 生成元素列表
Defaults 20 个默认值的常量定义
Section 配置段包装,构造即自注册到 Config.configSections

ConfigGui 传给父构造器的参数为:(parentScreen, elements, NEIIntegration.MODID, false, false, Utils.translate("config.title"))——后两个 false 分别是 worldCannotBeSaved 与 requiresWorldRestart。

GUI 元素的本地化键前缀为 neiintegration.config.。

语言支持

src/main/resources/assets/neiintegration/lang/ 下有 5 个语言文件:

zh_CN.lang   es_ES.lang   en_US.lang   ru_RU.lang   cs_CZ.lang

含简体中文(zh_CN)——这是本 wiki 已收录的少数带中文的 mod 之一。

资源总量

src/main/resources/assets/ 下共 14 个文件:5 个 lang + 9 张贴图(textures/basicProcessing.png、overlays.png、blank.png 与 textures/harvestcraft/ 下 5 张)。

相关条目