Skip to content

Commit

Permalink
Expand the configuration code example to give some more context (#107)
Browse files Browse the repository at this point in the history
Co-authored-by: IchHabeHunger54 <[email protected]>
  • Loading branch information
cakeGit and IchHabeHunger54 authored Oct 12, 2024
1 parent e7cc93b commit ee7abec
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions docs/misc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExampleConfig, ModConfigSpec> pair = new ModConfigSpec.Builder()
.configure(ExampleConfig::new);
// Store pair values in some constant field
Pair<ExampleConfig, ModConfigSpec> pair =
new ModConfigSpec.Builder().configure(ExampleConfig::new);

//Store the resulting values
CONFIG = pair.getLeft();
CONFIG_SPEC = pair.getRight();
}
```
:::
Expand Down Expand Up @@ -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<T> value = builder.comment("Comment")
.define("config_value_name", defaultValue);
//Store the config properties as public finals
public final ModConfigSpec.ConfigValue<String> 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.
Expand Down Expand Up @@ -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);
...
}
```

Expand Down Expand Up @@ -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<T> value = builder.comment("Comment")
.translation("modid.configuration.config_value_name")
ConfigValue<T> 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);
```
Expand Down

0 comments on commit ee7abec

Please sign in to comment.