Fabric Yaml Configuration

模组

· 作者 EarWarm

用于使用 YAML 创建配置的模组

0 本站下载 0 关注 更新于 3 年前

最新 1.0.1 · 1.21.10 · Fabric

一键开服(送7天)

Fabric Yaml 配置库模组

本模组允许你使用 YAML 配置文件作为模组的配置。 该模组已内置 snakeyaml 库。

FabricConfiguration 方法

// 设置最终配置文件的路径
setFile(File file)
setFile(String file)

// 设置原始配置文件的输入流
// 通常原始配置文件位于模组资源中。
setDefault(InputStream defaultConfigurationFile)
        
// 初始化配置
intialize()

// 将配置保存到文件
saveConfiguration()
        
// 从文件重新加载配置
// 用于在模组运行期间更新配置。
relaodConfiguration()

简单配置示例

assets/example-mod/config/example-mod.yml

# 测试分支
test:
  integer: 1
  double: 1.1
  string: 'test'
  list:
    - 'test-1'
    - 'test-2'
    - 'test-3'
  section:
    test: 'test'
public class ExampleMod implements ModInitializer {

    public static final Logger LOGGER = LogManager.getLogger();
    public FabricConfiguration configuration = new FabricConfiguration();

    @Override
    public void onInitialize() {
        // 从模组资源获取默认配置文件
        InputStream defaultConfigurationFile = ExampleMod.class.getResourceAsStream("/assets/example-mod/config/example-mod.yml");
        
        // 将默认配置文件设置到配置对象中
        configuration.setDefault(defaultConfigurationFile);
        // 设置配置文件路径
        configuration.setFile("config/example-mod.yml");
        // 初始化配置(如果文件存在则加载,否则生成配置)
        try {
            configuration.initialize();
        } catch (IOException e) {
            LOGGER.error("无法生成配置文件", e);
        } catch (InvalidConfigurationException e) {
            LOGGER.error("无法加载配置文件", e);
        }
        
        
    }
}

代码示例(保存和从配置中读取 ItemStack)

ItemStack exampleItemStack = new ItemStack(Items.BEDROCK);
NbtCompound itemStackNbt = exampleItemStack.writeNbt(new NbtCompound());
        
configuration.set("item-stack", NbtHelper.toNbtProviderString(itemStackNbt));
        
try {
    configuration.saveConfiguration();
} catch (IOException e) {
    LOGGER.error("无法保存配置", e);
}

ItemStack itemStack = null;
try {
    itemStack = ItemStack.fromNbt(NbtHelper.fromNbtProviderString(configuration.getString("item-stack")));
} catch (CommandSyntaxException e) {
    LOGGER.error("无法从配置中加载 ItemStack", e);
}

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源