> For the complete documentation index, see [llms.txt](https://leawinds-organization.gitbook.io/yetanotherconfiglib-jian-ti-zhong-wen-fei-guan-fang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leawinds-organization.gitbook.io/yetanotherconfiglib-jian-ti-zhong-wen-fei-guan-fang/pei-zhi-api-config-api/config-api-de-ji-chu-yong-fa.md).

# Config API的基础用法

了解Config API是做什么的。

{% hint style="danger" %}
这不是官方文档！官方文档位于 [https://docs.isxander.dev](https://docs.isxander.dev/yet-another-config-lib/)
{% endhint %}

## 这是什么？

随着YACL的主要功能，GUI，这个mod还提供了一个超级简单的方法来管理你的mod的配置。它可能是你制作的每个mod中乏味的一部分，这就是创建配置API的原因!

您所需要做的就是创建一个类，添加一些字段，并告诉YACL如何保存和加载配置。

## 一个示例

```java
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!";
    
}
```

就是这样！这就是配置所需的所有设置。

在此示例中，您可以看到我们告诉配置处理程序使用 GSON 来序列化您的字段，这意味着不仅原始/基本类型可以工作，任何类型都可以！ 您甚至可以看到我修改 GSON 构建器。 **仅考虑使用`@SerialEntry`注释的字段。**

## 保存和加载

在上面的示例中，你可以看到我们像这样指定了序列化器：

```java
.serializer(config -> GsonConfigSerializerBuilder.create(config)
        .setPath(YACLPlatform.getConfigDir().resolve("my_mod.json5"))
        .appendGsonBuilder(GsonBuilder::setPrettyPrint) // not needed, pretty print by default
        .setJson5(true)
        .build())
```

您可以看到我们指定了 GSON 序列化器，设置了其路径，配置了 GSON，并指定使用 JSON5 规范。 目前，GSON 是唯一可供您使用的序列化器。 您可以创建自己的，稍后会详细介绍。

要保存和加载，请使用受人尊敬的方法：

```java
MyConfig.HANDLER.save();
MyConfig.HANDLER.load();
```
