Skip to content

Creating Configs

Nicholas Smit edited this page Jan 14, 2024 · 3 revisions

Creating Configuration Files

Creating a configuration file is extremely easy. First, create a ConfigManager instance somewhere. Ideally, this will be in your mod constructor:

public YourMod(IEventBus modEventBus) {
	// Creates a config named "modid.json"
	new ConfigBuilder("modid", ConfigEntries.class).build();
}

Then, define a class that contains your configuration entries:

public static class ConfigEntries {
	@ConfigEntry
	public static int testIntOption = 50;   // Creates a JSON key named "testIntOption" with a default value of 50
}

That's the bare minimum required to start using Cobalt Config. You can access the values directly via static references. This type of configuration will automatically sync from servers to clients during the login process or after running /reload in-game.

More ConfigBuilder Settings

Suffixes

You can add a suffix to the filename (like modid-suffix.json). This is useful if you use separate common and client configurations.

public YourMod(IEventBus modEventBus) {
	// Creates a config named "modid-suffix.json"
	new ConfigBuilder("modid", "suffix", ConfigEntries.class).build();
}

Config Names

Config names are used to provide a friendly name when using the configuration menu. By default, the classname of the config class is used.

public YourMod(IEventBus modEventBus) {
	new ConfigBuilder("modid", ConfigEntries.class)
	.setConfigName("Friendly Configuration Name")
	.build();
}

You can also pass a translation string if you would like.

Client-Only Configs

Client-only configurations do not generate on dedicated server environments and are not synced anywhere.

public YourMod(IEventBus modEventBus) {
	new ConfigBuilder("modid", ConfigEntries.class)
	.setClientOnly(true)
	.build();
}

More @ConfigEntry Settings

Comments

You can place comments inside annotations which will be displayed underneath entries in the configuration menu. Comments have no effect on the configuration file itself.

@ConfigEntry(comment = "This is an int config option")
public static int testIntOption = 50;

Translation strings are supported.

Min/Max Bounds

For number-type values, min/max bounds can be declared. When loading a configuration that has out-of-bounds values, the values will be clamped to within the delcared range.

When both min/max bounds are specified, the configuration menu will use a slider widget to represent the entry.

@ConfigEntry(min = 0, max = 100)
public static int testIntOption = 50;

Groups

You can define groups for entries, and entries with the same group will be placed together in the configuration menu. Groups have no effect on the configuration file itself.

@ConfigEntry(group = "Test Group")
public static int testIntOption = 50;

Translation strings are supported.

Restart Required Marker

If an entry requires a restart for changes to take effect, you can mark it as such. This will not prevent the value from being updated or synced, but it indicates to the user that it will not be utilized until a restart.

@ConfigEntry(restartRequired = true)
public static int testIntOption = 50;