Skip to content

Commit

Permalink
合并拉取请求 #140
Browse files Browse the repository at this point in the history
性能优化和问题修复
  • Loading branch information
cnlimiter authored Feb 23, 2024
2 parents b41317a + 04b0e43 commit c74b802
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 78 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/fabric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ name: Fabric Action
on:
push:
branches: [ "fabric" ]
workflow_dispatch:


jobs:
Expand All @@ -25,7 +26,8 @@ jobs:
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'dragonwell'
distribution: 'zulu'
check-latest: true

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
15 changes: 5 additions & 10 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cn.evole.mods.mcbot.init.config.ModConfig;
import net.fabricmc.loader.api.FabricLoader;
import cn.evole.mods.mcbot.util.MessageThread;
import cn.evole.onebot.sdk.util.BotUtils;
import java.nio.file.Path;
//#if MC >= 11700
Expand Down Expand Up @@ -40,14 +39,13 @@ public static void sendGroupMsg(String message){
}

public static void groupMsg(long id, String message){
MessageThread thread;
// 发送消息时实际上所调用的函数。
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
thread = new MessageThread(id, message, false);
McBot.messageThread.submit(id, message, false);
}
else {
thread = new MessageThread(id, BotUtils.rawToJson(message), false);
McBot.messageThread.submit(id, BotUtils.rawToJson(message), false);
}
thread.start();
}

public static void sendGuildMsg(String message){
Expand All @@ -57,15 +55,12 @@ public static void sendGuildMsg(String message){
}

public static void guildMsg(String guildId, String channelId, String message){
// 发送消息时实际上所调用的函数。
MessageThread thread;
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
thread = new MessageThread(guildId, channelId, message);
McBot.messageThread.submit(guildId, channelId, message);
}
else {
thread = new MessageThread(guildId, channelId, BotUtils.rawToJson(message));
McBot.messageThread.submit(guildId, channelId, BotUtils.rawToJson(message));
}
thread.start();
}
}

Expand Down
16 changes: 15 additions & 1 deletion fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.evole.mods.mcbot.init.event.*;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.init.handler.CustomCmdHandler;
import cn.evole.mods.mcbot.util.MessageThread;
import cn.evole.mods.mcbot.util.locale.I18n;
import cn.evole.onebot.client.connection.ConnectFactory;
import cn.evole.onebot.client.core.Bot;
Expand All @@ -16,6 +17,8 @@
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;
import java.util.concurrent.LinkedBlockingQueue;
//#if MC >= 11900
//$$ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
Expand All @@ -26,6 +29,7 @@
//兼容1.20.1版本vanish
//#if MC == 12001
//$$ import cn.evole.mods.mcbot.init.compat.VanishCompat;
//$$ import cn.evole.mods.mcbot.init.compat.VanishLoader;
//#endif


Expand All @@ -44,6 +48,9 @@ public class McBot implements ModInitializer {

public static McBot INSTANCE = new McBot();

public static MessageThread messageThread;
public static ExecutorService CQUtilsExecutor;


public MinecraftServer getServer() {
return SERVER;
Expand Down Expand Up @@ -72,8 +79,11 @@ public void onInitialize() {

IEvents.SERVER_CHAT.register(IChatEvent::register);


//#if MC == 12001
//$$ VanishCompat.init();
//$$ if (Const.isLoad("melius_vanish")){
//$$ VanishCompat.init();
//$$ }
//#endif

}
Expand Down Expand Up @@ -110,6 +120,8 @@ public void onServerStarted(MinecraftServer server) {
listenerFactory.start();
CustomCmdHandler.INSTANCE.load();//自定义命令加载
IBotEvent.init(listenerFactory);//事件监听
messageThread = new MessageThread(); // 创建消息处理线程池
CQUtilsExecutor = Executors.newSingleThreadExecutor(); // 创建CQ码处理线程池
}

public void onServerStopping(MinecraftServer server) {
Expand All @@ -121,6 +133,8 @@ public void onServerStopping(MinecraftServer server) {
listenerFactory.stop();//分发器关闭
service.stop();
app.interrupt();
messageThread.stop();
CQUtilsExecutor.shutdownNow();
}

public void onServerStopped(MinecraftServer server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CommonConfig extends AutoLoadTomlConfig {
@TableField(rightComment = "是否开启聊天栏图片功能")
private boolean imageOn = true;//是否开启聊天栏图片功能
@TableField(rightComment = "是否开启绑定校验")
private boolean bindOn = true;//是否开启绑定校验
private boolean bindOn = false;//是否开启绑定校验

public CommonConfig() {
super(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

//注意不能优化import
import java.lang.reflect.InvocationTargetException;
import net.minecraft.server.MinecraftServer;
import java.lang.reflect.Method;
import java.util.UUID;
import org.spongepowered.asm.mixin.Unique;
import com.mojang.authlib.GameProfile;

//#if MC >= 12002
//$$ import net.minecraft.server.network.CommonListenerCookie;
//#endif

//兼容1.20.1版本vanish
//#if MC == 12001
//$$ import cn.evole.mods.mcbot.init.compat.VanishCompat;
//$$ import me.drex.vanish.util.VanishManager;
//$$ import cn.evole.mods.mcbot.init.compat.VanishLoader;
//#endif

/**
Expand All @@ -46,31 +49,19 @@ public abstract class MixinPlayerList {
@Inject(method = "placeNewPlayer", at = @At(value = "TAIL"))
public void PlayerList_placeNewPlayer(Connection connection, ServerPlayer player, CallbackInfo ci) {
//#if MC == 12001
//$$ if (!isVanished(player)) IEvents.PLAYER_LOGGED_IN.invoker().onPlayerLoggedIn(player.getCommandSenderWorld(), player);
//$$ if (!VanishLoader.isVanished(player)) IEvents.PLAYER_LOGGED_IN.invoker().onPlayerLoggedIn(player.getCommandSenderWorld(), player);
//#else
IEvents.PLAYER_LOGGED_IN.invoker().onPlayerLoggedIn(player.getCommandSenderWorld(), player);
//#endif
}
@Inject(method = "remove", at = @At(value = "HEAD"))
public void PlayerList_remove(ServerPlayer player, CallbackInfo ci) {
//#if MC == 12001
//$$ if (!isVanished(player)) IEvents.PLAYER_LOGGED_OUT.invoker().onPlayerLoggedOut(player.getCommandSenderWorld(), player);
//$$ if (!VanishLoader.isVanished(player)) IEvents.PLAYER_LOGGED_OUT.invoker().onPlayerLoggedOut(player.getCommandSenderWorld(), player);
//#else
IEvents.PLAYER_LOGGED_OUT.invoker().onPlayerLoggedOut(player.getCommandSenderWorld(), player);
//#endif

}
//#endif

//#if MC == 12001
//$$ @Unique
//$$ private boolean isVanished(ServerPlayer player) {
//$$ // 获取玩家的 GameProfile
//$$ GameProfile gameProfile = player.getGameProfile();
//$$ // 从 GameProfile 中获取 UUID
//$$ UUID uuid = gameProfile.getId();
//$$ // 使用 VanishManager.isVanished 方法检查玩家是否处于隐身状态
//$$ return VanishCompat.VANISH && VanishManager.isVanished(player.getServer(), uuid) ;
//$$ }
//#endif
}
60 changes: 20 additions & 40 deletions fabric/src/main/java/cn/evole/mods/mcbot/util/MessageThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,42 @@
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 extends Thread {
private long groupIDInt;
private String guildIDString;
private String messageString;
private JsonArray messageArray;
private boolean autoEscape;
private String channelIDString;
private final short mode;

public MessageThread(long groupId, String msg, boolean autoEscape) {
this.mode = 0;
this.groupIDInt = groupId;
this.messageString = msg;
this.autoEscape = autoEscape;
public class MessageThread {
private final ExecutorService executor;
public MessageThread() {
executor = Executors.newCachedThreadPool();
}

public MessageThread(long groupId, JsonArray msg, boolean autoEscape) {
this.mode = 1;
this.groupIDInt = groupId;
this.messageArray = msg;
this.autoEscape = autoEscape;
public void submit(long groupId, String msg, boolean autoEscape) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", msg));
executor.submit(() -> McBot.bot.sendGroupMsg(groupId, msg, autoEscape));
}

public MessageThread(String guildID, String channelID, String message) {
this.mode = 2;
this.guildIDString = guildID;
this.channelIDString = channelID;
this.messageString = message;
public void submit(long groupId, JsonArray msg, boolean autoEscape) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", msg));
executor.submit(() -> McBot.bot.sendGroupMsg(groupId, msg, autoEscape));
}

public MessageThread(String guildID, String channelID, JsonArray message) {
this.mode = 3;
this.guildIDString = guildID;
this.channelIDString = channelID;
this.messageArray = message;
public void submit(String guildID, String channelID, String message) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", message));
executor.submit(() -> McBot.bot.sendGuildMsg(guildID, channelID, message));
}

public void run() {
switch (mode) {
case 0 :{McBot.bot.sendGroupMsg(groupIDInt, messageString, autoEscape); break;}
case 1 :{McBot.bot.sendGroupMsg(groupIDInt, messageArray, autoEscape); break;}
case 2 :{McBot.bot.sendGuildMsg(guildIDString, channelIDString, messageString); break;}
case 3 :{McBot.bot.sendGuildMsg(guildIDString, channelIDString, messageArray); break;}
}
public void submit(String guildID, String channelID, JsonArray message) {
Const.LOGGER.debug(String.format("转发游戏消息: %s", message));
executor.submit(() -> McBot.bot.sendGuildMsg(guildID, channelID, message));
}

public void start() {
Const.LOGGER.debug(String.format("转发游戏消息: %s", messageString != null ? messageString : messageArray));
Thread thread = new Thread(this, "MessageThread");
thread.start();
public void stop() {
executor.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.evole.mods.mcbot.util.onebot;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.onebot.sdk.util.BotUtils;
import lombok.val;
Expand All @@ -25,7 +26,7 @@ public class CQUtils {


public static boolean hasImg(String msg) {
String regex = "\\[CQ:image,[(\\s\\S)]*\\]";
String regex = "\\[CQ:image,[(\\s\\S)]*]";
val p = Pattern.compile(regex);
val m = p.matcher(msg);
return m.find();
Expand All @@ -36,8 +37,7 @@ public static String replace(String msg) {
return BotUtils.unescape(msg);


final ExecutorService exec = Executors.newSingleThreadExecutor();
String back = "";
String back;
StringBuffer message = new StringBuffer();
Pattern pattern = Pattern.compile(CQ_CODE_REGEX);
Matcher matcher = pattern.matcher(msg);
Expand All @@ -47,7 +47,7 @@ public static String replace(String msg) {
val type = matcher.group(1);
val data = matcher.group(2);
switch (type) {
case "image": {
case "image":
if (ModConfig.INSTANCE.getCommon().isImageOn() && Const.isLoad("chatimage")) {
val url = Arrays.stream(data.split(","))//具体数据分割
.filter(it -> it.startsWith("url"))//非空判断
Expand All @@ -62,7 +62,6 @@ public static String replace(String msg) {
matcher.appendReplacement(message, "[图片]");
}
break;
}
case "at":
val id = Arrays.stream(data.split(","))//具体数据分割
.filter(it -> it.startsWith("qq"))//非空判断
Expand Down Expand Up @@ -104,14 +103,13 @@ public static String replace(String msg) {
return message.toString();
});
try {
exec.execute(call);
McBot.CQUtilsExecutor.execute(call);
back = call.get(1000 * 3, TimeUnit.MILLISECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
back = msg;
call.cancel(true);
Const.LOGGER.error(e.getLocalizedMessage());
}
exec.shutdownNow();
return back;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//#if MC == 12001
package cn.evole.mods.mcbot.init.compat;

import me.drex.vanish.util.VanishManager;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.level.ServerPlayer;
import java.util.UUID;

public class VanishLoader {
public static boolean isVanished(ServerPlayer player) {
if(!VanishCompat.VANISH) return false;
// 获取玩家的 GameProfile
GameProfile gameProfile = player.getGameProfile();
// 从 GameProfile 中获取 UUID
UUID uuid = gameProfile.getId();
// 使用 VanishManager.isVanished 方法检查玩家是否处于隐身状态
return VanishManager.isVanished(player.getServer(), uuid);
}
}
//#endif
4 changes: 2 additions & 2 deletions fabric/wrapper/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply(plugin: "dev.architectury.loom")

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

archivesBaseName = project.archives_base_name
version = project.mod_version
Expand Down

0 comments on commit c74b802

Please sign in to comment.