Optimize code

This commit is contained in:
方块君 2022-07-25 08:06:39 +08:00
parent 59f45610aa
commit f535372ae7
3 changed files with 35 additions and 43 deletions

View File

@ -1,54 +1,42 @@
package com.mojo.consoleplus; 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.SocketClient;
import com.mojo.consoleplus.socket.SocketServer; import com.mojo.consoleplus.socket.SocketServer;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
import emu.grasscutter.plugin.Plugin; 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.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; 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.HTTP_POLICIES;
import static emu.grasscutter.config.Configuration.PLUGIN;
import com.mojo.consoleplus.config.MojoConfig; public class ConsolePlus extends Plugin {
import org.slf4j.Logger;
public class ConsolePlus extends Plugin{
public static MojoConfig config = MojoConfig.loadConfig(); public static MojoConfig config = MojoConfig.loadConfig();
public static String versionTag; public static String versionTag;
public static AuthHandler authHandler; public static AuthHandler authHandler;
public static Logger logger;
public static ConsolePlus instance;
public static ConsolePlus getInstance() {
return instance;
}
@Override @Override
public void onLoad() { public void onLoad() {
instance = this;
logger = getLogger();
try (InputStream in = getClass().getResourceAsStream("/plugin.json"); 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(); Gson gson = new Gson();
PluginConfig pluginConfig = gson.fromJson(reader, PluginConfig.class); PluginConfig pluginConfig = gson.fromJson(reader, PluginConfig.class);
this.getLogger().info("[MojoConsole] loaded!"); this.getLogger().info("[MojoConsole] loaded!");
versionTag = pluginConfig.version; versionTag = pluginConfig.version;
} } catch (Exception e) {
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// Use resource // Use resource
@ -75,10 +63,10 @@ public class ConsolePlus extends Plugin{
authHandler = new AuthHandler(); authHandler = new AuthHandler();
if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) { if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) {
SocketServer.startServer(); SocketServer.startServer(getLogger());
Grasscutter.getHttpServer().addRouter(RequestOnlyHttpHandler.class); Grasscutter.getHttpServer().addRouter(RequestOnlyHttpHandler.class);
} else if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) { } else if (Grasscutter.config.server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
SocketClient.connectServer(); SocketClient.connectServer(getLogger());
new EventHandler<>(PlayerJoinEvent.class) new EventHandler<>(PlayerJoinEvent.class)
.priority(HandlerPriority.HIGH) .priority(HandlerPriority.HIGH)
.listener(EventListeners::onPlayerJoin) .listener(EventListeners::onPlayerJoin)

View File

@ -31,11 +31,23 @@ public class SocketClient {
public static ReceiveThread receiveThread; public static ReceiveThread receiveThread;
public static void connectServer(Logger logger) {
mLogger = logger;
connectServer();
}
// 连接服务器 // 连接服务器
public static void connectServer() { public static void connectServer() {
if (connect) return; 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; MojoConfig config = ConsolePlus.config;
mLogger = ConsolePlus.logger;
clientThread = new ClientThread(config.socketHost, config.socketPort); clientThread = new ClientThread(config.socketHost, config.socketPort);
if (timer != null) { if (timer != null) {
@ -51,7 +63,6 @@ public class SocketClient {
var p = SocketUtils.getPacket(packet); var p = SocketUtils.getPacket(packet);
if (!clientThread.sendPacket(p)) { if (!clientThread.sendPacket(p)) {
mLogger.warn("[Mojo Console] Send packet to server failed"); mLogger.warn("[Mojo Console] Send packet to server failed");
mLogger.info("[Mojo Console] Reconnect to server");
connect = false; connect = false;
connectServer(); connectServer();
return false; return false;
@ -63,7 +74,6 @@ public class SocketClient {
public static boolean sendPacket(BasePacket packet, String packetID) { public static boolean sendPacket(BasePacket packet, String packetID) {
if (!clientThread.sendPacket(SocketUtils.getPacketAndPackID(packet, packetID))) { if (!clientThread.sendPacket(SocketUtils.getPacketAndPackID(packet, packetID))) {
mLogger.warn("[Mojo Console] Send packet to server failed"); mLogger.warn("[Mojo Console] Send packet to server failed");
mLogger.info("[Mojo Console] Reconnect to server");
connect = false; connect = false;
connectServer(); connectServer();
return false; return false;
@ -128,7 +138,7 @@ public class SocketClient {
// 运行命令 // 运行命令
case RunCommand -> { case RunCommand -> {
var command = player.data; var command = player.data;
var playerData = ConsolePlus.getInstance().getServer().getPlayerByUid(player.uid); var playerData = Grasscutter.getGameServer().getPlayerByUid(player.uid);
if (playerData == null) { if (playerData == null) {
sendPacket(new HttpPacket(404, "[Mojo Console] Player not found."), packet.packetID); sendPacket(new HttpPacket(404, "[Mojo Console] Player not found."), packet.packetID);
return; return;
@ -151,7 +161,7 @@ public class SocketClient {
} }
// 发送信息 // 发送信息
case DropMessage -> { case DropMessage -> {
var playerData = ConsolePlus.getInstance().getServer().getPlayerByUid(player.uid); var playerData = Grasscutter.getGameServer().getPlayerByUid(player.uid);
if (playerData == null) { if (playerData == null) {
return; return;
} }
@ -218,12 +228,6 @@ public class SocketClient {
} catch (IOException e) { } catch (IOException e) {
connect = false; connect = false;
mLogger.warn("[Mojo Console] Connect to server failed: " + ip + ":" + port); 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(); connectServer();
} }
} }

View File

@ -24,10 +24,10 @@ public class SocketServer {
private static final HashMap<String, Integer> clientTimeout = new HashMap<>(); private static final HashMap<String, Integer> clientTimeout = new HashMap<>();
private static Logger mLogger; private static Logger mLogger;
public static void startServer() { public static void startServer(Logger logger) {
mLogger = logger;
try { try {
int port = ConsolePlus.config.socketPort; int port = ConsolePlus.config.socketPort;
mLogger = ConsolePlus.logger;
new Timer().schedule(new SocketClientCheck(), 500); new Timer().schedule(new SocketClientCheck(), 500);
new WaitClientConnect(port); new WaitClientConnect(port);
} catch (Throwable e) { } catch (Throwable e) {