未命名公共库(UCL)

未命名公共库(UCL)

非常简单易用,完美适配你的模组的配置库!

基础库

English

❔ 了解更多

Unnamed Common Library (UCL) 是我尝试制作的一个简单、实用(未来会变得强大)的配置库。它的目标是通用性,因此无论你构建什么模组或有什么偏好,你都可以使用它。这个库显然不打算单独使用,如果你这样做,它最多只能作为其能力的示例和测试。

🤩 为什么你应该喜欢它

  • 实现和使用非常简单
  • 可选的配置屏幕集成
  • 支持 JSON、TOML 和 YAML

🧑‍💻 面向开发者

安装

将仓库添加到你的 build.gradle(.kts):

repositories {
    maven {
        url = "https://maven.pkg.github.com/aurorarissime/unnamed-common-library"
    }
}

然后在你 的构建脚本中添加依赖(Fabric 使用 modImplementation,NeoForge 使用 implementation):

dependencies {
    (mod)Implementation("from.discorde:ucl:${ucl_version}+${minecraft_version}-fabric")
}
用法

1. 创建你的配置模型

MyModConfig.java

import from.discorde.ucl.common.config.annotations.*;

@Configuration(
    name = "mymod"
)
public class MyModConfig {
    @Comment("Enable the awesome feature")
    public boolean awesomeFeature = true;

    @Comment("Maximum power level (0-100)")
    public int maxPower = 50;

    @Comment("Welcome message displayed to players")
    public String welcomeMessage = "Hello!";
}

2. 在模组初始化器中加载配置

MyMod.java

public class MyMod implements ModInitializer {
    // 包装类在编译时自动生成
    public static final MyModConfigWrapper CONFIG = MyModConfigWrapper.createAndLoad();

    @Override
    public void onInitialize() {
        if (CONFIG.awesomeFeature) {
            System.out.println("Awesome feature is enabled!");
        }
    }
}
参考

注解

@Configuration

将此注解添加在你的配置类正上方

参数 类型 默认值 描述
name String 必填 配置文件名或文件夹名
location ConfigurationLocation SUBDIRECTORY 保存配置的位置
format ConfigurationFormat JSON5 使用的文件格式
screen boolean true 生成 YACL 配置屏幕

@Comment

将此注解添加在任何字段上方,以在配置文件中包含注释

@Comment("This comment will appear above the field in the config file")
public boolean myOption = true;

枚举

ConfigurationLocation

值 描述
ROOT 直接保存在配置文件夹中,文件名为 {name}.{ext}
SUBDIRECTORY 保存在子文件夹中,文件名为 {name}/configuration.{ext}

ConfigurationFormat

值 扩展名 描述
JSON5 .json5 支持注释、尾随逗号和无引号键的 JSON
TOML .toml Tom's Obvious Minimal Language
YAML .yaml YAML Ain't Markup Language
配置屏幕集成

如果你的 @Configuration 注解中 screen = true(默认值),并且安装了 YetAnotherConfigLib (YACL),则会自动生成配置屏幕,并可通过 Mod Menu 访问。
要禁用配置屏幕,只需在 @Configuration 注解中将 screen 参数设置为 false。

Enjoy 🙂