DumbassConfigLib 是一个配置库,用于我的模组。如果你是模组开发者,你可以轻松地在自己的项目中集成此库。
Java API
要开始使用此库,首先将其包含在你的 build.gradle 中:
repositories {
...
maven {
name = "KaseToatz"
url = "https://maven.kasetoatz.com/"
}
}
dependencies {
modCompileOnly "com.kasetoatz:dumbassconfiglib:${project.dacl_version}"
}
然后在你的 gradle.properties 中添加 dacl_version,你可以在 版本页面 查看版本号。
创建选项
首先创建模组的入口类,确保它实现了 ModInitializer。然后你可以将选项作为公共静态字段添加到该类中,以便稍后访问。你可以这样创建选项:
public static final BoolOption TEST_BOOL_OPT = new BoolOption(Component.literal("Test boolean option"), "test_bool_opt", false);
public static final IntOption TEST_INT_OPT = new IntOption(Component.literal("Test integer option"), "test_int_opt", 1);
public static final FloatOption TEST_FLOAT_OPT = new FloatOption(Component.literal("Test float option"), "test_float_opt", 1.F);
第一个参数是配置 UI 中显示的文本,第二个参数是选项的唯一标识符,第三个参数是默认值。
然后将此类添加到你的 fabric.mod.json 中,使其在启动时加载:
"entrypoints": {
"main": [
"com.kasetoatz.mymod.MyMod"
]
}
如果你的模组仅适用于客户端或服务器端,你可以将 main 替换为 client 或 server。
验证器
对于带有输入字段的选项,你可以添加第四个参数 AbstractValidator 来限制选项的值。要创建一个必须在 1 到 100 之间的整数选项,我们可以这样做:
public static final IntOption CLAMPED_INT_OPT = new IntOption(Component.literal("Clamped integer option"), "clamp_int_opt", 50, new RangeValidator<>(1, 100));
你也可以在 RangeValidator 中将下限或上限传递为 null 以移除该限制。
构建配置
当你完成创建选项后,是时候实际构建你的配置了。你可以使用 DumbassConfig 的静态 builder 方法来实现,如下所示:
public static final DumbassConfig CONFIG = DumbassConfig.builder(Component.literal("Test Screen Title"), "filename.json")
.withOption(TEST_BOOL_OPT)
.withOption(TEST_INT_OPT)
.withOption(TEST_FLOAT_OPT)
.build()
构建配置后,当配置被修改时,相应的 AbstractOption 的值也会改变,因此你始终可以通过以下方式访问它们:
bool enabled = TEST_BOOL_OPT.getValue();
子选项
你还可以添加子选项,这基本上是一个拥有自己选项的全新屏幕。要创建这个,我们可以使用 SubOption 类的 builder 方法。我们将使用 CLAMPED_INT_OPT 作为一个子选项。
public static final SubOption TEST_SUB_OPT = SubOption.builder(Component.literal("Sub Screen Title"), "sub_opt")
.withOption(CLAMPED_INT_OPT)
.build();
我们现在也可以将此选项添加到配置构建器中。我们可以根据需要嵌套这些子选项任意深度。
Modmenu 支持
要将我们新创建的配置屏幕添加到 modmenu,我们必须首先将其作为依赖项添加到我们的模组中,你可以在此处阅读如何操作 这里。
现在为我们的模组菜单集成创建一个新类,在此类中实现 ModMenuApi 并重写 getModConfigScreenFactory 以返回我们的配置 UI。
public class ModMenuIntegration implements ModMenuApi
{
private final DumbassConfigScreen screen = new DumbassConfigScreen(CONFIG);
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory()
{
return screen::get;
}
}
现在我们可以修改我们的 fabric.mod.json 以将新类设置为 modmenu 入口点:
"entrypoints": {
"main": [
"com.kasetoatz.mymod.MyMod"
],
"modmenu": [
"com.kasetoatz.mymod.ModMenuIntegration"
]
}
完整示例
public class MyMod implements ModInitializer
{
public static final BoolOption TEST_BOOL_OPT = new BoolOption(Component.literal("Test boolean option"), "test_bool_opt", false);
public static final IntOption TEST_INT_OPT = new IntOption(Component.literal("Test integer option"), "test_int_opt", 1);
public static final FloatOption TEST_FLOAT_OPT = new FloatOption(Component.literal("Test float option"), "test_float_opt", 1.F);
public static final IntOption CLAMPED_INT_OPT = new IntOption(Component.literal("Clamped integer option"), "clamp_int_opt", 50, new RangeValidator<>(1, 100));
public static final SubOption TEST_SUB_OPT = SubOption.builder(Component.literal("Sub Screen Title"), "sub_opt")
.withOption(CLAMPED_INT_OPT)
.build();
public static final DumbassConfig CONFIG = DumbassConfig.builder(Component.literal("Test Screen Title"), "filename.json")
.withOption(TEST_BOOL_OPT)
.withOption(TEST_INT_OPT)
.withOption(TEST_FLOAT_OPT)
.withOption(TEST_SUB_OPT)
.build()
@Override
public void onInitialize()
{
...
}
}
public class ModMenuIntegration implements ModMenuApi
{
private final DumbassConfigScreen screen = new DumbassConfigScreen(CONFIG);
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory()
{
return screen::get;
}
}
正在寻找一个有趣的《Minecraft》游玩地点,并体验我的一些模组?来 El Basurero SMP 聚聚吧!
- IP:
play.ebsmp.com
- 推荐版本: 26.2(其他版本也可以使用!)
- 基岩版: 支持
- 额外福利: 在游戏中实时体验大部分我的服务端模组!
更多信息请查看 文档!
DumbassConfigLib is a config library that is used for my mods. If you're a mod developer you can easily implement this library yourself.
Java API
To start using the library first include it in your build.gradle:
repositories {
...
maven {
name = "KaseToatz"
url = "https://maven.kasetoatz.com/"
}
}
dependencies {
modCompileOnly "com.kasetoatz:dumbassconfiglib:${project.dacl_version}"
}
Then add dacl_version to your gradle.properties, you can check the version on the versions page.
Creating options
Start by creating your mod entrypoint, make sure it implements ModInitializer. You can then add your options as public static fields to this class to access them later. You can create options like so:
public static final BoolOption TEST_BOOL_OPT = new BoolOption(Component.literal("Test boolean option"), "test_bool_opt", false);
public static final IntOption TEST_INT_OPT = new IntOption(Component.literal("Test integer option"), "test_int_opt", 1);
public static final FloatOption TEST_FLOAT_OPT = new FloatOption(Component.literal("Test float option"), "test_float_opt", 1.F);
The first parameter is the text displayed in the config UI, the second parameter is the unique identifier of the option and the third parameter is the default value.
Then add this class to your fabric.mod.json so it loads at start:
"entrypoints": {
"main": [
"com.kasetoatz.mymod.MyMod"
]
}
You can replace main with client or server if your mod is client/server side only.
Validators
For options with an input field you can add a fourth parameter with an AbstractValidator to add restrictions to the option's value. To create an integer option that must be between 1 and 100 we can do that like this:
public static final IntOption CLAMPED_INT_OPT = new IntOption(Component.literal("Clamped integer option"), "clamp_int_opt", 50, new RangeValidator<>(1, 100));
You can also pass null to either the lower or higher bound in RangeValidator to remove that restriction.
Building the config
When you're finished creating your options it's time to actually build your config. You can do this using the static builder method of DumbassConfig like so:
public static final DumbassConfig CONFIG = DumbassConfig.builder(Component.literal("Test Screen Title"), "filename.json")
.withOption(TEST_BOOL_OPT)
.withOption(TEST_INT_OPT)
.withOption(TEST_FLOAT_OPT)
.build()
After building the config, the respective AbstractOption's values are changed when the config is modified, so you can always access them by doing:
bool enabled = TEST_BOOL_OPT.getValue();
Sub options
You can also add sub options that are basically an entirely new screen with it's own options. To create this we can use the builder method of the SubOption class. We will use CLAMPED_INT_OPT as one of the sub options.
public static final SubOption TEST_SUB_OPT = SubOption.builder(Component.literal("Sub Screen Title"), "sub_opt")
.withOption(CLAMPED_INT_OPT)
.build();
We can now add this option to the config builder aswell. We can nest these sub options however deep we want.
Modmenu support
To add our newly created config screen to modmenu, we must first add it as a dependency to our mod, you can read how to do so here.
Now we create a new class for our mod menu integration, implement ModMenuApi into this class and override getModConfigScreenFactory to return our config UI.
public class ModMenuIntegration implements ModMenuApi
{
private final DumbassConfigScreen screen = new DumbassConfigScreen(CONFIG);
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory()
{
return screen::get;
}
}
Now we can modify our fabric.mod.json to set our new class as a modmenu entrypoint:
"entrypoints": {
"main": [
"com.kasetoatz.mymod.MyMod"
],
"modmenu": [
"com.kasetoatz.mymod.ModMenuIntegration"
]
}
Full Example
public class MyMod implements ModInitializer
{
public static final BoolOption TEST_BOOL_OPT = new BoolOption(Component.literal("Test boolean option"), "test_bool_opt", false);
public static final IntOption TEST_INT_OPT = new IntOption(Component.literal("Test integer option"), "test_int_opt", 1);
public static final FloatOption TEST_FLOAT_OPT = new FloatOption(Component.literal("Test float option"), "test_float_opt", 1.F);
public static final IntOption CLAMPED_INT_OPT = new IntOption(Component.literal("Clamped integer option"), "clamp_int_opt", 50, new RangeValidator<>(1, 100));
public static final SubOption TEST_SUB_OPT = SubOption.builder(Component.literal("Sub Screen Title"), "sub_opt")
.withOption(CLAMPED_INT_OPT)
.build();
public static final DumbassConfig CONFIG = DumbassConfig.builder(Component.literal("Test Screen Title"), "filename.json")
.withOption(TEST_BOOL_OPT)
.withOption(TEST_INT_OPT)
.withOption(TEST_FLOAT_OPT)
.withOption(TEST_SUB_OPT)
.build()
@Override
public void onInitialize()
{
...
}
}
public class ModMenuIntegration implements ModMenuApi
{
private final DumbassConfigScreen screen = new DumbassConfigScreen(CONFIG);
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory()
{
return screen::get;
}
}
Looking for a fun place to play Minecraft and try out some of my mods? Come hang out on the El Basurero SMP!
- IP:
play.ebsmp.com
- Recommended Version: 26.2 (other versions work too!)
- Bedrock Edition: Supported
- Bonus: Test out most of my server-side mods live in-game!
For more info check the docs!
评论 0
登录后参与评分
还没有评论,来抢沙发吧