From c057c6965314e7c473c98f95fd7baa6e475d87e0 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 7 Apr 2024 18:11:19 +0200 Subject: [PATCH] Enable optional logging in ChatLogHandler Modified the ChatLogHandler to support optional logging by introducing a new argument in the getInstance() method. This argument sets the logging state during instantiation. This facilitates better flexibility when using the ChatLogHandler across different sections of the code base as logging requirements may differ. --- .../chat/objects/chat_log/ChatLogHandler.java | 14 ++++++++++---- .../src/main/java/com/alttd/chat/ChatPlugin.java | 2 +- .../java/com/alttd/velocitychat/VelocityChat.java | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/com/alttd/chat/objects/chat_log/ChatLogHandler.java b/api/src/main/java/com/alttd/chat/objects/chat_log/ChatLogHandler.java index 93fd6f5..9eafa84 100644 --- a/api/src/main/java/com/alttd/chat/objects/chat_log/ChatLogHandler.java +++ b/api/src/main/java/com/alttd/chat/objects/chat_log/ChatLogHandler.java @@ -15,11 +15,11 @@ import java.util.concurrent.*; public class ChatLogHandler { private static ChatLogHandler instance = null; - private final ScheduledExecutorService executorService; + private ScheduledExecutorService executorService = null; - public static ChatLogHandler getInstance() { + public static ChatLogHandler getInstance(boolean enableLogging) { if (instance == null) - instance = new ChatLogHandler(); + instance = new ChatLogHandler(enableLogging); return instance; } @@ -27,7 +27,9 @@ public class ChatLogHandler { private final Queue chatLogQueue = new ConcurrentLinkedQueue<>(); private final HashMap> chatLogs = new HashMap<>(); - public ChatLogHandler() { + public ChatLogHandler(boolean enableLogging) { + if (!enableLogging) + return; Duration deleteThreshold = Duration.ofDays(Config.CHAT_LOG_DELETE_OLDER_THAN_DAYS); ChatLogQueries.deleteOldMessages(deleteThreshold).thenAccept(success -> { if (success) { @@ -41,6 +43,10 @@ public class ChatLogHandler { Config.CHAT_LOG_SAVE_DELAY_MINUTES, Config.CHAT_LOG_SAVE_DELAY_MINUTES, TimeUnit.MINUTES); } + /** + * Shuts down the executor service and saves the chat logs to the database. + * Will throw an error if called on a ChatLogHandler that was started without logging + */ public void shutDown() { executorService.shutdown(); saveToDatabase(true); diff --git a/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java b/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java index 6056d46..d56d317 100755 --- a/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java +++ b/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java @@ -39,7 +39,7 @@ public class ChatPlugin extends JavaPlugin { chatHandler = new ChatHandler(); DatabaseConnection.initialize(); serverConfig = new ServerConfig(Bukkit.getServerName()); - ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(); + ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(true); registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler)); if(serverConfig.GLOBALCHAT) { registerCommand("globalchat", new GlobalChat()); diff --git a/velocity/src/main/java/com/alttd/velocitychat/VelocityChat.java b/velocity/src/main/java/com/alttd/velocitychat/VelocityChat.java index a53c34e..d7cb3a9 100755 --- a/velocity/src/main/java/com/alttd/velocitychat/VelocityChat.java +++ b/velocity/src/main/java/com/alttd/velocitychat/VelocityChat.java @@ -110,7 +110,7 @@ public class VelocityChat { } public void loadCommands() { - ChatLogHandler instance = ChatLogHandler.getInstance(); //TODO disable logging part + ChatLogHandler instance = ChatLogHandler.getInstance(false); new SilentJoinCommand(server); new GlobalAdminChat(server); new Reload(server);