SuperMartijn642's Config Lib
Config Lib makes dealing with config files just a bit easier.
SuperMartijn642's Config Lib makes dealing with config files just a bit easier. A config is defined once in code, and Config Lib takes care of the rest: it generates the config file with comments and allowed values, reloads values when the file changes, syncs values from the server to clients, and keeps client-only and server-only values on the right side.
Just like Core Lib, Config Lib has the same API for every Minecraft version from 1.12 up to the latest versions, on Forge, NeoForge, Fabric and Quilt.
Players only need to install Config Lib when a mod requires it.
Features
Getting started
Config Lib can be added to a project from Modrinth's Maven repository.
Replace the version with the one for your Minecraft version and mod loader from the
versions page, e.g. 1..
repositories {
maven { url = "https://api.modrinth.com/maven" }
}
dependencies {
implementation "maven.modrinth:supermartijn642s-config-lib:<version>"
}
Creating a config
A config is created with an IConfigBuilder from ConfigBuilders. Every value is defined with a key, a default value and
optionally a comment, and define returns a Supplier from which the value can be read at any time. After all values
are defined, build() creates the config and its values can be used right away.
public class ExampleConfig {
public static final Supplier<Boolean> enableMagnet;
public static final Supplier<Integer> range;
public static final Supplier<Double> speed;
public static final Supplier<OverlayMode> overlayMode;
static{
IConfigBuilder builder = ConfigBuilders.newTomlConfig("examplemod", null, false);
builder.push("General").categoryComment("General settings");
enableMagnet = builder.comment("Should the magnet be enabled?").define("enableMagnet", true);
range = builder.comment("In what range should items be picked up?").define("range", 5, 1, 16);
speed = builder.comment("How fast should items move towards the player?").define("speed", 0.5, 0, 2);
builder.pop();
builder.push("Client");
overlayMode = builder.dontSync().comment("How should the overlay be displayed?").define("overlayMode", OverlayMode.COMPACT);
builder.pop();
builder.build();
}
public static void init(){
// Causes this class to load
}
}
Calling ExampleConfig. from the mod's constructor makes sure the config is created during startup, after which
a value is simply read with e.g. ExampleConfig..
The config file
Config Lib writes the config file for you, including every comment and the allowed values of each entry. The config
above becomes config/:
# General settings
[General]
# Should the magnet be enabled?
# Allowed values: true, false - Default: true
enableMagnet = true
# In what range should items be picked up?
# Allowed range: 1 ~ 16 - Default: 5
range = 5
# How fast should items move towards the player?
# Allowed range: 0.0 ~ 2.0 - Default: 0.5
speed = 0.5
[Client]
# How should the overlay be displayed?
# Allowed values: COMPACT, DETAILED - Default: COMPACT
overlayMode = "COMPACT"
The file is named after the mod id and the config's name. Without a name, the name is client or server when all
values are client-only or server-only, and common otherwise. Values which are missing or not allowed are replaced
by their default value. Configs can also use the json format instead, through ConfigBuilders..
Value types
Values can be booleans, integers, longs, doubles, enums and strings. Numbers have a minimum and maximum value, and strings a minimum and maximum length.
Supplier<Boolean> booleanValue = builder.define("booleanValue", true);
Supplier<Integer> integerValue = builder.define("integerValue", 5, 0, 10);
Supplier<Long> longValue = builder.define("longValue", 1000L, 0L, Long.MAX_VALUE);
Supplier<Double> doubleValue = builder.define("doubleValue", 0.5, 0, 1);
Supplier<OverlayMode> enumValue = builder.define("enumValue", OverlayMode.COMPACT);
Supplier<String> stringValue = builder.define("stringValue", "minecraft", 0, 100);
Syncing and sides
By default, values are synced from the server to clients when they join, so clients always use the server's values.
dontSync() keeps a value on each side separate, which is useful for client preferences. Values can also only exist on
one side with onlyOnClient() and onlyOnServer(), they will then only be written to the config file on that side.
Supplier<Boolean> showOverlay = builder.dontSync().define("showOverlay", true);
Supplier<Integer> particleCount = builder.onlyOnClient().define("particleCount", 20, 0, 100);
Supplier<Integer> maxLoadedChunks = builder.onlyOnServer().define("maxLoadedChunks", 64, 0, 1024);
Reloading
Config files are watched for changes, so they can be edited while the game is running. New values are applied the next
time a world is loaded or a server is joined. Values which can only be applied while the game starts, for example
because they change which blocks are registered, can be marked with gameRestart(). These values are not synced.
Supplier<Boolean> enableLargeTanks = builder.gameRestart().comment("Should the large tanks be registered?").define("enableLargeTanks", true);
Frequently asked questions
Why do I need to install Config Lib?
Many of SuperMartijn642's mods use Config Lib for their config files, so it is required for those mods to work. Config Lib does not add any content to the game by itself.
Where can I find the config files?
Config files are placed in the config folder of your Minecraft instance, named after the mod, e.g. config/.
Can I use Config Lib in my modpack?
Yes, feel free to use Config Lib in your modpack.
Can I use Config Lib for my own mods?
Yes, Config Lib is meant to be used by other mods as well.