From f3147b3256b888981fc5de1640a2888ef989fca8 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sat, 4 May 2024 23:41:27 +0200 Subject: [PATCH] Refine ChatPlugin and improve "vote to mute" logic Modified ChatPlugin to include "thisPlugin" within the ShutdownListener initialization. Additionally, adjusted the mute vote failure message color from green to red in ActiveVoteToMute file and added a condition to return false if there are no votes. Also, made improvements to the pagination logic in the VoteToMuteStarter file. Lastly, improved the chat logging mechanics in ChatLogHandler by adding and refining various log information for capturing action details. --- .../alttd/chat/objects/chat_log/ChatLogHandler.java | 13 +++++++++++-- galaxy/src/main/java/com/alttd/chat/ChatPlugin.java | 2 +- .../com/alttd/chat/listeners/ShutdownListener.java | 8 +++++++- .../commands/vote_to_mute/ActiveVoteToMute.java | 8 +++++--- .../commands/vote_to_mute/VoteToMuteStarter.java | 10 +++++----- 5 files changed, 29 insertions(+), 12 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 d226a36..a3fee53 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 @@ -41,9 +41,12 @@ public class ChatLogHandler { } }); executorService = Executors.newSingleThreadScheduledExecutor(); - executorService.scheduleAtFixedRate(() -> saveToDatabase(false), + executorService.scheduleAtFixedRate(() -> { + saveToDatabase(false); + ALogger.info(String.format("Running scheduler to save messages with a %d delay", Config.CHAT_LOG_SAVE_DELAY_MINUTES)); + }, Config.CHAT_LOG_SAVE_DELAY_MINUTES, Config.CHAT_LOG_SAVE_DELAY_MINUTES, TimeUnit.MINUTES); - ALogger.info("Logging started!"); + ALogger.info("Logging has started!"); } /** @@ -73,9 +76,11 @@ public class ChatLogHandler { private void saveToDatabase(boolean onMainThread) { savingToDatabase(true); + ALogger.info(String.format("Saving %d messages to database", chatLogs.size())); CompletableFuture booleanCompletableFuture = ChatLogQueries.storeMessages(chatLogs); if (onMainThread) { booleanCompletableFuture.join(); + ALogger.info("Finished saving messages on main thread"); return; } booleanCompletableFuture.whenComplete((result, throwable) -> { @@ -85,9 +90,13 @@ public class ChatLogHandler { ALogger.error("Failed to save chat messages."); } savingToDatabase(false); + if (!chatLogQueue.isEmpty()) { + ALogger.info("Adding back messages from queue to chatLogs map"); + } while (!chatLogQueue.isEmpty()) { addLog(chatLogQueue.remove()); } + ALogger.info("Finished saving messages"); }); } diff --git a/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java b/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java index d56d317..9721038 100755 --- a/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java +++ b/galaxy/src/main/java/com/alttd/chat/ChatPlugin.java @@ -40,7 +40,7 @@ public class ChatPlugin extends JavaPlugin { DatabaseConnection.initialize(); serverConfig = new ServerConfig(Bukkit.getServerName()); ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(true); - registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler)); + registerListener(new PlayerListener(serverConfig), new ChatListener(chatLogHandler), new BookListener(), new ShutdownListener(chatLogHandler, this)); if(serverConfig.GLOBALCHAT) { registerCommand("globalchat", new GlobalChat()); registerCommand("toggleglobalchat", new ToggleGlobalChat()); diff --git a/galaxy/src/main/java/com/alttd/chat/listeners/ShutdownListener.java b/galaxy/src/main/java/com/alttd/chat/listeners/ShutdownListener.java index 0c7ebd3..6b4dfc0 100644 --- a/galaxy/src/main/java/com/alttd/chat/listeners/ShutdownListener.java +++ b/galaxy/src/main/java/com/alttd/chat/listeners/ShutdownListener.java @@ -4,17 +4,23 @@ import com.alttd.chat.objects.chat_log.ChatLogHandler; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.plugin.Plugin; public class ShutdownListener implements Listener { private final ChatLogHandler chatLogHandler; + private final Plugin thisPlugin; - public ShutdownListener(ChatLogHandler chatLogHandler) { + public ShutdownListener(ChatLogHandler chatLogHandler, Plugin thisPlugin) { this.chatLogHandler = chatLogHandler; + this.thisPlugin = thisPlugin; } @EventHandler public void onShutdown(PluginDisableEvent event) { + if (!event.getPlugin().getName().equals(thisPlugin.getName())){ + return; + } chatLogHandler.shutDown(); } diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/ActiveVoteToMute.java b/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/ActiveVoteToMute.java index 570438b..cb427bc 100644 --- a/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/ActiveVoteToMute.java +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/ActiveVoteToMute.java @@ -17,7 +17,6 @@ import org.jetbrains.annotations.NotNull; import java.awt.*; import java.time.Duration; import java.util.*; -import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -97,7 +96,7 @@ public class ActiveVoteToMute { mutePlayer(); return; } - Component message = Utility.parseMiniMessage(" The vote to mute has failed, they will not be muted.", + Component message = Utility.parseMiniMessage(" The vote to mute has failed, they will not be muted.", Placeholder.component("prefix", prefix), Placeholder.parsed("player", votedPlayer.getUsername())); server.getPlayersConnected().stream() .filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute")) @@ -128,7 +127,10 @@ public class ActiveVoteToMute { public boolean votePassed() { double totalVotes = (votedFor.size() + votedAgainst.size()); - if (totalVotes / totalEligibleVoters < 0.6) { + if (totalVotes == 0) { + return false; + } + if (totalVotes / totalEligibleVoters < 0.6) { return false; } return votedFor.size() / totalVotes > 0.6; diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/VoteToMuteStarter.java b/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/VoteToMuteStarter.java index d20c8af..b08927e 100644 --- a/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/VoteToMuteStarter.java +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/vote_to_mute/VoteToMuteStarter.java @@ -73,19 +73,19 @@ public class VoteToMuteStarter { } public void showPage(int page) { - List collect = parsedChatLogs.stream().skip(page * 10L).limit(page).toList(); + List collect = parsedChatLogs.stream().skip((page - 1) * 10L).limit(10L).toList(); Component chatLogsComponent = Component.join(JoinConfiguration.newlines(), collect); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" ChatLogs for \n\n"); - if (page != 0) { - stringBuilder.append(" 1) { + stringBuilder.append("Click to go to previous page'> "); } if (parsedChatLogs.size() > page * 10) { - stringBuilder.append("Click to go to next page'> "); + .append(">Click to go to next page'> "); } commandSource.sendMessage(Utility.parseMiniMessage(stringBuilder.toString(), Placeholder.parsed("player", votedPlayer.getUsername()),