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.
This commit is contained in:
Teriuihi 2024-05-05 16:14:48 +02:00
parent 782c7df4ef
commit 8393d12c6d
3 changed files with 47 additions and 13 deletions

View File

@ -16,6 +16,7 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import jdk.jshell.execution.Util;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
@ -29,6 +30,8 @@ import java.util.stream.IntStream;
public class VoteToMuteHelper { public class VoteToMuteHelper {
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>");
public VoteToMuteHelper(ProxyServer proxyServer) { public VoteToMuteHelper(ProxyServer proxyServer) {
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
.<CommandSource, String>argument("player", StringArgumentType.string()) .<CommandSource, String>argument("player", StringArgumentType.string())
@ -162,7 +165,7 @@ public class VoteToMuteHelper {
RegisteredServer server = currentServer.get().getServer(); RegisteredServer server = currentServer.get().getServer();
long count = getTotalEligiblePlayers(server, voteToMuteStarter.countLowerRanks()); long count = getTotalEligiblePlayers(server, voteToMuteStarter.countLowerRanks());
new ActiveVoteToMute(voteToMuteStarter.getVotedPlayer(), server, proxyServer, Duration.ofMinutes(5), new ActiveVoteToMute(voteToMuteStarter.getVotedPlayer(), server, proxyServer, Duration.ofMinutes(5),
(int) count, voteToMuteStarter.countLowerRanks(), chatLogs) (int) count, voteToMuteStarter.countLowerRanks(), chatLogs, player)
.start(); .start();
return 1; return 1;
}) })
@ -176,11 +179,11 @@ public class VoteToMuteHelper {
.then(playerNode .then(playerNode
.then(yesNoNode .then(yesNoNode
.executes(commandContext -> { .executes(commandContext -> {
if (!(commandContext.getSource() instanceof Player)) { if (!(commandContext.getSource() instanceof Player player)) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage( commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red>Only players are allowed to vote</red>")); "<red>Only players are allowed to vote</red>"));
return 1;
} }
Player source = (Player) commandContext.getSource();
String playerName = commandContext.getArgument("player", String.class); String playerName = commandContext.getArgument("player", String.class);
Optional<ActiveVoteToMute> optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName); Optional<ActiveVoteToMute> optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName);
if (optionalActiveVoteToMute.isEmpty()) { if (optionalActiveVoteToMute.isEmpty()) {
@ -191,8 +194,8 @@ public class VoteToMuteHelper {
ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get(); ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get();
if (!activeVoteToMute.countLowerRanks()) { if (!activeVoteToMute.countLowerRanks()) {
if (!source.hasPermission("chat.vote-to-mute")) { if (!player.hasPermission("chat.vote-to-mute")) {
source.sendMessage(Utility.parseMiniMessage("<red>You are not eligible to vote.</red>")); player.sendMessage(Utility.parseMiniMessage("<red>You are not eligible to vote.</red>"));
return 1; return 1;
} }
} }
@ -200,12 +203,13 @@ public class VoteToMuteHelper {
String vote = commandContext.getArgument("yesNo", String.class); String vote = commandContext.getArgument("yesNo", String.class);
switch (vote.toLowerCase()) { switch (vote.toLowerCase()) {
case "yes" -> { case "yes" -> {
activeVoteToMute.vote(source.getUniqueId(), true); activeVoteToMute.vote(player.getUniqueId(), true);
commandContext.getSource().sendMessage(Utility.parseMiniMessage( commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<green>You voted to mute. Thanks for voting, staff will be online soon to review!</green>")); "<green>You voted to mute. Thanks for voting, staff will be online soon to review!</green>"));
player.getCurrentServer().ifPresent(serverConnection -> notifyEligiblePlayers(serverConnection.getServer(), activeVoteToMute));
} }
case "no" -> { case "no" -> {
activeVoteToMute.vote(source.getUniqueId(), false); activeVoteToMute.vote(player.getUniqueId(), false);
commandContext.getSource().sendMessage(Utility.parseMiniMessage( commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<green>You voted <red>not</red> to mute. Thanks for voting, staff will be online soon to review!</green>")); "<green>You voted <red>not</red> to mute. Thanks for voting, staff will be online soon to review!</green>"));
} }
@ -249,6 +253,18 @@ public class VoteToMuteHelper {
.count(); .count();
} }
private void notifyEligiblePlayers(RegisteredServer server, ActiveVoteToMute activeVoteToMute) {
Component message = Utility.parseMiniMessage("<prefix><green><voted_for> out of <total_votes> players have voted to mute <player></green>",
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) { private void sendHelpMessage(CommandSource commandSource) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomutehelper <player></gold>.</red>")); commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomutehelper <player></gold>.</red>"));
} }

View File

@ -28,6 +28,7 @@ public class ActiveVoteToMute {
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>"); private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>");
private final Player votedPlayer; private final Player votedPlayer;
private final Player startedByPlayer;
private HashSet<UUID> votedFor = new HashSet<>(); private HashSet<UUID> votedFor = new HashSet<>();
private HashSet<UUID> votedAgainst = new HashSet<>(); private HashSet<UUID> votedAgainst = new HashSet<>();
private int totalEligibleVoters; private int totalEligibleVoters;
@ -74,7 +75,7 @@ public class ActiveVoteToMute {
} }
public ActiveVoteToMute(@NotNull Player votedPlayer, @NotNull RegisteredServer server, ProxyServer proxyServer, Duration duration, 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.chatLogs = chatLogs;
this.votedPlayer = votedPlayer; this.votedPlayer = votedPlayer;
this.totalEligibleVoters = totalEligibleVoters; this.totalEligibleVoters = totalEligibleVoters;
@ -85,6 +86,7 @@ public class ActiveVoteToMute {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule(this::endVote, executorService.schedule(this::endVote,
duration.toMinutes(), TimeUnit.MINUTES); duration.toMinutes(), TimeUnit.MINUTES);
this.startedByPlayer = startedByPlayer;
} }
private RegisteredServer getServer() { 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")) .filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.forEach(player -> player.sendMessage(message)); .forEach(player -> player.sendMessage(message));
proxyServer.getCommandManager().executeAsync(proxyServer.getConsoleCommandSource(), 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); String chatLogsString = PlainTextComponentSerializer.plainText().serialize(chatLogs);
@ -179,7 +181,10 @@ public class ActiveVoteToMute {
false); false);
embedBuilder.addField("Server", embedBuilder.addField("Server",
server.getServerInfo().getName().substring(0, 1).toUpperCase() + server.getServerInfo().getName().substring(1), 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; return embedBuilder;
} }
@ -218,4 +223,16 @@ public class ActiveVoteToMute {
Placeholder.parsed("player", votedPlayer.getUsername()), Placeholder.parsed("player", votedPlayer.getUsername()),
Placeholder.component("logs", chatLogs)); Placeholder.component("logs", chatLogs));
} }
public Player getVotedPlayer() {
return votedPlayer;
}
public int getVotedFor() {
return votedFor.size();
}
public int getTotalEligibleVoters() {
return totalEligibleVoters;
}
} }

View File

@ -42,7 +42,7 @@ public class VoteToMuteStarter {
} }
public void start() { 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) { if (throwable != null) {
commandSource.sendMessage(Utility.parseMiniMessage("<prefix> <red>Unable to retrieve messages</red> for player <player>", commandSource.sendMessage(Utility.parseMiniMessage("<prefix> <red>Unable to retrieve messages</red> for player <player>",
Placeholder.component("prefix", prefix), Placeholder.component("prefix", prefix),
@ -59,14 +59,15 @@ public class VoteToMuteStarter {
private void parseChatLogs(List<ChatLog> chatLogs) { private void parseChatLogs(List<ChatLog> chatLogs) {
TagResolver.Single playerTag = Placeholder.parsed("player", votedPlayer.getUsername()); TagResolver.Single playerTag = Placeholder.parsed("player", votedPlayer.getUsername());
TagResolver.Single prefixTag = Placeholder.component("prefix", prefix);
chatLogs.sort(Comparator.comparing(ChatLog::getTimestamp)); chatLogs.sort(Comparator.comparing(ChatLog::getTimestamp));
parsedChatLogs = IntStream.range(0, chatLogs.size()) parsedChatLogs = IntStream.range(0, chatLogs.size())
.mapToObj(i -> Utility.parseMiniMessage( .mapToObj(i -> Utility.parseMiniMessage(
"<number>. [ChatLog] <player>: <message>", "<number>. <prefix> <player>: <message>",
TagResolver.resolver( TagResolver.resolver(
Placeholder.unparsed("message", chatLogs.get(i).getMessage()), Placeholder.unparsed("message", chatLogs.get(i).getMessage()),
Placeholder.parsed("number", String.valueOf(i + 1)), Placeholder.parsed("number", String.valueOf(i + 1)),
playerTag playerTag, prefixTag
)) ))
) )
.toList(); .toList();