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}'
}
BetterConfig
A very powerful and easy to use command based configuration library for servers and clients.
Creating a simple configuration
To start, create a new class. This will be the class where all your configurations are stored. For this example, we'll
call this class Configs. Make sure that this class is public! Next, create a field for your configuration entry. The
field should not be final. Mark this field with the annotation @Config. The initial value of the field will be used as
default (fallback) value. You can add a comment by setting the comment attribute.
public class Configs {
@Config(comment = "This is an example!")
public static String exampleString = "default";
}
Finally, register your Configs class.
- For Fabric users, register the
Configsclass in your mod'sonInitialize(Client)method. Replace<mod id>with your mod's id. Sometimes you may omit the generics and just do<>instead.- On clients:
new ModConfigBuilder<FabricClientCommandSource, CommandBuildContext>(<mod id>, Configs.class).build(); - On servers:
new ModConfigBuilder<CommandSourceStack, CommandBuildContext>(<mod id>, Configs.class).build();
- On clients:
- For Paper users, register the
Configsclass in your plugin'sonEnablemethod. Replace<plugin name>with your plugin's name.new ModConfigBuilder<>(<plugin name>, Configs.class).build();
That's it! Now you can access exampleString through Configs.exampleString. You can edit exampleString by using the
config command.
- On Fabric there are different commands for the client and server. For both, replace
<mod id>with your mod's id.- On clients, execute
/cconfig <mod id> exampleString set <string>. - On servers, execute
/config <mod id> exampleString set <string>.
- On clients, execute
- On Paper servers, execute
/config <plugin name> exampleString set <string>. Replace<plugin name>with your plugin's name.
That's not all!
This mod also natively supports the use of Collections and Maps as variable types. These configurations will have
the options add, put and remove available. Moreover, you can define your own (de)serialisers to create
configurations with arbitrary types. To do this, all you have to do is register the (de)serialiser when you build your
config. For instance, to create configurations with type Block you can do
new ModConfigBuilder<>(<mod id>, Configs.class)
.registerTypeHierarchy(Block.class, new BlockAdapter(), BlockArgumentType::block)
.build();
where BlockAdapter extends TypeAdapter<Block> and BlockArgumentType implements ArgumentType<Block>. See
these tests for a complete picture. An identical setup for Paper can
be found here.
Furthermore, you can completely change the behaviour of updating your config values by creating your own methods. Simply
add one or more of setter, adder, putter or remover as attribute to your @Config annotation. A great use for
this would be adding key-value entries to a Map based on a single value. Consider the following configuration.
@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));
}
The value of "none" for the putter indicates that no putter will be available. This way, you can use this Map in your
code like usual, and add values to it using /(c)config <mod id> exampleMapAdder add <string>. For more details, see
the JavaDocs for @Config.
The parameters of the update method can also be customised.
@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));
}
For putters, there are separate key and value type attributes.
And many more things! For some illustrative examples, see the Configs class for both
Fabric and
Paper.
Installation
Replace ${betterconfig_version} with the artifact version.
You may choose between my own maven repository and GitHub's package repository.
My own
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")
}
}
}
Import it:
dependencies {
// Fabric
include modImplementation('dev.xpple:betterconfig-fabric:${betterconfig_version}')
// Paper (also include the JAR in the plugins folder)
compileOnly 'dev.xpple:betterconfig-paper:${betterconfig_version}'
}
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
下载 BetterConfig
- 大幅改进了
Config类的文档
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
插件
PacketEvents 是一款专为 Minecraft Java 版打造的协议库,旨在简化数据包的处理与传输。
插件
ExyliaPlugins 的库。
插件
重新定义服务器上的生物运输方式。使用高度可定制的口袋道具,安全地捕获、存储和释放任何实体。
插件
为基于 Spigot/Paper/Folia 的服务器添加对 Vivecraft 的支持
插件
一个强大的 API,用于为你的 Minecraft 服务器创建自定义进度。
插件
SkyBlock、OneBlock、Boxed、SkyGrid、AcidIsland、CaveBlock、Poseidon、StrangerRealms——以及更多!这些只是由 BentoBox 支持的基于岛屿的游戏模式之一。来自原版 ASkyBlock 的作者。
评论 0
登录后参与评分
还没有评论,来抢沙发吧