mirror of
https://github.com/mingjun97/gc-mojoconsole-plus.git
synced 2024-11-24 03:12:52 +00:00
Optimize code
This commit is contained in:
parent
59f45610aa
commit
f535372ae7
@ -1,54 +1,42 @@
|
||||
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 com.mojo.consoleplus.config.MojoConfig;
|
||||
import org.slf4j.Logger;
|
||||
import static emu.grasscutter.config.Configuration.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))) {
|
||||
Gson gson = new Gson();
|
||||
PluginConfig pluginConfig = gson.fromJson(reader, PluginConfig.class);
|
||||
this.getLogger().info("[MojoConsole] loaded!");
|
||||
versionTag = pluginConfig.version;
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 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)
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -24,10 +24,10 @@ public class SocketServer {
|
||||
private static final HashMap<String, Integer> 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user