From 8393d12c6d5f888404c5f0206af324a342e519e0 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 5 May 2024 16:14:48 +0200 Subject: [PATCH] Update vote-to-mute feature The vote-to-mute feature is updated to include information about the vote initiating player. Also, the duration to retrieve chat logs increased from 5 minutes to 10 minutes. Lastly, eligible players are now notified live about the voting progress. --- .../commands/VoteToMuteHelper.java | 30 ++++++++++++++----- .../vote_to_mute/ActiveVoteToMute.java | 23 ++++++++++++-- .../vote_to_mute/VoteToMuteStarter.java | 7 +++-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/VoteToMuteHelper.java b/velocity/src/main/java/com/alttd/velocitychat/commands/VoteToMuteHelper.java index 0b26984..f646c81 100644 --- a/velocity/src/main/java/com/alttd/velocitychat/commands/VoteToMuteHelper.java +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/VoteToMuteHelper.java @@ -16,6 +16,7 @@ import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; +import jdk.jshell.execution.Util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -29,6 +30,8 @@ import java.util.stream.IntStream; public class VoteToMuteHelper { + private static final Component prefix = Utility.parseMiniMessage("[VoteMute]"); + public VoteToMuteHelper(ProxyServer proxyServer) { RequiredArgumentBuilder playerNode = RequiredArgumentBuilder .argument("player", StringArgumentType.string()) @@ -162,7 +165,7 @@ public class VoteToMuteHelper { RegisteredServer server = currentServer.get().getServer(); long count = getTotalEligiblePlayers(server, voteToMuteStarter.countLowerRanks()); new ActiveVoteToMute(voteToMuteStarter.getVotedPlayer(), server, proxyServer, Duration.ofMinutes(5), - (int) count, voteToMuteStarter.countLowerRanks(), chatLogs) + (int) count, voteToMuteStarter.countLowerRanks(), chatLogs, player) .start(); return 1; }) @@ -176,11 +179,11 @@ public class VoteToMuteHelper { .then(playerNode .then(yesNoNode .executes(commandContext -> { - if (!(commandContext.getSource() instanceof Player)) { + if (!(commandContext.getSource() instanceof Player player)) { commandContext.getSource().sendMessage(Utility.parseMiniMessage( "Only players are allowed to vote")); + return 1; } - Player source = (Player) commandContext.getSource(); String playerName = commandContext.getArgument("player", String.class); Optional optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName); if (optionalActiveVoteToMute.isEmpty()) { @@ -191,8 +194,8 @@ public class VoteToMuteHelper { ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get(); if (!activeVoteToMute.countLowerRanks()) { - if (!source.hasPermission("chat.vote-to-mute")) { - source.sendMessage(Utility.parseMiniMessage("You are not eligible to vote.")); + if (!player.hasPermission("chat.vote-to-mute")) { + player.sendMessage(Utility.parseMiniMessage("You are not eligible to vote.")); return 1; } } @@ -200,12 +203,13 @@ public class VoteToMuteHelper { String vote = commandContext.getArgument("yesNo", String.class); switch (vote.toLowerCase()) { case "yes" -> { - activeVoteToMute.vote(source.getUniqueId(), true); + activeVoteToMute.vote(player.getUniqueId(), true); commandContext.getSource().sendMessage(Utility.parseMiniMessage( "You voted to mute. Thanks for voting, staff will be online soon to review!")); + player.getCurrentServer().ifPresent(serverConnection -> notifyEligiblePlayers(serverConnection.getServer(), activeVoteToMute)); } case "no" -> { - activeVoteToMute.vote(source.getUniqueId(), false); + activeVoteToMute.vote(player.getUniqueId(), false); commandContext.getSource().sendMessage(Utility.parseMiniMessage( "You voted not to mute. Thanks for voting, staff will be online soon to review!")); } @@ -249,6 +253,18 @@ public class VoteToMuteHelper { .count(); } + private void notifyEligiblePlayers(RegisteredServer server, ActiveVoteToMute activeVoteToMute) { + Component message = Utility.parseMiniMessage(" out of players have voted to mute ", + Placeholder.component("prefix", prefix), + Placeholder.parsed("voted_for", String.valueOf(activeVoteToMute.getVotedFor())), + Placeholder.parsed("total_votes", String.valueOf(activeVoteToMute.getTotalEligibleVoters())), + Placeholder.parsed("player", activeVoteToMute.getVotedPlayer().getUsername())); + boolean countLowerRanks = activeVoteToMute.countLowerRanks(); + server.getPlayersConnected().stream() + .filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute")) + .forEach(player -> player.sendMessage(message)); + } + private void sendHelpMessage(CommandSource commandSource) { commandSource.sendMessage(Utility.parseMiniMessage("Use: /votetomutehelper .")); } 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 9d629f1..65978cd 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 @@ -28,6 +28,7 @@ public class ActiveVoteToMute { private static final Component prefix = Utility.parseMiniMessage("[VoteMute]"); private final Player votedPlayer; + private final Player startedByPlayer; private HashSet votedFor = new HashSet<>(); private HashSet votedAgainst = new HashSet<>(); private int totalEligibleVoters; @@ -74,7 +75,7 @@ public class ActiveVoteToMute { } public ActiveVoteToMute(@NotNull Player votedPlayer, @NotNull RegisteredServer server, ProxyServer proxyServer, Duration duration, - int totalEligibleVoters, boolean countLowerRanks, Component chatLogs) { + int totalEligibleVoters, boolean countLowerRanks, Component chatLogs, @NotNull Player startedByPlayer) { this.chatLogs = chatLogs; this.votedPlayer = votedPlayer; this.totalEligibleVoters = totalEligibleVoters; @@ -85,6 +86,7 @@ public class ActiveVoteToMute { ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.schedule(this::endVote, duration.toMinutes(), TimeUnit.MINUTES); + this.startedByPlayer = startedByPlayer; } private RegisteredServer getServer() { @@ -151,7 +153,7 @@ public class ActiveVoteToMute { .filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute")) .forEach(player -> player.sendMessage(message)); proxyServer.getCommandManager().executeAsync(proxyServer.getConsoleCommandSource(), - String.format("tempmute %s 1h Muted by the community - under review.", votedPlayer.getUsername())); + String.format("tempmute %s 1h Muted by the community - under review. -p", votedPlayer.getUsername())); String chatLogsString = PlainTextComponentSerializer.plainText().serialize(chatLogs); @@ -179,7 +181,10 @@ public class ActiveVoteToMute { false); embedBuilder.addField("Server", server.getServerInfo().getName().substring(0, 1).toUpperCase() + server.getServerInfo().getName().substring(1), - false); + true); + embedBuilder.addField("Started by", + String.format("Username: %s\nUUID: %s", startedByPlayer.getUsername(), startedByPlayer.getUniqueId().toString()), + true); return embedBuilder; } @@ -218,4 +223,16 @@ public class ActiveVoteToMute { Placeholder.parsed("player", votedPlayer.getUsername()), Placeholder.component("logs", chatLogs)); } + + public Player getVotedPlayer() { + return votedPlayer; + } + + public int getVotedFor() { + return votedFor.size(); + } + + public int getTotalEligibleVoters() { + return totalEligibleVoters; + } } 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 66ea87e..dfafe3c 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 @@ -42,7 +42,7 @@ public class VoteToMuteStarter { } public void start() { - chatLogHandler.retrieveChatLogs(votedPlayer.getUniqueId(), Duration.ofMinutes(5), serverName).whenCompleteAsync((chatLogs, throwable) -> { + chatLogHandler.retrieveChatLogs(votedPlayer.getUniqueId(), Duration.ofMinutes(10), serverName).whenCompleteAsync((chatLogs, throwable) -> { if (throwable != null) { commandSource.sendMessage(Utility.parseMiniMessage(" Unable to retrieve messages for player ", Placeholder.component("prefix", prefix), @@ -59,14 +59,15 @@ public class VoteToMuteStarter { private void parseChatLogs(List chatLogs) { TagResolver.Single playerTag = Placeholder.parsed("player", votedPlayer.getUsername()); + TagResolver.Single prefixTag = Placeholder.component("prefix", prefix); chatLogs.sort(Comparator.comparing(ChatLog::getTimestamp)); parsedChatLogs = IntStream.range(0, chatLogs.size()) .mapToObj(i -> Utility.parseMiniMessage( - ". [ChatLog] : ", + ". : ", TagResolver.resolver( Placeholder.unparsed("message", chatLogs.get(i).getMessage()), Placeholder.parsed("number", String.valueOf(i + 1)), - playerTag + playerTag, prefixTag )) ) .toList();