Add VoteToMuteHelper and enhance ActiveVoteToMute
Implemented a new module titled VoteToMuteHelper to enhance the voting system and augment the user experience. This module enhances the system by providing relevant player suggestions and setting up the mute player. Made updates to the ActiveVoteToMute module to handle potential voters and mute the player if the vote is passed. VoteToMute module is also updated to include the total eligible players. The code is made robust by adding appropriate error checks.
This commit is contained in:
parent
37aa9fdf4c
commit
c25767e473
|
|
@ -5,6 +5,7 @@ import com.alttd.chat.ChatImplementation;
|
|||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import com.alttd.velocitychat.commands.*;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
|
|
@ -23,6 +24,7 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
|||
import com.velocitypowered.api.plugin.Dependency;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ConsoleCommandSource;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
|
||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||
|
|
@ -108,12 +110,14 @@ public class VelocityChat {
|
|||
}
|
||||
|
||||
public void loadCommands() {
|
||||
ChatLogHandler instance = ChatLogHandler.getInstance(); //TODO disable logging part
|
||||
new SilentJoinCommand(server);
|
||||
new GlobalAdminChat(server);
|
||||
new Reload(server);
|
||||
new MailCommand(server);
|
||||
new Report(server);
|
||||
new VoteToMute(server);
|
||||
new VoteToMute(server, instance);
|
||||
new VoteToMuteHelper(server);
|
||||
server.getCommandManager().register("party", new PartyCommand());
|
||||
// all (proxy)commands go here
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package com.alttd.velocitychat.commands;
|
||||
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import com.alttd.chat.util.Utility;
|
||||
import com.alttd.velocitychat.commands.vote_to_mute.ActiveVoteToMute;
|
||||
import com.alttd.velocitychat.commands.vote_to_mute.VoteToMuteStarter;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
|
|
@ -17,7 +17,6 @@ import com.velocitypowered.api.proxy.ServerConnection;
|
|||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
|
@ -25,7 +24,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
public class VoteToMute {
|
||||
|
||||
public VoteToMute(ProxyServer proxyServer) {
|
||||
public VoteToMute(ProxyServer proxyServer, ChatLogHandler chatLogHandler) {
|
||||
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
|
||||
.<CommandSource, String>argument("player", StringArgumentType.string())
|
||||
.suggests((context, builder) -> {
|
||||
|
|
@ -59,77 +58,71 @@ public class VoteToMute {
|
|||
return 1;
|
||||
});
|
||||
|
||||
RequiredArgumentBuilder<CommandSource, String> yesNoNode = RequiredArgumentBuilder.
|
||||
<CommandSource, String>argument("vote", StringArgumentType.string())
|
||||
.suggests(((commandContext, suggestionsBuilder) -> {
|
||||
List<String> yesNoValues = Arrays.asList("yes", "no");
|
||||
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
|
||||
yesNoValues.stream()
|
||||
.filter((String str) -> str.toLowerCase().startsWith(remaining))
|
||||
.map(StringArgumentType::escapeIfRequired)
|
||||
.forEach(suggestionsBuilder::suggest);
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}));
|
||||
|
||||
LiteralArgumentBuilder<CommandSource> voteNode = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("vote")
|
||||
.then(playerNode
|
||||
.then(yesNoNode
|
||||
.executes(commandContext -> {
|
||||
if (!(commandContext.getSource() instanceof Player)) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>Only players are allowed to vote</red>"));
|
||||
}
|
||||
Player source = (Player) commandContext.getSource();
|
||||
String playerName = commandContext.getArgument("player", String.class);
|
||||
Optional<ActiveVoteToMute> optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName);
|
||||
if (optionalActiveVoteToMute.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>This player does not have an active vote to mute them</red>"));
|
||||
return 1;
|
||||
}
|
||||
ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get();
|
||||
String vote = commandContext.getArgument("vote", String.class);
|
||||
switch (vote.toLowerCase()) {
|
||||
case "yes" -> activeVoteToMute.vote(source.getUniqueId(), true);
|
||||
case "no" -> activeVoteToMute.vote(source.getUniqueId(), false);
|
||||
default -> commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red><vote> is not a valid vote option</red>", Placeholder.parsed("vote", vote)));
|
||||
}
|
||||
return 1;
|
||||
})).executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
})).executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
});
|
||||
|
||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("votetomute")
|
||||
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
|
||||
.requires(commandSource -> commandSource instanceof Player)
|
||||
.then(playerNode
|
||||
.suggests(((commandContext, suggestionsBuilder) -> {
|
||||
if (!(commandContext.getSource() instanceof Player player)) {
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isEmpty()) {
|
||||
sendHelpMessage(commandContext.getSource());
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}
|
||||
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
|
||||
currentServer.get().getServer().getPlayersConnected().stream()
|
||||
.filter(connectedPlayer -> connectedPlayer.hasPermission("chat.affected-by-vote-to-mute"))
|
||||
.map(Player::getUsername)
|
||||
.filter((String str) -> str.toLowerCase().startsWith(remaining))
|
||||
.map(StringArgumentType::escapeIfRequired)
|
||||
.forEach(suggestionsBuilder::suggest);
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}))
|
||||
.executes(commandContext -> {
|
||||
String playerName = commandContext.getArgument("player", String.class);
|
||||
Optional<Player> optionalPlayer = proxyServer.getPlayer(playerName);
|
||||
if (optionalPlayer.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>Player <player> is not online</red>",
|
||||
"<red>Player <player> is not online.</red>",
|
||||
Placeholder.parsed("player", playerName)));
|
||||
return 1;
|
||||
}
|
||||
Player voteTarget = optionalPlayer.get();
|
||||
if (!voteTarget.hasPermission("chat.affected-by-vote-to-mute")) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>Player <player> can not be muted by a vote.</red>",
|
||||
Placeholder.parsed("player", playerName)));
|
||||
return 1;
|
||||
}
|
||||
Player player = (Player) commandContext.getSource();
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isEmpty()) {
|
||||
sendHelpMessage(commandContext.getSource());
|
||||
return 1;
|
||||
}
|
||||
RegisteredServer server = currentServer.get().getServer();
|
||||
boolean countLowerRanks = false;
|
||||
long count = getTotalEligiblePlayers(server, false);
|
||||
if (count < 10) {
|
||||
countLowerRanks = true;
|
||||
count = getTotalEligiblePlayers(server, true);
|
||||
if (count < 10) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Not enough eligible players online to vote.</red>"));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
new VoteToMuteStarter(chatLogHandler, voteTarget, player, server.getServerInfo().getName(), countLowerRanks)
|
||||
.start();
|
||||
return 1;
|
||||
}))
|
||||
.then(voteNode)
|
||||
.executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
})
|
||||
.build();
|
||||
//TODO test command
|
||||
//TODO add command to pick out the messages
|
||||
//TODO add command to go to the next page
|
||||
|
||||
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
|
||||
|
||||
|
|
@ -140,6 +133,12 @@ public class VoteToMute {
|
|||
proxyServer.getCommandManager().register(meta, brigadierCommand);
|
||||
}
|
||||
|
||||
private int getTotalEligiblePlayers(RegisteredServer server, boolean countLowerRanks) {
|
||||
return (int) server.getPlayersConnected().stream()
|
||||
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
|
||||
.count();
|
||||
}
|
||||
|
||||
private void sendHelpMessage(CommandSource commandSource) {
|
||||
commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomute <player></gold>.</red>"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,261 @@
|
|||
package com.alttd.velocitychat.commands;
|
||||
|
||||
import com.alttd.chat.util.Utility;
|
||||
import com.alttd.velocitychat.commands.vote_to_mute.ActiveVoteToMute;
|
||||
import com.alttd.velocitychat.commands.vote_to_mute.VoteToMuteStarter;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import com.velocitypowered.api.command.BrigadierCommand;
|
||||
import com.velocitypowered.api.command.CommandMeta;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
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 net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class VoteToMuteHelper {
|
||||
|
||||
public VoteToMuteHelper(ProxyServer proxyServer) {
|
||||
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
|
||||
.<CommandSource, String>argument("player", StringArgumentType.string())
|
||||
.suggests((context, builder) -> {
|
||||
List<Player> possiblePlayers;
|
||||
if (context.getSource() instanceof Player player) {
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isPresent()) {
|
||||
possiblePlayers = getEligiblePlayers(currentServer.get().getServer());
|
||||
} else {
|
||||
possiblePlayers = getEligiblePlayers(proxyServer);
|
||||
}
|
||||
} else {
|
||||
possiblePlayers = getEligiblePlayers(proxyServer);
|
||||
}
|
||||
Collection<String> possibleValues = possiblePlayers.stream()
|
||||
.map(Player::getUsername)
|
||||
.toList();
|
||||
|
||||
if (possibleValues.isEmpty())
|
||||
return Suggestions.empty();
|
||||
|
||||
String remaining = builder.getRemaining().toLowerCase();
|
||||
possibleValues.stream()
|
||||
.filter(str -> str.toLowerCase().startsWith(remaining))
|
||||
.map(StringArgumentType::escapeIfRequired)
|
||||
.forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
})
|
||||
.executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
});
|
||||
|
||||
RequiredArgumentBuilder<CommandSource, String> yesNoNode = RequiredArgumentBuilder.
|
||||
<CommandSource, String>argument("yesNo", StringArgumentType.string())
|
||||
.suggests(((commandContext, suggestionsBuilder) -> {
|
||||
List<String> yesNoValues = Arrays.asList("yes", "no");
|
||||
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
|
||||
yesNoValues.stream()
|
||||
.filter((String str) -> str.toLowerCase().startsWith(remaining))
|
||||
.map(StringArgumentType::escapeIfRequired)
|
||||
.forEach(suggestionsBuilder::suggest);
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}));
|
||||
|
||||
LiteralArgumentBuilder<CommandSource> pageNode = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("page")
|
||||
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
|
||||
.then(RequiredArgumentBuilder.<CommandSource, Integer>argument("page number", IntegerArgumentType.integer(1))
|
||||
.suggests(((commandContext, suggestionsBuilder) -> {
|
||||
if (!(commandContext.getSource() instanceof Player player)) {
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}
|
||||
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
|
||||
if (instance.isEmpty()) {
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}
|
||||
VoteToMuteStarter voteToMuteStarter = instance.get();
|
||||
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
|
||||
int totalPages = voteToMuteStarter.getTotalPages();
|
||||
IntStream.range(1, totalPages + 1)
|
||||
.mapToObj(String::valueOf)
|
||||
.filter((String str) -> str.toLowerCase().startsWith(remaining))
|
||||
.map(StringArgumentType::escapeIfRequired)
|
||||
.forEach(suggestionsBuilder::suggest);
|
||||
return suggestionsBuilder.buildFuture();
|
||||
}))
|
||||
.executes(commandContext -> {
|
||||
if (!(commandContext.getSource() instanceof Player player)) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Only players can use this command.</red>"));
|
||||
return 1;
|
||||
}
|
||||
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
|
||||
if (instance.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>You don't have an active vote to mute.</red>"));
|
||||
return 1;
|
||||
}
|
||||
int pageNumber = commandContext.getArgument("page number", Integer.class);
|
||||
instance.get().showPage(pageNumber);
|
||||
return 1;
|
||||
})
|
||||
).executes(commandContext -> {
|
||||
sendHelpMessage(commandContext.getSource());
|
||||
return 1;
|
||||
});
|
||||
|
||||
LiteralArgumentBuilder<CommandSource> enterPageNode = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("messages")
|
||||
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
|
||||
.then(RequiredArgumentBuilder.<CommandSource, String>argument("list of messages", StringArgumentType.greedyString())
|
||||
.executes(commandContext -> {
|
||||
if (!(commandContext.getSource() instanceof Player player)) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Only players can use this command.</red>"));
|
||||
return 1;
|
||||
}
|
||||
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
|
||||
if (instance.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>You don't have an active vote to mute.</red>"));
|
||||
return 1;
|
||||
}
|
||||
String listOfPages = commandContext.getArgument("list of messages", String.class);
|
||||
if (!listOfPages.matches("([1-9][0-9]*, )*[1-9][0-9]*")) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Please make sure to format the command correctly.</red>"));
|
||||
return 1;
|
||||
}
|
||||
VoteToMuteStarter voteToMuteStarter = instance.get();
|
||||
|
||||
List<Integer> collect = Arrays.stream(listOfPages.split(", "))
|
||||
.map(Integer::parseInt)
|
||||
.collect(Collectors.toList());
|
||||
Optional<Integer> max = collect.stream().max(Integer::compare);
|
||||
if (max.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Some of your selected messages do not exist.</red>"));
|
||||
return 1;
|
||||
}
|
||||
int highestLogEntry = max.get();
|
||||
|
||||
if (voteToMuteStarter.getTotalLogEntries() > highestLogEntry) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Some of your selected messages do not exist.</red>"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isEmpty()) {
|
||||
sendHelpMessage(commandContext.getSource());
|
||||
return 1;
|
||||
}
|
||||
|
||||
Component chatLogs = voteToMuteStarter.getChatLogsAndClose(collect);
|
||||
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)
|
||||
.start();
|
||||
return 1;
|
||||
})
|
||||
).executes(commandContext -> {
|
||||
sendHelpMessage(commandContext.getSource());
|
||||
return 1;
|
||||
});
|
||||
|
||||
LiteralArgumentBuilder<CommandSource> voteNode = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("vote")
|
||||
.then(playerNode
|
||||
.then(yesNoNode
|
||||
.executes(commandContext -> {
|
||||
if (!(commandContext.getSource() instanceof Player)) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>Only players are allowed to vote</red>"));
|
||||
}
|
||||
Player source = (Player) commandContext.getSource();
|
||||
String playerName = commandContext.getArgument("player", String.class);
|
||||
Optional<ActiveVoteToMute> optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName);
|
||||
if (optionalActiveVoteToMute.isEmpty()) {
|
||||
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red>This player does not have an active vote to mute them.</red>"));
|
||||
return 1;
|
||||
}
|
||||
ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get();
|
||||
|
||||
if (!activeVoteToMute.countLowerRanks()) {
|
||||
if (!source.hasPermission("chat.vote-to-mute")) {
|
||||
source.sendMessage(Utility.parseMiniMessage("<red>You are not eligible to vote.</red>"));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
String vote = commandContext.getArgument("yesno", String.class);
|
||||
switch (vote.toLowerCase()) {
|
||||
case "yes" -> activeVoteToMute.vote(source.getUniqueId(), true);
|
||||
case "no" -> activeVoteToMute.vote(source.getUniqueId(), false);
|
||||
default -> commandContext.getSource().sendMessage(Utility.parseMiniMessage(
|
||||
"<red><vote> is not a valid vote option</red>", Placeholder.parsed("vote", vote)));
|
||||
}
|
||||
return 1;
|
||||
})).executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
})).executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
});
|
||||
|
||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
||||
.<CommandSource>literal("votetomutehelper")
|
||||
.requires(commandSource -> commandSource.hasPermission("chat.backup-vote-to-mute"))
|
||||
.requires(commandSource -> commandSource instanceof Player)
|
||||
.then(voteNode)
|
||||
.then(pageNode)
|
||||
.then(enterPageNode)
|
||||
.executes(context -> {
|
||||
sendHelpMessage(context.getSource());
|
||||
return 1;
|
||||
})
|
||||
.build();
|
||||
|
||||
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
|
||||
|
||||
CommandMeta.Builder metaBuilder = proxyServer.getCommandManager().metaBuilder(brigadierCommand);
|
||||
|
||||
CommandMeta meta = metaBuilder.build();
|
||||
|
||||
proxyServer.getCommandManager().register(meta, brigadierCommand);
|
||||
}
|
||||
|
||||
private int getTotalEligiblePlayers(RegisteredServer server, boolean countLowerRanks) {
|
||||
return (int) server.getPlayersConnected().stream()
|
||||
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
|
||||
.count();
|
||||
}
|
||||
|
||||
private void sendHelpMessage(CommandSource commandSource) {
|
||||
commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomutehelper <player></gold>.</red>"));
|
||||
}
|
||||
|
||||
private List<Player> getEligiblePlayers(ProxyServer proxyServer) {
|
||||
return proxyServer.getAllPlayers().stream()
|
||||
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Player> getEligiblePlayers(RegisteredServer registeredServer) {
|
||||
return registeredServer.getPlayersConnected().stream()
|
||||
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,29 +2,35 @@ package com.alttd.velocitychat.commands.vote_to_mute;
|
|||
|
||||
import com.alttd.chat.util.Utility;
|
||||
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 net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ActiveVoteToMute {
|
||||
|
||||
private static final HashMap<String, ActiveVoteToMute> instances = new HashMap<>();
|
||||
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>");
|
||||
|
||||
private final Instant start;
|
||||
private final Player votedPlayer;
|
||||
private final Duration duration;
|
||||
private HashSet<UUID> votedFor = new HashSet<>();
|
||||
private HashSet<UUID> votedAgainst = new HashSet<>();
|
||||
private int totalEligibleVoters;
|
||||
private final boolean countLowerRanks;
|
||||
private final RegisteredServer server;
|
||||
private final ProxyServer proxyServer;
|
||||
private final Component chatLogs;
|
||||
|
||||
public static Optional<ActiveVoteToMute> getInstance(String username) {
|
||||
if (!instances.containsKey(username))
|
||||
|
|
@ -32,26 +38,71 @@ public class ActiveVoteToMute {
|
|||
return Optional.of(instances.get(username));
|
||||
}
|
||||
|
||||
public ActiveVoteToMute(@NotNull Player votedPlayer, Duration duration, int totalEligibleVoters) {
|
||||
this.start = Instant.now();
|
||||
this.votedPlayer = votedPlayer;
|
||||
this.duration = duration;
|
||||
this.totalEligibleVoters = totalEligibleVoters;
|
||||
instances.put(votedPlayer.getUsername(), this);
|
||||
public static void removePotentialVoter(Player player, RegisteredServer previousServer) {
|
||||
if (!player.hasPermission("chat.backup-vote-to-mute"))
|
||||
return;
|
||||
if (player.hasPermission("chat.vote-to-mute")) {
|
||||
instances.values().stream()
|
||||
.filter(activeVoteToMute -> previousServer == null || activeVoteToMute.getServer().getServerInfo().hashCode() == previousServer.getServerInfo().hashCode())
|
||||
.forEach(inst -> inst.removeEligibleVoter(player.getUniqueId()));
|
||||
} else {
|
||||
instances.values().stream()
|
||||
.filter(ActiveVoteToMute::countLowerRanks)
|
||||
.filter(activeVoteToMute -> previousServer == null || activeVoteToMute.getServer().getServerInfo().hashCode() == previousServer.getServerInfo().hashCode())
|
||||
.forEach(inst -> inst.removeEligibleVoter(player.getUniqueId()));
|
||||
}
|
||||
}
|
||||
|
||||
public void start(@NotNull RegisteredServer registeredServer, Component chatLogs) {
|
||||
Component message = Utility.parseMiniMessage(
|
||||
String.format("""
|
||||
<prefix> <gold>[VoteMute]</gold> <green>A vote to mute <player> for one hour has been started, please read the logs below before voting.</green>
|
||||
<logs>
|
||||
<prefix> Click: <click:run_command:'/votetomute vote %s yes'><red>Mute</red></click> --- <click:run_command:'/votetomute vote %s no'><yellow>Don't mute</yellow></click>""",
|
||||
votedPlayer.getUsername(), votedPlayer.getUsername()),
|
||||
Placeholder.component("prefix", prefix),
|
||||
Placeholder.parsed("player", votedPlayer.getUsername()),
|
||||
Placeholder.component("logs", chatLogs));
|
||||
registeredServer.getPlayersConnected().stream()
|
||||
.filter(player -> player.hasPermission("chat.vote-to-mute"))
|
||||
public static void addPotentialVoter(Player player, ServerConnection server) {
|
||||
if (!player.hasPermission("chat.backup-vote-to-mute"))
|
||||
return;
|
||||
if (player.hasPermission("chat.vote-to-mute")) {
|
||||
instances.values().stream()
|
||||
.filter(activeVoteToMute -> activeVoteToMute.getServer().getServerInfo().hashCode() == server.getServerInfo().hashCode())
|
||||
.forEach(activeVoteToMute -> activeVoteToMute.addEligibleVoter(player));
|
||||
} else {
|
||||
instances.values().stream()
|
||||
.filter(ActiveVoteToMute::countLowerRanks)
|
||||
.filter(activeVoteToMute -> activeVoteToMute.getServer().getServerInfo().hashCode() == server.getServerInfo().hashCode())
|
||||
.forEach(activeVoteToMute -> activeVoteToMute.addEligibleVoter(player));
|
||||
}
|
||||
}
|
||||
|
||||
public ActiveVoteToMute(@NotNull Player votedPlayer, @NotNull RegisteredServer server, ProxyServer proxyServer, Duration duration,
|
||||
int totalEligibleVoters, boolean countLowerRanks, Component chatLogs) {
|
||||
this.chatLogs = chatLogs;
|
||||
this.votedPlayer = votedPlayer;
|
||||
this.totalEligibleVoters = totalEligibleVoters;
|
||||
this.countLowerRanks = countLowerRanks;
|
||||
this.server = server;
|
||||
this.proxyServer = proxyServer;
|
||||
instances.put(votedPlayer.getUsername(), this);
|
||||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
executorService.schedule(this::endVote,
|
||||
duration.toMinutes(), TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
private RegisteredServer getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
private void endVote() {
|
||||
instances.remove(votedPlayer.getUsername());
|
||||
if (votePassed()) {
|
||||
mutePlayer();
|
||||
return;
|
||||
}
|
||||
Component message = Utility.parseMiniMessage("<prefix> <red>The vote to mute <player> has failed, they will not be muted.</green>",
|
||||
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"))
|
||||
.forEach(player -> player.sendMessage(message));
|
||||
}
|
||||
|
||||
public void start() {
|
||||
Component message = getVoteStartMessage();
|
||||
server.getPlayersConnected().stream()
|
||||
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
|
||||
.forEach(player -> player.sendMessage(message));
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +110,11 @@ public class ActiveVoteToMute {
|
|||
if (votedToMute) {
|
||||
votedFor.add(uuid);
|
||||
votedAgainst.remove(uuid);
|
||||
if (!votePassed()) {
|
||||
return;
|
||||
}
|
||||
instances.remove(votedPlayer.getUsername());
|
||||
mutePlayer();
|
||||
} else {
|
||||
votedAgainst.add(uuid);
|
||||
votedFor.remove(uuid);
|
||||
|
|
@ -73,9 +129,43 @@ public class ActiveVoteToMute {
|
|||
return votedFor.size() / totalVotes > 0.6;
|
||||
}
|
||||
|
||||
public boolean voteEnded() {
|
||||
if (votedFor.size() + votedAgainst.size() == totalEligibleVoters)
|
||||
return true;
|
||||
return duration.minus(Duration.between(start, Instant.now())).isNegative();
|
||||
public boolean countLowerRanks() {
|
||||
return countLowerRanks;
|
||||
}
|
||||
|
||||
private void mutePlayer() {
|
||||
Component message = Utility.parseMiniMessage("<prefix> <green>The vote to mute <player> has passed, they will be muted.</green>",
|
||||
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"))
|
||||
.forEach(player -> player.sendMessage(message));
|
||||
proxyServer.getCommandManager().executeAsync(proxyServer.getConsoleCommandSource(),
|
||||
String.format("tempmute %s 1h Muted by the community - under review.", votedPlayer.getUsername()));
|
||||
}
|
||||
|
||||
public void addEligibleVoter(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
if (votedAgainst.contains(uuid) || votedFor.contains(uuid))
|
||||
return;
|
||||
totalEligibleVoters++;
|
||||
player.sendMessage(getVoteStartMessage());
|
||||
}
|
||||
|
||||
public void removeEligibleVoter(UUID uuid) {
|
||||
if (votedFor.contains(uuid) || votedAgainst.contains(uuid))
|
||||
return;
|
||||
totalEligibleVoters--;
|
||||
}
|
||||
|
||||
private Component getVoteStartMessage() {
|
||||
return Utility.parseMiniMessage(
|
||||
String.format("""
|
||||
<prefix> <gold>[VoteMute]</gold> <green>A vote to mute <player> for one hour has been started, please read the logs below before voting.</green>
|
||||
<logs>
|
||||
<prefix> Click: <click:run_command:'/votetomutehelper vote %s yes'><red>Mute</red></click> --- <click:run_command:'/votetomutehelper vote %s no'><yellow>Don't mute</yellow></click>""",
|
||||
votedPlayer.getUsername(), votedPlayer.getUsername()),
|
||||
Placeholder.component("prefix", prefix),
|
||||
Placeholder.parsed("player", votedPlayer.getUsername()),
|
||||
Placeholder.component("logs", chatLogs));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public class VoteToMuteStarter {
|
|||
private final Player commandSource;
|
||||
private final String serverName;
|
||||
private List<Component> parsedChatLogs;
|
||||
private final boolean countLowerRanks;
|
||||
|
||||
public static Optional<VoteToMuteStarter> getInstance(UUID uuid) {
|
||||
if (!instanceMap.containsKey(uuid))
|
||||
|
|
@ -31,11 +32,12 @@ public class VoteToMuteStarter {
|
|||
return Optional.of(instanceMap.get(uuid));
|
||||
}
|
||||
|
||||
public VoteToMuteStarter(ChatLogHandler chatLogHandler, Player votedPlayer, Player commandSource, String serverName) {
|
||||
public VoteToMuteStarter(ChatLogHandler chatLogHandler, Player votedPlayer, Player commandSource, String serverName, boolean countLowerRanks) {
|
||||
this.chatLogHandler = chatLogHandler;
|
||||
this.votedPlayer = votedPlayer;
|
||||
this.commandSource = commandSource;
|
||||
this.serverName = serverName;
|
||||
this.countLowerRanks = countLowerRanks;
|
||||
instanceMap.put(commandSource.getUniqueId(), this);
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +52,7 @@ public class VoteToMuteStarter {
|
|||
parseChatLogs(chatLogs);
|
||||
commandSource.sendMessage(Utility.parseMiniMessage(
|
||||
"<prefix> <green>Please select up to 10 messages other players should see to decide their vote, seperated by comma's. " +
|
||||
"Example: <gold>/votetomute messages 1, 2, 5, 8</gold></green>"));
|
||||
"Example: <gold>/votetomutehelper messages 1, 2, 5, 8</gold></green>"));
|
||||
showPage(0);
|
||||
});
|
||||
}
|
||||
|
|
@ -107,4 +109,20 @@ public class VoteToMuteStarter {
|
|||
instanceMap.remove(commandSource.getUniqueId());
|
||||
return Component.join(JoinConfiguration.newlines(), selectedChatLogs);
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return (int) Math.ceil((double) parsedChatLogs.size() / 10);
|
||||
}
|
||||
|
||||
public Player getVotedPlayer() {
|
||||
return votedPlayer;
|
||||
}
|
||||
|
||||
public int getTotalLogEntries() {
|
||||
return parsedChatLogs.size();
|
||||
}
|
||||
|
||||
public boolean countLowerRanks() {
|
||||
return countLowerRanks;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import com.alttd.chat.objects.Mail;
|
|||
import com.alttd.chat.util.Utility;
|
||||
import com.alttd.velocitychat.VelocityChat;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.velocitychat.commands.vote_to_mute.ActiveVoteToMute;
|
||||
import com.alttd.velocitychat.data.ServerWrapper;
|
||||
import com.alttd.velocitychat.handlers.ServerHandler;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
|
|
@ -17,6 +18,7 @@ import com.velocitypowered.api.event.connection.LoginEvent;
|
|||
import com.velocitypowered.api.event.player.ServerConnectedEvent;
|
||||
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ServerConnection;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
|
|
@ -48,9 +50,20 @@ public class ProxyPlayerListener {
|
|||
|
||||
@Subscribe(order = PostOrder.LAST)
|
||||
public void afterPlayerLogin(ServerPostConnectEvent event) {
|
||||
if (event.getPreviousServer() != null)
|
||||
return;
|
||||
Player player = event.getPlayer();
|
||||
RegisteredServer previousServer = event.getPreviousServer();
|
||||
if (previousServer != null) {
|
||||
ActiveVoteToMute.removePotentialVoter(player, previousServer);
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isEmpty())
|
||||
return;
|
||||
ActiveVoteToMute.addPotentialVoter(player, currentServer.get());
|
||||
return;
|
||||
}
|
||||
Optional<ServerConnection> currentServer = player.getCurrentServer();
|
||||
if (currentServer.isEmpty())
|
||||
return;
|
||||
ActiveVoteToMute.addPotentialVoter(player, currentServer.get());
|
||||
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
List<Mail> unReadMail = chatUser.getUnReadMail();
|
||||
if (unReadMail.isEmpty())
|
||||
|
|
@ -62,6 +75,7 @@ public class ProxyPlayerListener {
|
|||
|
||||
@Subscribe
|
||||
public void quitEvent(DisconnectEvent event) {
|
||||
ActiveVoteToMute.removePotentialVoter(event.getPlayer(), null);
|
||||
UUID uuid = event.getPlayer().getUniqueId();
|
||||
Party party = PartyManager.getParty(event.getPlayer().getUniqueId());
|
||||
if (party == null) return;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user