# Config API

{% hint style="danger" %}
This is an archive of the old GitHub wiki! This will be re-written in the near future.
{% endhint %}

## 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.

```java
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`.

```java
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.

```json
{
  "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](https://github.com/isXander/YetAnotherConfigLib/wiki/home) for more details).

```java
configInstance.buildConfig((configInstance, builder) -> /* return the builder here */)
```

**NOTE: In 1.19.3 versions of YACL, the above has changed to the following:**

```java
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.

```java
.binding(
    config.getDefaults().myOption,
    () -> config.getConfig().myOption,
    val -> config.getConfig().myOption = val
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.isxander.dev/yet-another-config-lib/archive/config-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
