Skip to content

Commit

Permalink
perf(fabric): 部分优化
Browse files Browse the repository at this point in the history
* 绑定消息冗余优化
* 连接机制优化
  • Loading branch information
cnlimiter committed Mar 20, 2024
1 parent 62729a4 commit b2349b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
16 changes: 16 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.config.ModConfig;
import cn.evole.mods.mcbot.core.event.IBotEvent;
import cn.evole.mods.mcbot.core.event.ITickEvent;
import cn.evole.mods.mcbot.util.onebot.MessageThread;
import cn.evole.onebot.client.OneBotClient;
import cn.evole.onebot.sdk.action.ActionPath;
import com.google.gson.JsonObject;
import lombok.val;
Expand Down Expand Up @@ -99,6 +101,20 @@ public static void sendAllPlayerMsg(String message){
ITickEvent.getSendQueue().add(toSend);
}

/**
* WS连接
*/
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.connected = true;
}


public static void shutdown() {
messageThread.stop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.evole.mods.mcbot.command;


import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.config.ModConfig;
import cn.evole.mods.mcbot.core.event.IBotEvent;
Expand All @@ -23,21 +24,13 @@ public class ConnectCommand {
public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
val parameter = context.getArgument("parameter", String.class);


val pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)");
val matcher = pattern.matcher(parameter);
if (matcher.find()) {
ModConfig.INSTANCE.getBotConfig().setUrl(parameter);
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("▌ " + ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#endif
doConnect();
ModConfig.INSTANCE.save();
doConnect(context);
return 1;

} else {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("▌ " +ChatFormatting.RED + "参数错误❌"), true);
Expand All @@ -51,25 +44,29 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
}



public static int commonExecute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#endif
doConnect();
doConnect(context);
return 1;

}

public static void doConnect() {
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.connected = true;
public static void doConnect(CommandContext<CommandSourceStack> context) {
if (!McBot.onebot.getWs().isOpen()){
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
//#endif
Const.wsConnect();
} else {
//#if MC >= 12000
//$$ context.getSource().sendSuccess(()->Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "已存在WS连接"), true);
//#elseif MC < 11900
context.getSource().sendSuccess(new TextComponent("▌ " + ChatFormatting.LIGHT_PURPLE + "已存在WS连接"), true);
//#else
//$$ context.getSource().sendSuccess(Component.literal("▌ " +ChatFormatting.LIGHT_PURPLE + "已存在WS连接"), true);
//#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void loggedIn(Level world, ServerPlayer player) {
val toSend = new TextComponent("请先完成绑定(爱来自群服互联~)");
//#endif
player.connection.disconnect(toSend);
return;//防止冗余消息
}

if (ModConfig.INSTANCE.getStatus().isSJoinEnable() && ModConfig.INSTANCE.getStatus().isSEnable()) {
Expand All @@ -43,6 +44,9 @@ public static void loggedIn(Level world, ServerPlayer player) {
}
}
public static void loggedOut(Level world, ServerPlayer player) {
if (ModConfig.INSTANCE.getCommon().isBindOn() && !UserBindApi.isIn(player.getGameProfile().getName())){
return;//防止冗余消息
}
if (ModConfig.INSTANCE.getStatus().isSLeaveEnable() && ModConfig.INSTANCE.getStatus().isSEnable()) {
val msg = player.getDisplayName().getString() + " 离开了服务器";
send(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@Getter
public class KeepAlive {
private final Queue<HeartbeatMetaEvent> heartBeatQueue = new LinkedBlockingQueue<>(5);
// private long timeoutMillis = 100000;

public KeepAlive() {
}
Expand All @@ -36,11 +35,6 @@ public void register() {
while (true) {
val limit = ModConfig.INSTANCE.getBotConfig().getMaxReconnectAttempts();
if (McBot.connected && ModConfig.INSTANCE.getBotConfig().isReconnect() && limit >= 1) {
// try {
// timeoutMillis = getHeartbeat(timeoutMillis + ModConfig.INSTANCE.getBotConfig().getTimeoutCompensation()).getInterval();
// } catch (TimeoutException e) {
// reconnect(limit);
// }
if (McBot.onebot.getWs().isClosed()) { // 当你写完复杂的机制后突然发现有现成的api时 be like
reconnect(limit);
}
Expand All @@ -57,12 +51,7 @@ private void reconnect(final int limit) {
int hasReconnect = 0;
while (hasReconnect <= limit) {
Const.LOGGER.info("正在尝试重连...第{}次", hasReconnect + 1);
ConnectCommand.doConnect();

// try {
// getHeartbeat(100000);
// return;
// } catch (TimeoutException ignored) {}
Const.wsConnect();
try {
Thread.sleep(ModConfig.INSTANCE.getBotConfig().getTimeoutCompensation());
} catch (InterruptedException e) {
Expand Down

0 comments on commit b2349b3

Please sign in to comment.