diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java b/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java index 0879fc8..c986b62 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java @@ -1,57 +1,45 @@ package com.mojo.consoleplus; +import com.google.gson.Gson; +import com.mojo.consoleplus.command.PluginCommand; +import com.mojo.consoleplus.config.MojoConfig; import com.mojo.consoleplus.socket.SocketClient; import com.mojo.consoleplus.socket.SocketServer; import emu.grasscutter.Grasscutter; import emu.grasscutter.command.CommandMap; import emu.grasscutter.plugin.Plugin; +import emu.grasscutter.plugin.PluginConfig; +import emu.grasscutter.server.event.EventHandler; +import emu.grasscutter.server.event.HandlerPriority; +import emu.grasscutter.server.event.player.PlayerJoinEvent; +import emu.grasscutter.server.event.player.PlayerQuitEvent; +import io.javalin.http.staticfiles.Location; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; -import com.google.gson.Gson; -import com.mojo.consoleplus.command.PluginCommand; - -import emu.grasscutter.server.event.EventHandler; -import emu.grasscutter.server.event.HandlerPriority; -import emu.grasscutter.server.event.player.PlayerJoinEvent; -import emu.grasscutter.server.event.player.PlayerQuitEvent; -import io.javalin.http.staticfiles.Location; -import emu.grasscutter.plugin.PluginConfig; -import static emu.grasscutter.config.Configuration.PLUGIN; import static emu.grasscutter.config.Configuration.HTTP_POLICIES; +import static emu.grasscutter.config.Configuration.PLUGIN; -import com.mojo.consoleplus.config.MojoConfig; -import org.slf4j.Logger; - -public class ConsolePlus extends Plugin{ +public class ConsolePlus extends Plugin { public static MojoConfig config = MojoConfig.loadConfig(); public static String versionTag; public static AuthHandler authHandler; - public static Logger logger; - public static ConsolePlus instance; - - public static ConsolePlus getInstance() { - return instance; - } @Override public void onLoad() { - instance = this; - logger = getLogger(); try (InputStream in = getClass().getResourceAsStream("/plugin.json"); - BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { + BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { Gson gson = new Gson(); PluginConfig pluginConfig = gson.fromJson(reader, PluginConfig.class); this.getLogger().info("[MojoConsole] loaded!"); versionTag = pluginConfig.version; + } catch (Exception e) { + e.printStackTrace(); } - catch (Exception e) { - e.printStackTrace(); - } - // Use resource + // Use resource } @@ -75,10 +63,10 @@ public class ConsolePlus extends Plugin{ authHandler = new AuthHandler(); if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) { - SocketServer.startServer(); + SocketServer.startServer(getLogger()); Grasscutter.getHttpServer().addRouter(RequestOnlyHttpHandler.class); } else if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) { - SocketClient.connectServer(); + SocketClient.connectServer(getLogger()); new EventHandler<>(PlayerJoinEvent.class) .priority(HandlerPriority.HIGH) .listener(EventListeners::onPlayerJoin) diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketClient.java b/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketClient.java index e88fea9..3b13029 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketClient.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketClient.java @@ -31,11 +31,23 @@ public class SocketClient { public static ReceiveThread receiveThread; + public static void connectServer(Logger logger) { + mLogger = logger; + connectServer(); + } + // 连接服务器 public static void connectServer() { if (connect) return; + if (clientThread != null) { + mLogger.warn("[Mojo Console] Retry connecting to the server after 15 seconds"); + try { + Thread.sleep(15000); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + } MojoConfig config = ConsolePlus.config; - mLogger = ConsolePlus.logger; clientThread = new ClientThread(config.socketHost, config.socketPort); if (timer != null) { @@ -51,7 +63,6 @@ public class SocketClient { var p = SocketUtils.getPacket(packet); if (!clientThread.sendPacket(p)) { mLogger.warn("[Mojo Console] Send packet to server failed"); - mLogger.info("[Mojo Console] Reconnect to server"); connect = false; connectServer(); return false; @@ -63,7 +74,6 @@ public class SocketClient { public static boolean sendPacket(BasePacket packet, String packetID) { if (!clientThread.sendPacket(SocketUtils.getPacketAndPackID(packet, packetID))) { mLogger.warn("[Mojo Console] Send packet to server failed"); - mLogger.info("[Mojo Console] Reconnect to server"); connect = false; connectServer(); return false; @@ -128,7 +138,7 @@ public class SocketClient { // 运行命令 case RunCommand -> { var command = player.data; - var playerData = ConsolePlus.getInstance().getServer().getPlayerByUid(player.uid); + var playerData = Grasscutter.getGameServer().getPlayerByUid(player.uid); if (playerData == null) { sendPacket(new HttpPacket(404, "[Mojo Console] Player not found."), packet.packetID); return; @@ -151,7 +161,7 @@ public class SocketClient { } // 发送信息 case DropMessage -> { - var playerData = ConsolePlus.getInstance().getServer().getPlayerByUid(player.uid); + var playerData = Grasscutter.getGameServer().getPlayerByUid(player.uid); if (playerData == null) { return; } @@ -218,12 +228,6 @@ public class SocketClient { } catch (IOException e) { connect = false; mLogger.warn("[Mojo Console] Connect to server failed: " + ip + ":" + port); - mLogger.warn("[Mojo Console] Retry connecting to the server after 15 seconds"); - try { - Thread.sleep(15000); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } connectServer(); } } diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketServer.java b/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketServer.java index 4ae389a..26f43e4 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketServer.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/socket/SocketServer.java @@ -24,10 +24,10 @@ public class SocketServer { private static final HashMap clientTimeout = new HashMap<>(); private static Logger mLogger; - public static void startServer() { + public static void startServer(Logger logger) { + mLogger = logger; try { int port = ConsolePlus.config.socketPort; - mLogger = ConsolePlus.logger; new Timer().schedule(new SocketClientCheck(), 500); new WaitClientConnect(port); } catch (Throwable e) { @@ -38,7 +38,7 @@ public class SocketServer { // 向全部客户端发送数据 public static boolean sendAllPacket(BasePacket packet) { var p = SocketUtils.getPacket(packet); - HashMap old = (HashMap) clientList.clone(); + HashMap old = (HashMap) clientList.clone(); for (var client : old.entrySet()) { if (!client.getValue().sendPacket(p)) { mLogger.warn("[Mojo Console] Send packet to client {} failed", client.getKey());