Skip to content

Commit

Permalink
+ toml配置
Browse files Browse the repository at this point in the history
+ 监控配置更新
+ i18优化
  • Loading branch information
cnlimiter committed Dec 3, 2023
1 parent 64f4ee8 commit 6a6f337
Show file tree
Hide file tree
Showing 37 changed files with 706 additions and 715 deletions.
2 changes: 1 addition & 1 deletion forge/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
forge "net.minecraftforge:forge:${project.minecraft_version}-${project.forge_version}"

implementation forgeRuntimeLibrary(include("cn.evole.bot:OneBot-Client:${project.onebot_client_version}"))
implementation forgeRuntimeLibrary(include("cn.evole.config:AtomConfig-Yaml:${project.atomconfig_version}"))
implementation forgeRuntimeLibrary(include("cn.evole.config:AtomConfig-Toml:${project.toml_version}"))

compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor "org.projectlombok:lombok:1.18.24"
Expand Down
6 changes: 3 additions & 3 deletions forge/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
org.gradle.jvmargs=-Xmx8G

# Mod Properties
mod_version=2.1.7
mod_version=2.1.9
maven_group=cn.evole.mods
archives_base_name=McBot-forge
mod_id=mcbot
# Wrapper Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
onebot_client_version=0.3.6-light
onebot_client_version=0.3.7
jankson_version=1.2.1
atomconfig_version=0.2.0
toml_version=0.1.1
37 changes: 33 additions & 4 deletions forge/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cn.evole.mods.mcbot;

//#if MC >= 11700
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.onebot.sdk.util.BotUtils;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLModContainer;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.loading.FMLPaths;
import java.nio.file.Path;
//#if MC >= 11700
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//#else
Expand All @@ -27,9 +27,38 @@ public class Const {
//$$ public static final Logger LOGGER = LogManager.getLogger("McBot");
//#endif
public static boolean isShutdown = false;
public static Path configDir = FMLPaths.CONFIGDIR.get();

public static boolean isLoad(String modId){
return ModList.get().isLoaded(modId);
}
public static void sendGroupMsg(String message){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
groupMsg(id, message);
}
}

public static void groupMsg(long id, String message){
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
IMcBot.bot.sendGroupMsg(id, message, false);
}
else {
IMcBot.bot.sendGroupMsg(id, BotUtils.rawToJson(message), false);
}
}

public static void sendGuildMsg(String message){
for (String id : ModConfig.INSTANCE.getCommon().getChannelIdList()){
guildMsg(ModConfig.INSTANCE.getCommon().getGuildId(), id, message);
}
}

public static void guildMsg(String guildId, String channelId, String message){
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
IMcBot.bot.sendGuildMsg(guildId, channelId, message);
}
else {
IMcBot.bot.sendGuildMsg(guildId, channelId, BotUtils.rawToJson(message));
}
}
}
37 changes: 11 additions & 26 deletions forge/src/main/java/cn/evole/mods/mcbot/IMcBot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.evole.mods.mcbot;

import cn.evole.config.yaml.serialization.ConfigurationSerialization;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.init.event.IBotEvent;
import cn.evole.mods.mcbot.init.event.IChatEvent;
Expand All @@ -24,39 +23,27 @@
public class IMcBot {

public static MinecraftServer SERVER = null;

public static CustomCmdHandler CMD ;
public static Path CONFIG_FOLDER;

public static Path CONFIG_FILE;

public static LinkedBlockingQueue<String> blockingQueue;
public static ConnectFactory service;
public static EventBus bus;
public static Bot bot;
public static Thread app;

public static ModConfig config;


public MinecraftServer getServer() {
return SERVER;
}

public IMcBot(Path configDir) {
init(configDir);
}
public void init(Path config_dir) {
ConfigurationSerialization.registerClass(ModConfig.Status.class);//注册序列对象
ConfigurationSerialization.registerClass(ModConfig.Bot.class);//注册序列对象
ConfigurationSerialization.registerClass(ModConfig.Cmd.class);//注册序列对象
ConfigurationSerialization.registerClass(ModConfig.Common.class);//注册序列对象

CONFIG_FOLDER = config_dir.resolve("mcbot");
public void init(Path config_dir) {
CONFIG_FOLDER = Const.configDir.resolve("mcbot");
FileUtils.checkFolder(CONFIG_FOLDER);
config = new ModConfig(CONFIG_FOLDER.toFile());
config.load();

CMD = new CustomCmdHandler(CONFIG_FOLDER);
CONFIG_FILE = CONFIG_FOLDER.resolve("config.toml");
Runtime.getRuntime().addShutdownHook(new Thread(IMcBot::killOutThreads));
}

Expand All @@ -66,10 +53,10 @@ public void onServerStarting(MinecraftServer server) {

public void onServerStarted(MinecraftServer server) {
blockingQueue = new LinkedBlockingQueue<>();//使用队列传输数据
if (config.getCommon().isAutoOpen()) {
if (ModConfig.INSTANCE.getCommon().isAutoOpen()) {
try {
app = new Thread(() -> {
service = new ConnectFactory(IMcBot.config.getBotConfig(), blockingQueue);//创建websocket连接
service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), blockingQueue);//创建websocket连接
bot = service.ws.createBot();//创建机器人实例
}, "BotServer");
app.start();
Expand All @@ -78,7 +65,7 @@ public void onServerStarted(MinecraftServer server) {
}
}
bus = new EventBus(blockingQueue);//创建事件分发器
CMD.load();//自定义命令加载
CustomCmdHandler.INSTANCE.load();//自定义命令加载
IBotEvent.init(bus);//事件监听s
}

Expand All @@ -96,8 +83,7 @@ public void onServerStopped(MinecraftServer server) {

private static void killOutThreads() {
try {
config.save();//保存配置
CMD.clear();//自定义命令持久层清空
CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -107,19 +93,18 @@ public void onServerTick(MinecraftServer server) {
ITickEvent.register(server);
}
public void onServerChat(Level level, Player player, String msg) {
IChatEvent.register(level, player, msg);
IChatEvent.register(player, msg);
}
public void onPlayerLogIn(Level level, Player player) {
IPlayerEvent.loggedIn(level, player);
}

public void onPlayerLogOut(Level level, Player player) {
IPlayerEvent.loggedOut(level, player);
}
public void onPlayerDeath(Level level, DamageSource source, ServerPlayer player) {
IPlayerEvent.death(level, source, player);
IPlayerEvent.death(source, player);
}
public void onPlayerAdvancement(Level level, Player player, Advancement advancement) {
IPlayerEvent.advancement(level, player, advancement);
IPlayerEvent.advancement(player, advancement);
}
}
3 changes: 2 additions & 1 deletion forge/src/main/java/cn/evole/mods/mcbot/cmds/BotCmdRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void sendSuccess(Supplier<Component> supplier, boolean bl) {
}
//#else
//$$ @Override
//$$ public void sendSuccess(Component component, boolean p_81356_) {
//$$ public void sendSuccess(@NotNull Component component, boolean p_81356_) {
//$$ super.sendSuccess(component, p_81356_);
//$$ this.outPut.add(component.getString());
//$$ }
Expand Down
26 changes: 13 additions & 13 deletions forge/src/main/java/cn/evole/mods/mcbot/cmds/CmdApi.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.evole.mods.mcbot.cmds;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.IMcBot;
import cn.evole.mods.mcbot.init.handler.CustomCmdHandler;
import cn.evole.mods.mcbot.util.onebot.BotUtils;
import cn.evole.onebot.client.core.Bot;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.GuildMessageEvent;

Expand All @@ -29,37 +29,37 @@ private static StringBuilder CmdMain(String cmd, boolean isOp) {
return result;
}

private static void GroupCmd(Bot bot, long groupId, String cmd, boolean isOp) {
bot.sendGroupMsg(groupId, CmdMain(cmd, isOp).toString(), true);
private static void GroupCmd(long groupId, String cmd, boolean isOp) {
Const.groupMsg(groupId, CmdMain(cmd, isOp).toString());
}

private static void GuildCmd(Bot bot, String guildId, String channelId, String cmd, boolean isOp) {
bot.sendGuildMsg(guildId, channelId, CmdMain(cmd, isOp).toString());
private static void GuildCmd(String guildId, String channelId, String cmd, boolean isOp) {
Const.guildMsg(guildId, channelId, CmdMain(cmd, isOp).toString());
}

public static void invokeCommandGroup(GroupMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀

if (BotUtils.groupAdminParse(event)) {
IMcBot.CMD.getCustomCmds().stream()
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(IMcBot.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), true));//admin
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, command), true));//admin
} else
IMcBot.CMD.getCustomCmds().stream()
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(IMcBot.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), false));
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, command), false));

}

public static void invokeCommandGuild(GuildMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀

if (BotUtils.guildAdminParse(event)) {
IMcBot.CMD.getCustomCmds().stream()
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(IMcBot.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true));//admin
} else IMcBot.CMD.getCustomCmds().stream()
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true));//admin
} else CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(IMcBot.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.IMcBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand All @@ -16,8 +16,8 @@ public class AddChannelIDCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val id = context.getArgument("ChannelID", String.class);
IMcBot.config.getCommon().setGuildOn(true);
if (IMcBot.config.getCommon().getChannelIdList().contains(id)) {
ModConfig.INSTANCE.getCommon().setGuildOn(true);
if (ModConfig.INSTANCE.getCommon().getChannelIdList().contains(id)) {

//#if MC >= 12000
context.getSource().sendSuccess(()->Component.literal("子频道号:" + id + "已经出现了!"), true);
Expand All @@ -28,9 +28,9 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
//#endif

} else {
IMcBot.config.getCommon().addChannelId(id);
ModConfig.INSTANCE.getCommon().addChannelId(id);
}
IMcBot.config.save();
ModConfig.INSTANCE.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.IMcBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand All @@ -16,7 +16,7 @@ public class AddGroupIDCommand {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.IMcBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand All @@ -15,15 +15,15 @@ public class AuthKeyCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val id = context.getArgument("AuthKey", String.class);
IMcBot.config.getBotConfig().setToken(id);
ModConfig.INSTANCE.getBotConfig().setToken(id);
//#if MC >= 12000
context.getSource().sendSuccess(()->Component.literal("已设置Mirai框架的AuthKey为:" + id), true);
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
IMcBot.config.save();
ModConfig.INSTANCE.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.IMcBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -15,15 +15,15 @@ public class BotIDCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
long id = context.getArgument("BotId", Long.class);
IMcBot.config.getCommon().setBotId(id);
ModConfig.INSTANCE.getCommon().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
IMcBot.config.save();
ModConfig.INSTANCE.save();
return 1;
}

Expand Down
Loading

0 comments on commit 6a6f337

Please sign in to comment.