BetterConfig
一个功能强大且易于使用的基于命令的配置库,适用于服务器和客户端。
创建简单配置
首先,创建一个新的类。这个类将用于存储所有配置项。在本例中,我们将此类命名为 Configs。请确保该类是 public 的!接下来,为你的配置条目创建一个字段该字段不应为 final。使用 @Config 注解标记此字段。字段的初始值将用作默认(回退)值。你可以通过设置 comment 属性来添加注释。
public class Configs {
@Config(comment = "这是一个示例!")
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>替换为你的插件名称。
不止于此!
此模组原生支持将 Collections 和 Maps 用作变量类型。这些配置将提供 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 包含在 plugins 文件夹中)
compileOnly 'dev.xpple:betterconfig-paper:${betterconfig_version}'
}
你是这个资源的作者?登录后可认领此资源。
相似资源
PacketEvents 是一款专为 Minecraft Java 版打造的协议库,旨在简化数据包的处理与传输。
ExyliaPlugins 的库。
重新定义服务器上的生物运输方式。使用高度可定制的口袋道具,安全地捕获、存储和释放任何实体。
为基于 Spigot/Paper/Folia 的服务器添加对 Vivecraft 的支持
一个强大的 API,用于为你的 Minecraft 服务器创建自定义进度。
SkyBlock、OneBlock、Boxed、SkyGrid、AcidIsland、CaveBlock、Poseidon、StrangerRealms——以及更多!这些只是由 BentoBox 支持的基于岛屿的游戏模式之一。来自原版 ASkyBlock 的作者。
评论 0
登录后参与评分
还没有评论,来抢沙发吧