
更好的配置
一个非常强大且易于使用的、基于命令的服务器和客户端配置库。
BetterConfig
一个功能强大且易于使用的基于命令的配置库,适用于服务端和客户端。
创建一个简单配置
首先,创建一个新类。这将是你存储所有配置的类。在本示例中,我们将此类命名为 Configs。请确保该类是 public 的!接下来,为你的配置项创建一个字段。该字段不应为 final。使用注解 @Config 标记此字段。字段的初始值将作为默认(后备)值。你可以通过设置 comment 属性来添加注释。
public class Configs {
@Config(comment = "This is an example!")
public static String exampleString = "default";
}
最后,注册你的 Configs 类。
- 对于 Fabric 用户,请在你的模组的
onInitialize(Client)方法中注册Configs类。将<mod id>替换为你的模组 ID。有时你可以省略泛型,直接使用<>即可。- 在客户端上:
new ModConfigBuilder<FabricClientCommandSource, CommandBuildContext>(<mod id>, Configs.class).build(); - 在服务端上:
new ModConfigBuilder<CommandSourceStack, CommandBuildContext>(<mod id>, Configs.class).build();
- 在客户端上:
- 对于 Paper 用户,请在你的插件的
onEnable方法中注册Configs类。将<plugin name>替换为你的插件名称。new ModConfigBuilder<>(<plugin name>, Configs.class).build();
就这样!现在你可以通过 Configs.exampleString 访问 exampleString。你可以使用配置命令来编辑 exampleString。
- 在 Fabric 上,客户端和服务端有不同的命令。对于两者,都需要将
<mod id>替换为你的模组 ID。- 在客户端上,执行
/cconfig <mod id> exampleString set <string>。 - 在服务端上,执行
/config <mod id> exampleString set <string>。
- 在客户端上,执行
- 在 Paper 服务端上,执行
/config <plugin name> exampleString set <string>。将<plugin name>替换为你的插件名称。
这还不是全部!
此模组还原生支持使用 Collection 和 Map 作为变量类型。这些配置将具有 add、put 和 remove 选项。此外,你可以定义自己的(反)序列化器,以创建任意类型的配置。为此,你只需在构建配置时注册(反)序列化器即可。例如,要创建类型为 Block 的配置,你可以这样做:
new ModConfigBuilder<>(<mod id>, Configs.class)
.registerTypeHierarchy(Block.class, new BlockAdapter(), BlockArgumentType::block)
.build();
其中 BlockAdapter 继承自 TypeAdapter<Block>,BlockArgumentType 实现 ArgumentType<Block>。请参阅这些测试以获取完整示例。Paper 的相同设置可以在此处找到。
此外,你可以通过创建自己的方法来完全更改更新配置值的行为。只需在 @Config 注解中添加 setter、adder、putter 或 remover 中的一个或多个属性。一个很好的用途是基于单个值向 Map 添加键值对。考虑以下配置:
@Config(putter = @Config.Putter("none"), adder = @Config.Adder("customMapAdder"))
public static Map<String, String> exampleMapAdder = new HashMap<>(Map.of("a", "A", "b", "B"));
public static void customMapAdder(String string) {
exampleMapAdder.put(string.toLowerCase(Locale.ROOT), string.toUpperCase(Locale.ROOT));
}
putter 的值 "none" 表示不提供 putter。这样,你可以像平常一样在代码中使用这个 Map,并使用 /(c)config <mod id> exampleMapAdder add <string> 向其添加值。更多详情,请参阅@Config 的 JavaDocs。
更新方法的参数也可以自定义。
@Config(adder = @Config.Adder(value = "customTypeAdder", type = int.class))
public static Collection<String> exampleCustomType = new ArrayList<>(List.of("%", "@"));
public static void customTypeAdder(int codepoint) {
exampleCustomType.add(Character.toString(codepoint));
}
对于 putter,有单独的键和值类型属性。
还有更多功能!有关一些示例,请参阅 Fabric 和 Paper 的 Configs 类。
安装
将 ${betterconfig_version} 替换为制品版本。
你可以选择使用我自己的 Maven 仓库或 GitHub 的包仓库。
我自己的
repositories {
maven {
url 'https://maven.xpple.dev/maven2'
}
}
GitHub packages
repositories {
maven {
url 'https://maven.pkg.github.com/xpple/BetterConfig'
credentials {
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
}
}
}
导入它:
dependencies {
// Fabric
include modImplementation('dev.xpple:betterconfig-fabric:${betterconfig_version}')
// Paper(也将 JAR 包含在插件文件夹中)
compileOnly 'dev.xpple:betterconfig-paper:${betterconfig_version}'
}
正在加载版本记录…
正在加载评论…
评论在新手盒子客户端中发表,这里同步展示。