Plugin enabling & disabling

This commit is contained in:
KingRainbow44
2022-04-23 01:17:35 -04:00
parent 3ba8c42b42
commit 7a3fbcdcf7
3 changed files with 69 additions and 44 deletions

View File

@@ -8,6 +8,7 @@ import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.plugin.PluginManager;
import emu.grasscutter.utils.Utils;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;
@@ -33,6 +34,7 @@ public final class Grasscutter {
public static RunMode MODE = RunMode.BOTH;
private static DispatchServer dispatchServer;
private static GameServer gameServer;
private static PluginManager pluginManager;
public static final Reflections reflector = new Reflections();
@@ -52,15 +54,11 @@ public final class Grasscutter {
for (String arg : args) {
switch (arg.toLowerCase()) {
case "-auth":
MODE = RunMode.AUTH;
break;
case "-game":
MODE = RunMode.GAME;
break;
case "-handbook":
Tools.createGmHandbook();
return;
case "-auth" -> MODE = RunMode.AUTH;
case "-game" -> MODE = RunMode.GAME;
case "-handbook" -> {
Tools.createGmHandbook(); return;
}
}
}
@@ -71,19 +69,21 @@ public final class Grasscutter {
ResourceLoader.loadAll();
// Database
DatabaseManager.initialize();
// Create server instances.
dispatchServer = new DispatchServer();
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
// Create plugin manager instance.
pluginManager = new PluginManager();
// Start servers.
if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
dispatchServer = new DispatchServer();
dispatchServer.start();
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
gameServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
dispatchServer = new DispatchServer();
} else if (getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
dispatchServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
} else if (getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
gameServer.start();
} else {
getLogger().error("Invalid server run mode. " + getConfig().RunMode);
@@ -91,12 +91,23 @@ public final class Grasscutter {
getLogger().error("Shutting down...");
System.exit(1);
}
// Enable all plugins.
pluginManager.enablePlugins();
// Open console.
startConsole();
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
}
/**
* Server shutdown event.
*/
private static void onShutdown() {
// Disable all plugins.
pluginManager.disablePlugins();
}
public static void loadConfig() {
try (FileReader file = new FileReader(configFile)) {
@@ -112,7 +123,7 @@ public final class Grasscutter {
try (FileWriter file = new FileWriter(configFile)) {
file.write(gson.toJson(config));
} catch (Exception e) {
Grasscutter.getLogger().error("Config save error");
Grasscutter.getLogger().error("Unable to save config file.");
}
}
@@ -122,13 +133,13 @@ public final class Grasscutter {
while ((input = br.readLine()) != null) {
try {
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
getLogger().error("Commands are not supported in dispatch only mode");
getLogger().error("Commands are not supported in dispatch only mode.");
return;
}
CommandMap.getInstance().invoke(null, input);
} catch (Exception e) {
Grasscutter.getLogger().error("Command error: ");
e.printStackTrace();
Grasscutter.getLogger().error("Command error:", e);
}
}
} catch (Exception e) {
@@ -161,4 +172,8 @@ public final class Grasscutter {
public static GameServer getGameServer() {
return gameServer;
}
public static PluginManager getPluginManager() {
return pluginManager;
}
}