From ee7abec346aea99749500e4a18cfc64ef4df216f Mon Sep 17 00:00:00 2001 From: Cake <65340665+cakeGit@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:00:43 +0100 Subject: [PATCH] Expand the configuration code example to give some more context (#107) Co-authored-by: IchHabeHunger54 <52419336+IchHabeHunger54@users.noreply.github.com> --- docs/misc/config.md | 48 +++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/docs/misc/config.md b/docs/misc/config.md index 6a37f420..2f5aa57b 100644 --- a/docs/misc/config.md +++ b/docs/misc/config.md @@ -15,16 +15,18 @@ A configuration can be created using a subtype of `IConfigSpec`. NeoForge implem `ModConfigSpec.Builder#configure` is typically used with a `static` block and a class that takes in `ModConfigSpec.Builder` as part of its constructor to attach and hold the values: ```java -// In some config class -ExampleConfig(ModConfigSpec.Builder builder) { - // Define values here in final fields -} - -// Somewhere the constructor is accessible +//Define a field to keep the config and spec for later +public static final ExampleConfig CONFIG; +public static final ModConfigSpec CONFIG_SPEC; + +//CONFIG and CONFIG_SPEC are both built from the same builder, so we use a static block to seperate the properties static { - Pair pair = new ModConfigSpec.Builder() - .configure(ExampleConfig::new); - // Store pair values in some constant field + Pair pair = + new ModConfigSpec.Builder().configure(ExampleConfig::new); + + //Store the resulting values + CONFIG = pair.getLeft(); + CONFIG_SPEC = pair.getRight(); } ``` ::: @@ -52,9 +54,14 @@ The `ConfigValue` specific methods take in two additional components: - A class representing the data type of the config value ```java -// For some ModConfigSpec.Builder builder -ConfigValue value = builder.comment("Comment") - .define("config_value_name", defaultValue); +//Store the config properties as public finals +public final ModConfigSpec.ConfigValue welcomeMessage; + +private ExampleConfig(ModConfigSpec.Builder builder) { + //Define each property + //One property could be a message to log to the console when the game is initialised + welcomeMessage = builder.define("welcome_message", "Hello from the config!"); +} ``` The values themselves can be obtained using `ConfigValue#get`. The values are additionally cached to prevent multiple readings from files. @@ -109,9 +116,10 @@ Once a `ModConfigSpec` has been built, it must be registered to allow NeoForge t ```java // In the main mod file with a ModConfigSpec CONFIG public ExampleMod(ModContainer container) { - container.registerConfig(ModConfig.Type.COMMON, CONFIG); - - // Do other things + ... + //Register the config + container.registerConfig(ModConfig.Type.COMMON, ExampleConfig.CONFIG); + ... } ``` @@ -170,20 +178,22 @@ A configuration screen can be registered for a mod by registering a `IConfigScre ```java // In the main client mod file public ExampleModClient(ModContainer container) { + ... // This will use NeoForge's ConfigurationScreen to display this mod's configs container.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new); + ... } ``` The configuration screen can be accessed in game by going to the 'Mods' page, selecting the mod from the sidebar, and clicking the 'Config' button. Startup, Common, and Client config options will always be editable at any point. Server configs are only editable in the screen when playing on a world locally. If connected to a server or to another person's LAN world, Server config option will be disabled in the screen. The first page of the config screen for the mod will show every registered config file for players to pick which one to edit. :::warning -Translation keys should be added and specified within the lang JSON for all config entries. +Translation keys should be added and have the text defined within the lang JSON for all config entries if you are making a screen. -You can specify a translation key for a config by using the `ModConfigSpec$Builder#translation` method: +You can specify a translation key for a config by using the `ModConfigSpec$Builder#translation` method, so we can extend the previous code to: ```java -ConfigValue value = builder.comment("Comment") - .translation("modid.configuration.config_value_name") +ConfigValue value = builder.comment("This value is called 'config_value_name', and is set to defaultValue if no existing config is present") + .translation("modid.config.config_value_name") .define("config_value_name", defaultValue); ```