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.
This commit is contained in:
Teriuihi 2024-04-07 18:11:19 +02:00
parent c25767e473
commit c057c69653
3 changed files with 12 additions and 6 deletions

View File

@ -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<ChatLog> chatLogQueue = new ConcurrentLinkedQueue<>();
private final HashMap<UUID, List<ChatLog>> 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);

View File

@ -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());

View File

@ -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);