Config API

This is an archive of the old GitHub wiki! This will be re-written in the near future.

What is this?

The Config API handles saving and loading fields in a class automatically. This pairs great with YetAnotherConfigLib because now this library is a full solution to config in your mods.
This API doesn't handle automatically generating a GUI, that is up to you to build. It would be silly to have one full, comprehensive API along with a mediocre annotations-based API together. This API is purely saving and loading.

How do I use it?

Before we get to actual code, it's best to explain the structure of this API (it's very simple)
It's split into to main components, a config instance and config data. Config data is a class you create, containing all the fields that you want to save and load. A config instance manages that class, actually implementing saving and loading.

Tutorial

First, let's make a config data class.
public class MyConfig {
@ConfigEntry public boolean myOption = true;
}
Let's leave this for now, and come back to it later.
There is one implementation of ConfigInstance available by default: GsonConfigInstance. This instance uses GSON to serialize and deserialize JSON to a file. Only fields annotated with @ConfigEntry are included in the JSON. Text, Style and java.awt.Color have default type adapters, so there is no need to provide them in your GSON instance. Also, GSON is automatically configured to format fields as lower_camel_case.
Now, let's construct a GsonConfigInstance.
GsonConfigInstace<MyConfig> configInstance = new GsonConfigInstance<>(MyConfig.class, Path.of("path/to/config.json"));
It's as simple as that, you can optionally pass your own Gson instance or GsonBuilder as a third argument for fine control of GSON.
You can load your config with configInstance.load() and save it with configInstance.save(), producing the following JSON file.
{
"my_option": true
}
The fun doesn't stop there, there is also a utility method to help building YetAnotherConfigLib instances, (see the main wiki page for more details).
configInstance.buildConfig((configInstance, builder) -> /* return the builder here */)
NOTE: In 1.19.3 versions of YACL, the above has changed to the following:
YetAnotherConfigLib.create(configInstance, (defaults, config, builder) -> /* return the builder here */)
This method is useful because it does a few things:
  • Automatically adds the save runnable, so you don't need to do it.
  • Provides the config instance, allowing you to do some cool things:
Config instances, also store a default version of your config data that you can access, along with the current one. This is really useful when creating bindings because you don't need to mirror your defaults, once in YACL builder, once in actual field initialisation.
.binding(
config.getDefaults().myOption,
() -> config.getConfig().myOption,
val -> config.getConfig().myOption = val
)
Last modified 9mo ago