vote_mute #2

Merged
stijn merged 19 commits from vote_mute into main 2025-01-22 20:45:21 +00:00
5 changed files with 29 additions and 12 deletions
Showing only changes of commit f3147b3256 - Show all commits

View File

@ -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<Boolean> 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");
});
}

View File

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

View File

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

View File

@ -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("<prefix> <red>The vote to mute <player> has failed, they will not be muted.</green>",
Component message = Utility.parseMiniMessage("<prefix> <red>The vote to mute <player> has failed, they will not be muted.</red>",
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;

View File

@ -73,19 +73,19 @@ public class VoteToMuteStarter {
}
public void showPage(int page) {
List<Component> collect = parsedChatLogs.stream().skip(page * 10L).limit(page).toList();
List<Component> collect = parsedChatLogs.stream().skip((page - 1) * 10L).limit(10L).toList();
Component chatLogsComponent = Component.join(JoinConfiguration.newlines(), collect);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<prefix> ChatLogs for <player>\n<logs>\n");
if (page != 0) {
stringBuilder.append("<click:run_command:/votetomute page ")
if (page > 1) {
stringBuilder.append("<click:run_command:/votetomutehelper page ")
.append(page - 1)
.append("><hover:show_text:'<gold>Click to go to previous page'><gold><previous page></gold></hover></click> ");
}
if (parsedChatLogs.size() > page * 10) {
stringBuilder.append("<click:run_command:/votetomute page ")
stringBuilder.append("<click:run_command:/votetomutehelper page ")
.append(page + 1)
.append("><hover:show_text:'<gold>Click to go to next page'><gold><previous page></gold></hover></click> ");
.append("><hover:show_text:'<gold>Click to go to next page'><gold><next page></gold></hover></click> ");
}
commandSource.sendMessage(Utility.parseMiniMessage(stringBuilder.toString(),
Placeholder.parsed("player", votedPlayer.getUsername()),