SuperMartijn642's Config Lib 允许你仅指定一次配置,随后它将自动处理世界加载之间的数值重载、与客户端同步数值,并自行生成仅适用于客户端或服务端的数值。
创建配置:
配置使用 ModConfigBuilder 创建。
只需使用 #ModConfigBuilder() 创建一个新实例即可。
ModConfigBuilder builder = new ModConfigBuilder();
可以使用 ModConfigBuilder#define 向配置中添加值,该方法需要传入名称和默认值。
对于整数(Integer)和双精度浮点数(Double)类型的值,还需要提供最小值和最大值。
ModConfigBuilder#define 返回一个 Supplier,应将其存储以便从配置中获取该值。
Supplier<Boolean> booleanValue = builder.define( "booleanValue", true );
Supplier<Integer> integerValue = builder.define( "integerValue", 5, 0, 10 );
Supplier<Double> doubleValue = builder.define( "doubleValue", 0.5, 0, 1);
Supplier<ExampleEnum> enumValue = builder.define( "enumValue", ExampleEnum.VALUE_1 );
可以在定义值之前调用 ModConfigBuilder#comment(String) 来为值添加注释。
Supplier<Boolean> valueWithComment = builder.comment( "this is a comment for 'valueWithComment'" ).define( "valueWithComment ", true );
默认情况下,值会在世界加载时重新加载。
可以通过在定义值之前调用 ModConfigBuilder#gameRestart() 将其更改为仅在 Minecraft 启动时重新加载。
Supplier<Boolean> notReloadedValue = builder.comment( "this is value will not be reloaded" ).define( "notReloadedValue", true );
COMMON 或 SERVER 配置中的值默认会与客户端同步,如需阻止此行为,请使用 ModConfigBuilder#dontSync()。
Supplier<Boolean> notSynchronizedValue = builder.comment( "this is value will not be synchronized" ).define( "notSynchronizedValue", true );
还可以将值放入分类中。
ModConfigBuilder#push(String) 用于压入一个分类,ModConfigBuilder#pop() 用于弹出分类。
builder.push( "special" );
Supplier<Boolean> specialValue = builder.comment( "this value is in the 'special' category" ).define( "specialValue", true );
builder.pop();
可以使用 ModConfigBuilder#categoryComment(String) 为当前活动的分类添加注释。
builder.push( "client" ).categoryComment( "this, is a comment for the 'client' category" );
定义所有值后,必须调用 ModConfigBuilder#build() 来完成配置。
builder.build();
现在,配置中的值将自动重新加载和同步,并且可以使用存储的 Supplier 实例来获取这些值。
这适用于所有可用版本,包括 Minecraft 1.12、1.14、1.15、1.16、1.17、1.18 和 1.19。
示例模组:
有关如何使用 Config Lib 的具体示例,请查看 示例模组。
常见问题解答
我可以在我的整合包中使用你的模组吗?
是的,欢迎在你的整合包中使用我的模组。
Discord
如需了解后续内容、即将推出的模组以及参与讨论,欢迎加入 SuperMartijn642 的 Discord 服务器!
SuperMartijn642's Config Lib allows you to specify a config once and it then handles reloading values between world loads, syncing values with clients, and generating values for client- or server-only on its own.
Creating a config:
A config is created using a ModConfigBuilder.
Simply create a new instance using #ModConfigBuilder().
ModConfigBuilder builder = new ModConfigBuilder();
A value can be added to the config with ModConfigBuilder#define which takes a name and a default value.
For integer and double values a minimum and maximum value are also required.
ModConfigBuilder#define returns a Supplier which should be stored to retrieve the value from the config.
Supplier<Boolean> booleanValue = builder.define( "booleanValue", true );
Supplier<Integer> integerValue = builder.define( "integerValue", 5, 0, 10 );
Supplier<Double> doubleValue = builder.define( "doubleValue", 0.5, 0, 1);
Supplier<ExampleEnum> enumValue = builder.define( "enumValue", ExampleEnum.VALUE_1 );
A comment can be added to a value by calling ModConfigBuilder#comment(String) before defining the value.
Supplier<Boolean> valueWithComment = builder.comment( "this is a comment for 'valueWithComment'" ).define( "valueWithComment ", true );
By default values are reloaded when world is loaded.
This can be changed to only reload a value when Minecraft launches by calling ModConfigBuilder#gameRestart() before defining the value.
Supplier<Boolean> notReloadedValue = builder.comment( "this is value will not be reloaded" ).define( "notReloadedValue", true );
Values in COMMON or SERVER configs are synchronized with clients by default, to prevent this use ModConfigBuilder#dontSync().
Supplier<Boolean> notSynchronizedValue = builder.comment( "this is value will not be synchronized" ).define( "notSynchronizedValue", true );
Values can also be put into categories.
ModConfigBuilder#push(String) pushes a category and ModConfigBuilder#pop() pops a category.
builder.push( "special" );
Supplier<Boolean> specialValue = builder.comment( "this value is in the 'special' category" ).define( "specialValue", true );
builder.pop();
A comment can be added to the active category using ModConfigBuilder#categoryComment(String).
builder.push( "client" ).categoryComment( "this, is a comment for the 'client' category" );
After defining all values ModConfigBuilder#build() must be called to finish the config.
builder.build();
Now the values in your config will reloaded and synced automatically and the values can be retrieved using the stored Supplier instances.
This will work for all available versions, that includes Minecraft 1.12, 1.14, 1.15, 1.16, 1.17, 1.18, and 1.19.
Example Mod:
For a concrete example of how to use Config Lib checkout the example mod.
FAQ
Can I use your mod in my modpack?
Yes, feel free to use my mod in your modpack
Discord
For future content, upcoming mods, and discussion, feel free to join the SuperMartijn642 discord server!
评论 0
登录后参与评分
还没有评论,来抢沙发吧