Skip to content

Commit

Permalink
修复同步消息到QQ时可能的卡顿 [Checked] (#144)
Browse files Browse the repository at this point in the history
* 临时的崩溃修复

* 问题修复和代码格式化

现在发送消息不会导致服务器暂停
调整一些对象的作用域
  • Loading branch information
xia-mc authored Mar 15, 2024
1 parent 6c9b941 commit 6f8bfa8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
11 changes: 6 additions & 5 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.config.ModConfig;
import cn.evole.mods.mcbot.util.onebot.MessageThread;
import net.fabricmc.loader.api.FabricLoader;
import java.nio.file.Path;
//#if MC >= 11700
Expand All @@ -27,6 +28,7 @@ public class Const {
public static boolean isShutdown = false;
public static Path configDir = FabricLoader.getInstance().getConfigDir();
public static Path gameDir = FabricLoader.getInstance().getGameDir();
private static final MessageThread messageThread = new MessageThread();

public static boolean isLoad(String modId){
return FabricLoader.getInstance().isModLoaded(modId);
Expand All @@ -39,11 +41,10 @@ public static void sendAllGroupMsg(String message){
}

public static void sendGroupMsg(long id, String message){
McBot.onebot.getBot().sendGroupMsg(id, message, false);
messageThread.submit(id, message, false);
}


public static void shutdown() {
messageThread.stop();
}
}



8 changes: 3 additions & 5 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import cn.evole.mods.mcbot.util.FileUtil;
import cn.evole.mods.mcbot.util.lib.LibUtils;
import cn.evole.mods.mcbot.util.locale.I18n;
import cn.evole.mods.mcbot.util.onebot.CQUtils;
import cn.evole.onebot.client.OneBotClient;
import lombok.Getter;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//#if MC >= 11900
//$$ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
//#else
Expand All @@ -40,7 +39,6 @@ public class McBot implements ModInitializer {
public static McBot INSTANCE = new McBot();

public static OneBotClient onebot;
public static ExecutorService CQUtilsExecutor;

@Override
public void onInitialize() {
Expand Down Expand Up @@ -89,7 +87,6 @@ public void onServerStarted(MinecraftServer server) {
onebot = OneBotClient.create(ModConfig.INSTANCE.getBotConfig().build()).open().registerEvents(new IBotEvent());
}
CustomCmdHandler.INSTANCE.load();//自定义命令加载
CQUtilsExecutor = Executors.newSingleThreadExecutor(); // 创建CQ码处理线程池
}

public void onServerStopping(MinecraftServer server) {
Expand All @@ -101,7 +98,8 @@ public void onServerStopping(MinecraftServer server) {
}

public void onServerStopped(MinecraftServer server) {
CQUtilsExecutor.shutdownNow();
Const.shutdown();
CQUtils.shutdown();
if (onebot != null) onebot.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import lombok.val;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -26,6 +23,7 @@ public class CQUtils {

private final static String CQ_CODE_REGEX = "\\[CQ:(.*?),(.*?)]";

private static final ExecutorService Executor = Executors.newSingleThreadExecutor(); // 创建CQ码处理线程池;

public static boolean hasImg(String msg) {
String regex = "\\[CQ:image,[(\\s\\S)]*]";
Expand Down Expand Up @@ -100,7 +98,7 @@ public static String replace(MessageEvent event) {
return message.toString();
});
try {
McBot.CQUtilsExecutor.execute(call);
Executor.execute(call);
back = call.get(1000 * 3, TimeUnit.MILLISECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException | IllegalStateException e) {
back = event.getRawMessage();
Expand All @@ -109,4 +107,8 @@ public static String replace(MessageEvent event) {
}
return back;
}

public static void shutdown() {
Executor.shutdownNow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cn.evole.mods.mcbot.util.onebot;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import com.google.gson.JsonArray;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @Project: McBot-fabric
* @Author: xia-mc
* @CreateTime: 2024/2/12 16:11
* @Description: 笑点解析:
*/
public class MessageThread {
private final ExecutorService executor;
public MessageThread() {
executor = Executors.newCachedThreadPool();
}

public void submit(long groupId, String msg, boolean autoEscape) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", msg));
executor.submit(() -> McBot.onebot.getBot().sendGroupMsg(groupId, msg, autoEscape));
}

public void submit(long groupId, JsonArray msg, boolean autoEscape) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", msg));
executor.submit(() -> McBot.onebot.getBot().sendGroupMsg(groupId, msg, autoEscape));
}

public void submit(String guildID, String channelID, String message) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", message));
executor.submit(() -> McBot.onebot.getBot().sendGuildMsg(guildID, channelID, message));
}

public void submit(String guildID, String channelID, JsonArray message) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", message));
executor.submit(() -> McBot.onebot.getBot().sendGuildMsg(guildID, channelID, message));
}

public void stop() {
executor.shutdownNow();
}
}

0 comments on commit 6f8bfa8

Please sign in to comment.