YetAnotherConfigLib
Ask or search…
K

Basic usage of Config API

Learn about what the Config API does.

What is it?

Along with YACL's primary functionality, the GUI, this mod also provides a super easy way to manage your mod's config. It can be a tedious part of every mod you make, this is why the Config API was created!
All you need to do is create a class, add some fields and tell YACL how to save and load your config.

An example...

public class MyConfig {
public static ConfigClassHandler<MyConfig> HANDLER = ConfigClassHandler.createBuilder(MyConfig.class)
.id(new ResourceLocation("mymod", "my_config"))
.serializer(config -> GsonConfigSerializerBuilder.create(config)
.setPath(FabricLoader.getInstance().getConfigDir().resolve("my_mod.json5"))
.appendGsonBuilder(GsonBuilder::setPrettyPrint) // not needed, pretty print by default
.setJson5(true)
.build())
.build();
@SerialEntry
public boolean myCoolBoolean = true;
@SerialEntry
public int myCoolInteger = 5;
@SerialEntry(comment = "This string is amazing")
public String myCoolString = "How amazing!";
}
That is it! That's all the setup you need for your config.
In this example, you can see that we told the config handler to use GSON to serialize your fields, meaning not just primitive/basic types work, but any! You can even see me modifying the GSON builder. Only fields annotated with @SerialEntry are considered.

Saving and loading

In the above example, you can see we specified the serializer like so:
.serializer(config -> GsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("my_mod.json5"))
.appendGsonBuilder(GsonBuilder::setPrettyPrint) // not needed, pretty print by default
.setJson5(true)
.build())
You can see we specified the GSON serializer, set its path, configured GSON, and specified to use JSON5 spec. Currently, GSON is the only serializer made available to you. You can create your own, more on that later.
To save and load use the respected methods:
MyConfig.HANDLER.save();
MyConfig.HANDLER.load();