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);
}
你是这个资源的作者?登录后可认领此资源。
相似资源
Woot Revived
pharuxtan
一个用于收集生物掉落物的多区块模组。
生物科技
355
玉Jade ?
Snownee
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
实用前置
344
物品管理器Just Enough Items (JEI)
mezz
查看物品与配方
前置实用
240
KubeJS
Lat
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
前置实用
239
AAA Particles
ChloePrime
适用于 MC 1.18+ 的 Effekseer 加载器
前置装饰
186
WTHIT
deirn
那是什么?一个 Waila/Hwyla 的分支。
前置实用
165
评论 0
登录后参与评分
还没有评论,来抢沙发吧