From 265ba8895a809c80a9f3a22eacc0426e55a35bee Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Sat, 25 Nov 2023 17:41:40 -0800 Subject: [PATCH] Minor refactoring of DatabaseManager --- src/main/java/emu/lunarcore/LunarCore.java | 16 +++++- .../lunarcore/database/DatabaseManager.java | 55 +++++++++---------- .../emu/lunarcore/server/game/GameServer.java | 5 +- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/main/java/emu/lunarcore/LunarCore.java b/src/main/java/emu/lunarcore/LunarCore.java index 9e3ad1f..feddbdd 100644 --- a/src/main/java/emu/lunarcore/LunarCore.java +++ b/src/main/java/emu/lunarcore/LunarCore.java @@ -81,9 +81,8 @@ public class LunarCore { break; case "-database": // Database only - DatabaseManager databaseManager = new DatabaseManager(); - databaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer()); - LunarCore.getLogger().info("Running local mongo server at " + databaseManager.getServer().getConnectionString()); + DatabaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer()); + LunarCore.getLogger().info("Running local mongo server at " + DatabaseManager.getServer().getConnectionString()); // Console LunarCore.startConsole(); return; @@ -113,6 +112,9 @@ public class LunarCore { gameServer = new GameServer(getConfig().getGameServer()); gameServer.start(); } + + // Hook into shutdown event + Runtime.getRuntime().addShutdownHook(new Thread(LunarCore::onShutdown)); // Start console LunarCore.startConsole(); @@ -202,6 +204,14 @@ public class LunarCore { LunarCore.getLogger().error("Terminal error: ", e); } } + + // Shutdown event + + private static void onShutdown() { + if (gameServer != null) { + gameServer.onShutdown(); + } + } // Server enums diff --git a/src/main/java/emu/lunarcore/database/DatabaseManager.java b/src/main/java/emu/lunarcore/database/DatabaseManager.java index f381a1c..caab010 100644 --- a/src/main/java/emu/lunarcore/database/DatabaseManager.java +++ b/src/main/java/emu/lunarcore/database/DatabaseManager.java @@ -34,14 +34,11 @@ import lombok.Getter; @Getter public final class DatabaseManager { - private MongoServer server; + @Getter private static MongoServer server; private Datastore datastore; private final DeleteOptions DELETE_MANY = new DeleteOptions().multi(true); - // Empty constructor for when we want to start an internal server - public DatabaseManager() {} - public DatabaseManager(DatabaseInfo info, ServerType type) { // Variables String connectionString = info.getUri(); @@ -123,31 +120,6 @@ public final class DatabaseManager { } } - // Internal mongo server - - public String startInternalMongoServer(InternalMongoInfo internalMongo) { - // Get backend - MongoBackend backend = null; - - if (internalMongo.filePath != null && internalMongo.filePath.length() > 0) { - backend = new H2Backend(internalMongo.filePath); - } else { - backend = new MemoryBackend(); - } - - // Create the local mongo server and replace the connection string - server = new MongoServer(backend); - - // Bind to address of it exists - if (internalMongo.getAddress() != null && internalMongo.getPort() != 0) { - server.bind(internalMongo.getAddress(), internalMongo.getPort()); - } else { - server.bind(); // Binds to random port - } - - return server.getConnectionString(); - } - // Database Functions public boolean checkIfObjectExists(Class cls, long uid) { @@ -199,4 +171,29 @@ public final class DatabaseManager { getDatastore().save(counter); } } + + // Internal MongoDB server + + public static String startInternalMongoServer(InternalMongoInfo internalMongo) { + // Get backend + MongoBackend backend = null; + + if (internalMongo.filePath != null && internalMongo.filePath.length() > 0) { + backend = new H2Backend(internalMongo.filePath); + } else { + backend = new MemoryBackend(); + } + + // Create the local mongo server and replace the connection string + server = new MongoServer(backend); + + // Bind to address of it exists + if (internalMongo.getAddress() != null && internalMongo.getPort() != 0) { + server.bind(internalMongo.getAddress(), internalMongo.getPort()); + } else { + server.bind(); // Binds to random port + } + + return server.getConnectionString(); + } } diff --git a/src/main/java/emu/lunarcore/server/game/GameServer.java b/src/main/java/emu/lunarcore/server/game/GameServer.java index 099e483..8b17715 100644 --- a/src/main/java/emu/lunarcore/server/game/GameServer.java +++ b/src/main/java/emu/lunarcore/server/game/GameServer.java @@ -63,9 +63,6 @@ public class GameServer extends KcpServer { onTick(); } }, 0, 1000); - - // Hook into shutdown event. - Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown)); } public GameServerConfig getServerConfig() { @@ -166,7 +163,7 @@ public class GameServer extends KcpServer { } } - private void onShutdown() { + public void onShutdown() { // Close server socket this.stop();