From 1937a3d4ded7fda8aaac7e7c82c8328a7934792d Mon Sep 17 00:00:00 2001 From: Video Date: Sun, 13 Nov 2022 06:19:10 -0700 Subject: [PATCH] Resolves FS-318 by rewriting the /mute command This has the fortunate side-effect of also fixing several bugs present in the original command. --- .../totalfreedom/totalfreedommod/Muter.java | 19 -- .../command/Command_blockedit.java | 2 +- .../totalfreedommod/command/Command_mute.java | 217 ++++++++---------- .../totalfreedommod/player/FPlayer.java | 4 - 4 files changed, 95 insertions(+), 147 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java index c6b336ae..b6b50d50 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java @@ -1,7 +1,5 @@ package me.totalfreedom.totalfreedommod; -import java.util.ArrayList; -import java.util.List; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.util.FLog; @@ -13,13 +11,9 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerJoinEvent; public class Muter extends FreedomService { - - public final List MUTED_PLAYERS = new ArrayList<>(); - @Override public void onStart() { @@ -45,7 +39,6 @@ public class Muter extends FreedomService if (plugin.al.isAdminSync(player)) { fPlayer.setMuted(false); - MUTED_PLAYERS.remove(player.getName()); return; } @@ -98,16 +91,4 @@ public class Muter extends FreedomService FLog.info(String.format("[PREPROCESS_COMMAND] %s(%s): %s", player.getName(), ChatColor.stripColor(player.getDisplayName()), message), true); } } - - @EventHandler(priority = EventPriority.LOW) - public void onPlayerJoin(PlayerJoinEvent event) - { - Player player = event.getPlayer(); - FPlayer playerdata = plugin.pl.getPlayer(player); - - if (MUTED_PLAYERS.contains(player.getName())) - { - playerdata.setMuted(true); - } - } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java index e5283368..9a32d157 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java @@ -14,7 +14,7 @@ import java.util.List; import java.util.Optional; @CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH) -@CommandParameters(description = "Restricts/restores block modification abilities for everyone on the server or a certain player.", usage = "/ [[-s] [reason] | list | purge | all]") +@CommandParameters(description = "Restricts/restores block modification abilities for everyone on the server or a certain player.", usage = "/ [ [reason] | list | purge | all]") public class Command_blockedit extends FreedomCommand { @Override diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mute.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mute.java index e66c0c73..dc2ee775 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mute.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mute.java @@ -1,10 +1,6 @@ package me.totalfreedom.totalfreedommod.command; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import me.totalfreedom.totalfreedommod.player.FPlayer; +import java.util.*; import me.totalfreedom.totalfreedommod.punishments.Punishment; import me.totalfreedom.totalfreedommod.punishments.PunishmentType; import me.totalfreedom.totalfreedommod.rank.Rank; @@ -20,7 +16,6 @@ import org.bukkit.entity.Player; @CommandParameters(description = "Mutes a player with brute force.", usage = "/ <[-s | -q] [reason] | list | purge | all>", aliases = "stfu") public class Command_mute extends FreedomCommand { - @Override public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { @@ -29,134 +24,110 @@ public class Command_mute extends FreedomCommand return false; } - if (args[0].equalsIgnoreCase("list")) + switch (args[0].toLowerCase()) { - msg("Muted players:"); - FPlayer info; - int count = 0; - for (Player mp : server.getOnlinePlayers()) + case "list" -> { - info = plugin.pl.getPlayer(mp); - if (info.isMuted()) + msg("Muted players:"); + List list = server.getOnlinePlayers().stream().filter(player -> + plugin.pl.getPlayer(player).isMuted()).toList(); + + if (list.size() > 0) + list.forEach(player -> msg("- " + player.getName())); + else + msg("- none"); + } + case "purge" -> + { + FUtil.adminAction(sender.getName(), "Unmuting all players", true); + List list = server.getOnlinePlayers().stream().filter(player -> + plugin.pl.getPlayer(player).isMuted()).toList(); + + list.forEach(player -> { - msg("- " + mp.getName()); - count++; - } - } - if (count == 0) - { - msg("- none"); - } + plugin.pl.getPlayer(player).setMuted(false); + player.sendTitle(ChatColor.RED + "You have been unmuted.", + ChatColor.YELLOW + "Be sure to follow the rules!", 20, 100, 60); + }); - return true; - } - - if (args[0].equalsIgnoreCase("purge")) - { - FUtil.adminAction(sender.getName(), "Unmuting all players.", true); - FPlayer info; - int count = 0; - for (Player mp : server.getOnlinePlayers()) + msg("Unmuted " + list.size() + " player" + (list.size() != 1 ? "s" : "") + "."); + } + case "all" -> { - info = plugin.pl.getPlayer(mp); - if (info.isMuted()) + FUtil.adminAction(sender.getName(), "Muting all non-admins", true); + List list = server.getOnlinePlayers().stream().filter(player -> + !plugin.al.isAdmin(player)).toList(); + + list.forEach(player -> { - info.setMuted(false); - mp.sendTitle(ChatColor.RED + "You've been unmuted.", ChatColor.YELLOW + "Be sure to follow the rules!", 20, 100, 60); - count++; - } + plugin.pl.getPlayer(player).setMuted(true); + player.sendTitle(ChatColor.RED + "You've been muted globally.", + ChatColor.YELLOW + "Please be patient and you will be unmuted shortly.", 20, 100, 60); + }); + + msg("Muted " + list.size() + " player" + (list.size() != 1 ? "s" : "") + "."); } - plugin.mu.MUTED_PLAYERS.clear(); - msg("Unmuted " + count + " players."); - return true; - } - - if (args[0].equalsIgnoreCase("all")) - { - FUtil.adminAction(sender.getName(), "Muting all non-admins", true); - - FPlayer playerdata; - int counter = 0; - for (Player player : server.getOnlinePlayers()) + default -> { - if (!plugin.al.isAdmin(player)) + boolean quiet = args[0].equalsIgnoreCase("-q"); + boolean smite = args[0].equalsIgnoreCase("-s"); + + // Handling the -q parameter + if (quiet || smite) { - player.sendTitle(ChatColor.RED + "You've been muted globally.", ChatColor.YELLOW + "Please be patient and you will be unmuted shortly.", 20, 100, 60); - playerdata = plugin.pl.getPlayer(player); - playerdata.setMuted(true); - counter++; + if (args.length == 1) return false; + args = ArrayUtils.subarray(args, 1, args.length); } + + // Handling the (optional) reason + String reason = args.length > 1 ? StringUtils.join(args, " ", 1, args.length) : null; + + // Showtime + Optional.ofNullable(getPlayer(args[0])).ifPresentOrElse(player -> + { + if (plugin.al.isAdmin(player)) + { + msg(player.getName() + " is an admin, and as such can't be muted.", ChatColor.RED); + return; + } + else if (plugin.pl.getPlayer(player).isMuted()) + { + msg(player.getName() + " is already muted.", ChatColor.RED); + return; + } + + // Don't broadcast the mute if it was quiet + if (!quiet) + { + FUtil.adminAction(sender.getName(), "Muting " + player.getName(), true); + } + + // Smite the player if we're supposed to + if (smite) + { + Command_smite.smite(sender, player, reason, true, false); + } + + // Mutes the player + plugin.pl.getPlayer(player).setMuted(true); + + // Notify the player that they have been muted + player.sendTitle(ChatColor.RED + "You've been muted.", + ChatColor.YELLOW + "Be sure to follow the rules!", 20, 100, 60); + msg(player, "You have been muted by " + ChatColor.YELLOW + sender.getName() + + ChatColor.RED + ".", ChatColor.RED); + + // Give them the reason if one is present. + if (reason != null) + { + msg(player, "Reason: " + ChatColor.YELLOW + reason, ChatColor.RED); + } + + msg((quiet ? "Quietly m" : "M") + "uted " + player.getName() + "."); + plugin.pul.logPunishment(new Punishment(player.getName(), FUtil.getIp(player), sender.getName(), + PunishmentType.MUTE, reason)); + }, () -> msg(PLAYER_NOT_FOUND)); } - - msg("Muted " + counter + " players."); - return true; - } - - // -s option (smite) - boolean smite = args[0].equals("-s"); - // -q option (shadowmute) - boolean quiet = args[0].equals("-q"); - if (smite || quiet) - { - args = ArrayUtils.subarray(args, 1, args.length); - - if (args.length < 1) - { - return false; - } - } - - final Player player = getPlayer(args[0]); - if (player == null) - { - msg(PLAYER_NOT_FOUND); - return true; - } - - String reason = null; - if (args.length > 1) - { - reason = StringUtils.join(args, " ", 1, args.length); - } - - FPlayer playerdata = plugin.pl.getPlayer(player); - if (plugin.al.isAdmin(player)) - { - msg(player.getName() + " is an admin, and can't be muted."); - return true; - } - - if (!playerdata.isMuted()) - { - playerdata.setMuted(true); - player.sendTitle(ChatColor.RED + "You've been muted.", ChatColor.YELLOW + "Be sure to follow the rules!", 20, 100, 60); - - if (quiet) - { - msg("Muted " + player.getName() + " quietly"); - return true; // doesn't announce reason - } - - FUtil.adminAction(sender.getName(), "Muting " + player.getName(), true); - - if (reason != null) - { - msg(player, ChatColor.RED + "Reason: " + ChatColor.YELLOW + reason); - } - - if (smite) - { - Command_smite.smite(sender, player, reason); - } - - msg(player, "You have been muted by " + ChatColor.YELLOW + sender.getName(), ChatColor.RED); - msg("Muted " + player.getName()); - - plugin.pul.logPunishment(new Punishment(player.getName(), FUtil.getIp(player), sender.getName(), PunishmentType.MUTE, reason)); - } - else - { - msg(ChatColor.RED + "That player is already muted."); } return true; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java b/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java index ced5a67a..6cfa0e87 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java @@ -294,7 +294,6 @@ public class FPlayer public void setMuted(boolean muted, int minutes) { FUtil.cancel(unmuteTask); - plugin.mu.MUTED_PLAYERS.remove(getPlayer().getName()); unmuteTask = null; if (!muted) @@ -307,8 +306,6 @@ public class FPlayer return; } - plugin.mu.MUTED_PLAYERS.add(getPlayer().getName()); - // TODO: Simplify this into a Consumer lambda? unmuteTask = new BukkitRunnable() { @@ -323,7 +320,6 @@ public class FPlayer else { FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + getName(), false); - plugin.mu.MUTED_PLAYERS.remove(getName()); } } }.runTaskLater(plugin, minutes * (60L * 20L));