跳到正文

Config X

作者:Norcleeh

A fabric API library for plugins to load & save configurations

收藏

介绍

中文介绍正在准备中,下面暂时显示原文。

Config X

Author
LICENSE
Stars
Github Workflow Status

Description

This is a fabric API library for plugins to load & save configurations.

Installation

Add the following to your build script:

repositories {
    maven {
        url = uri("https://repo.codemc.io/repository/tetoe-mc")
    }
}

dependencies {
    // Approach #1: Ensure Config X is always available by including it within your own jar
    include(modImplementation("space.nocp:configx:2.0.1"))

    // Approach #2: Depend on Config X, but require that users install it themselves
    modImplementation "space.nocp:configx:2.0.1"
}

Then refresh your project.

Usage

Remember to import the package before starting.

import space.nocp.configx.api.*;

Register a configuration

Before registering a configuration, you need to write a class describing the configuration object and prepare a default configuration. Here, let’s call the class ConfigType.

// ...
Configuration config = ConfigManager.get().register("my-config", defaultConfig, ConfigType.class);

Get a configuration

After registering your configuration, you can use config() method to get the configuration at anywhere in your project.

Configuration config = ConfigManager.get().config("my-config");

Get & set the configuration object

Configuration config = ...;
ConfigType configObj = config.get();

configObj.myValue = "Hello World";
config.set(configObj);
config.save(); // Remember to save the config after setting it

Load the configuration from the file system manually

Usually, the configuration is always up-to-date with the one storing in the file system. But sometimes, you may want to refresh it manually.

Configuration config = ...;
config.load();

Get the info of the configuration

Configuration config = ...;

String id = config.id; // my-config
config.getFileName(); // my-config.json
config.getPath().toString(); // C:/path/to/config/my-config.json

Use custom Gson to parse JSON

Config X stores your configuration in the JSON format. When you stores objects that requires custom serializers and deserializers, you need to build your own Gson to achieve this.

Before registering your configuration, you need to prepare your Gson in advance. Here, let’s assume we already have a custom Gson object called myGson.

Just simply add the Gson object as a parameter after all the parameters in register() method, and the parsing operations will be done with the provided Gson.

// ...
Configuration config = ConfigManager.get().register("my-config", defaultConfig, ConfigType.class, myGson);

For more information about custom serializer and deserializer in Gson, see the article: Building a Personalized Serializer and Deserializer using Java Gson Library

Build from source

git clone https://github.com/tetoe-mc/configx.git
cd configx
./gradlew build

Then you can get the jar file in the build folder.

LICENSE

CC0 1.0

常见问题

Config X 支持哪些 Minecraft 版本?

Config X 支持 Minecraft Java 版 1.21.4–1.21.5,共 2 个版本,可用的加载器:Fabric。下载时注意选择和你游戏版本一致的文件。

Config X 怎么安装?

先用 PCL2 或 HMCL 启动器安装对应版本的 Fabric,再把下载的 .jar 文件放进游戏目录的 mods 文件夹,重新启动游戏即可。开启了版本隔离的,mods 文件夹在 versions/版本名 下面。

评论

登录后评论和评分

Norcleeh 的其他作品

查看全部 →