Skip to content

Commit

Permalink
refactor(config): migrate from Toml to Hocon configuration library
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Jul 30, 2024
1 parent 2907d86 commit b6d60ea
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 251 deletions.
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ preprocess {
def mc11904 = createNode("1.19.4", 1_19_04, "yarn")
def mc12001 = createNode("1.20.1", 1_20_01, "yarn")
def mc12004 = createNode("1.20.4", 1_20_04, "yarn")
def mc12100 = createNode("1.21", 1_21_00, "yarn")
def mc12100 = createNode("1.21.0", 1_21_00, "yarn")

mc11605.link(mc11802, null)
mc11802.link(mc11902, null)
Expand Down
2 changes: 1 addition & 1 deletion fabric/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
modImplementation include("dev.vankka:dependencydownload-runtime:1.3.1") {exclude module: 'jar-relocator'}
modImplementation include("dev.vankka:dependencydownload-common:1.3.1")
implementation ("cn.evole.onebot:OneBot-Client:${onebot_client_version}"){transitive = false}
implementation ("cn.evole.config:AtomConfig-Toml:${toml_version}"){transitive = false}
implementation ("org.spongepowered:configurate-hocon:${hocon_version}")

if (mcVersion == 11605) include ("org.slf4j:slf4j-api:2.0.7")
modCompileOnly ("maven.modrinth:vanish:1.5.4+1.20.1")
Expand Down
2 changes: 1 addition & 1 deletion fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ minecraft_version=1.20.1
loader_version=0.15.11
fabric_version=0.83.1+1.20.1
onebot_client_version=0.4.0
toml_version=0.1.5
hocon_version=4.1.2
http_core_version=4.4.16
apache_version=4.5.13
netty_version=4.1.97.Final
2 changes: 1 addition & 1 deletion fabric/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def versions = Arrays.asList(
"1.19.4",
"1.20.1",
"1.20.4",
"1.21",
"1.21.0",
)
for (String version : versions) {
include(":$version")
Expand Down
14 changes: 7 additions & 7 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public static boolean isLoad(String modId){
}

public static void sendAllGroupMsg(String message){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
for (long id : ModConfig.INSTANCE().getCommon().getGroupIdList()){
sendGroupMsg(id, message);
}
}

public static void sendAllGroupMsg(Callable<String> message){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
for (long id : ModConfig.INSTANCE().getCommon().getGroupIdList()){
sendGroupMsg(id, message);
}
}
Expand All @@ -66,7 +66,7 @@ public static void sendAllGroupMsg(Callable<String> message){
* @param player 玩家
*/
public static void sendAllGroupMsg(Callable<String> message, ServerPlayer player){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
for (long id : ModConfig.INSTANCE().getCommon().getGroupIdList()){
messageThread.submit(id, message, false, player);
}
}
Expand Down Expand Up @@ -107,10 +107,10 @@ public static void sendAllPlayerMsg(String message){
public static void wsConnect(){
McBot.onebot.close();//关闭线程
McBot.onebot = null;//强制为null
McBot.onebot = OneBotClient.create(ModConfig.INSTANCE.getBotConfig().build()).open().registerEvents(new IBotEvent());//重新实例化
ModConfig.INSTANCE.getStatus().setREnable(true);
ModConfig.INSTANCE.getCommon().setEnable(true);
ModConfig.INSTANCE.save();
McBot.onebot = OneBotClient.create(ModConfig.INSTANCE().getBotConfig().build()).open().registerEvents(new IBotEvent());//重新实例化
ModConfig.INSTANCE().getStatus().setREnable(true);
ModConfig.INSTANCE().getCommon().setEnable(true);
ModConfig.save();
McBot.connected = true;
}

Expand Down
12 changes: 8 additions & 4 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void init() {
FileUtil.checkFolder(CONFIG_FOLDER);
LIB_FOLDER = CONFIG_FOLDER.resolve("libs");
FileUtil.checkFolder(LIB_FOLDER);
CONFIG_FILE = CONFIG_FOLDER.resolve("config.toml");
CONFIG_FILE = CONFIG_FOLDER.resolve("config.hocon");
LibUtils.create(LIB_FOLDER, "libs.txt").download();//下载依赖
I18n.init();//初始化国际化
UserBindApi.load(CONFIG_FOLDER);//群服绑定
Expand All @@ -87,9 +87,13 @@ public void onServerStarting(MinecraftServer server) {
}

public void onServerStarted(MinecraftServer server) {
ModConfig.INSTANCE.save();
if (ModConfig.INSTANCE.getCommon().isAutoOpen()) {
onebot = OneBotClient.create(ModConfig.INSTANCE.getBotConfig().build()).open().registerEvents(new IBotEvent());
try {
ModConfig.load();
} catch (Exception e) {
Const.LOGGER.error("配置加载错误...");
}
if (ModConfig.INSTANCE().getCommon().isAutoOpen()) {
onebot = OneBotClient.create(ModConfig.INSTANCE().getBotConfig().build()).open().registerEvents(new IBotEvent());
connected = true;
}
CustomCmdHandler.INSTANCE.load();//自定义命令加载
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AddGroupIDCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val id = context.getArgument("GroupId", Long.class);
if (ModConfig.INSTANCE.getCommon().getGroupIdList().contains(id)) {
if (ModConfig.INSTANCE().getCommon().getGroupIdList().contains(id)) {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("QQ群号:" + id + "已经出现了!"), true);
//#elseif MC < 11900
Expand All @@ -24,7 +24,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("QQ群号:" + id + "已经出现了!"), true);
//#endif
} else {
ModConfig.INSTANCE.getCommon().addGroupId(id);
ModConfig.INSTANCE().getCommon().addGroupId(id);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("已成功添加QQ群号:" + id + "!"), true);
//#elseif MC < 11900
Expand All @@ -33,7 +33,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("已成功添加QQ群号:" + id + "!"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class AuthKeyCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val id = context.getArgument("AuthKey", String.class);
ModConfig.INSTANCE.getBotConfig().setToken(id);
ModConfig.INSTANCE().getBotConfig().setToken(id);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("已设置框架的AuthKey为:" + id), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("已设置Mirai框架的VerifyKey为:" + id), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("已设置Mirai框架的VerifyKey为:" + id), true);
//#endif
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class BotIDCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
long id = context.getArgument("BotId", Long.class);
ModConfig.INSTANCE.getBotConfig().setBotId(id);
ModConfig.INSTANCE().getBotConfig().setBotId(id);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("已设置机器人QQ号为:" + id + "!"), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("已设置机器人QQ号为:" + id + "!"), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("已设置机器人QQ号为:" + id + "!"), true);
//#endif
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
val parameter = context.getArgument("parameter", String.class);

if (ipv4Pattern.matcher(parameter).find() || ipv6Pattern.matcher(parameter).find()) {
ModConfig.INSTANCE.getBotConfig().setUrl(String.format("ws://%s", parameter));
ModConfig.INSTANCE.save();
ModConfig.INSTANCE().getBotConfig().setUrl(String.format("ws://%s", parameter));
ModConfig.save();
doConnect(context);
return 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DebugCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
ModConfig.INSTANCE.getCommon().setDebug(isEnabled);
ModConfig.INSTANCE().getCommon().setDebug(isEnabled);
if (isEnabled) {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("已开启开发者模式"), true);
Expand All @@ -34,7 +34,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("已关闭开发者模式"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class DelGroupIDCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val id = context.getArgument("GroupID", Long.class);
if (ModConfig.INSTANCE.getCommon().getGroupIdList().contains(id)) {
ModConfig.INSTANCE.getCommon().removeGroupId(id);
if (ModConfig.INSTANCE().getCommon().getGroupIdList().contains(id)) {
ModConfig.INSTANCE().getCommon().removeGroupId(id);
} else {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("QQ群号:" + id + "并未出现!"), true);
Expand All @@ -25,7 +25,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("QQ群号:" + id + "并未出现!"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("WebSocket目前未连接"), true);
//#endif
}
ModConfig.INSTANCE.getCommon().setEnable(false);
ModConfig.INSTANCE().getCommon().setEnable(false);
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
+ "2.请使用/mcbot addGroup <GroupId> 添加互通的群\n"
+ "3.请使用/mcbot setBot <BotId> 设置机器人的qq号\n"
+ "4.如果打开了鉴权验证,请输入/mcbot setAuthKey <AuthKey> 设置\n"
+ "5.准备工作完成,请使用/mcbot connect <cqhttp/mirai> <host:port> 与框架对接\n"
+ "5.准备工作完成,请使用/mcbot connect <host:port> 与框架对接\n"
+ "*************************************\n"
+ "在框架默认配置下,请使用/mcbot connect <cqhttp/mirai>\n"
+ "如果使用的cqhttp,配置完cqhttp后,本模组会自动连接\n"
+ "在框架默认配置下,请使用/mcbot connect\n"
+ "*************************************\n"
+ "全部命令:\n"
+ "/mcbot connect <cqhttp/mirai> <host:port>\n"
+ "/mcbot connect <host:port>\n"
+ "/mcbot addGroup <GroupId> 添加群\n"
+ "/mcbot delGroup <GroupId> 删除群\n"
+ "/mcbot addChannel <ChannelId> 添加群\n"
Expand Down Expand Up @@ -65,7 +64,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//#else
//$$ context.getSource().sendSuccess(Component.literal(toSend).append(urlC).append(endC), true);
//#endif
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//#else
//$$ context.getSource().sendSuccess(Component.literal(out.toString()), true);
//#endif
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ReConnectCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
ModConfig.INSTANCE.getBotConfig().setReconnect(isEnabled);
ModConfig.INSTANCE().getBotConfig().setReconnect(isEnabled);
if (isEnabled) {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("已设置自动重连"), true);
Expand All @@ -31,7 +31,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//$$ context.getSource().sendSuccess(Component.literal("已关闭自动重连"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ReceiveCommand {

public static int allExecute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
ModConfig.INSTANCE.getStatus().setREnable(isEnabled);
ModConfig.INSTANCE().getStatus().setREnable(isEnabled);
if (isEnabled) {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("全局接收群消息开关已被设置为打开"), true);
Expand All @@ -31,15 +31,15 @@ public static int allExecute(CommandContext<CommandSourceStack> context) throws
//$$ context.getSource().sendSuccess(Component.literal("全局接收群消息开关已被设置为关闭"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}

public static int chatExecute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
ModConfig.INSTANCE.getStatus().setRChatEnable(isEnabled);
ModConfig.INSTANCE().getStatus().setRChatEnable(isEnabled);
if (isEnabled) {
ModConfig.INSTANCE.getStatus().setREnable(true);
ModConfig.INSTANCE().getStatus().setREnable(true);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("接收群内聊天消息开关已被设置为打开"), true);
//#elseif MC < 11900
Expand All @@ -56,16 +56,16 @@ public static int chatExecute(CommandContext<CommandSourceStack> context) throws
//$$ context.getSource().sendSuccess(Component.literal("接收群内聊天消息开关已被设置为关闭"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;

}

public static int cmdExecute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
ModConfig.INSTANCE.getStatus().setRCmdEnable(isEnabled);
ModConfig.INSTANCE().getStatus().setRCmdEnable(isEnabled);
if (isEnabled) {
ModConfig.INSTANCE.getStatus().setREnable(true);
ModConfig.INSTANCE().getStatus().setREnable(true);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("接收群内命令消息开关已被设置为打开"), true);
//#elseif MC < 11900
Expand All @@ -82,7 +82,7 @@ public static int cmdExecute(CommandContext<CommandSourceStack> context) throws
//$$ context.getSource().sendSuccess(Component.literal("接收群内命令消息开关已被设置为关闭"), true);
//#endif
}
ModConfig.INSTANCE.save();
ModConfig.save();
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
public class ReloadConfigCmd {
public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
try {
ModConfig.INSTANCE.reload();
if (ModConfig.INSTANCE == null) {
ModConfig.reload();
if (ModConfig.INSTANCE() == null) {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("重载配置失败"), true);
//#elseif MC < 11900
Expand Down
Loading

0 comments on commit b6d60ea

Please sign in to comment.