Resolves FS-318 by rewriting the /mute command

This has the fortunate side-effect of also fixing several bugs present in the original command.
This commit is contained in:
Video 2022-11-13 06:19:10 -07:00
parent f0d6549eec
commit af18299613
4 changed files with 95 additions and 147 deletions

View File

@ -1,7 +1,5 @@
package me.totalfreedom.totalfreedommod; package me.totalfreedom.totalfreedommod;
import java.util.ArrayList;
import java.util.List;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
@ -13,13 +11,9 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class Muter extends FreedomService public class Muter extends FreedomService
{ {
public final List<String> MUTED_PLAYERS = new ArrayList<>();
@Override @Override
public void onStart() public void onStart()
{ {
@ -45,7 +39,6 @@ public class Muter extends FreedomService
if (plugin.al.isAdminSync(player)) if (plugin.al.isAdminSync(player))
{ {
fPlayer.setMuted(false); fPlayer.setMuted(false);
MUTED_PLAYERS.remove(player.getName());
return; 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); 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);
}
}
} }

View File

@ -14,7 +14,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
@CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH) @CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH)
@CommandParameters(description = "Restricts/restores block modification abilities for everyone on the server or a certain player.", usage = "/<command> [[-s] <player> [reason] | list | purge | all]") @CommandParameters(description = "Restricts/restores block modification abilities for everyone on the server or a certain player.", usage = "/<command> [<player> [reason] | list | purge | all]")
public class Command_blockedit extends FreedomCommand public class Command_blockedit extends FreedomCommand
{ {
@Override @Override

View File

@ -1,10 +1,6 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.punishments.Punishment; import me.totalfreedom.totalfreedommod.punishments.Punishment;
import me.totalfreedom.totalfreedommod.punishments.PunishmentType; import me.totalfreedom.totalfreedommod.punishments.PunishmentType;
import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.rank.Rank;
@ -20,7 +16,6 @@ import org.bukkit.entity.Player;
@CommandParameters(description = "Mutes a player with brute force.", usage = "/<command> <[-s | -q] <player> [reason] | list | purge | all>", aliases = "stfu") @CommandParameters(description = "Mutes a player with brute force.", usage = "/<command> <[-s | -q] <player> [reason] | list | purge | all>", aliases = "stfu")
public class Command_mute extends FreedomCommand public class Command_mute extends FreedomCommand
{ {
@Override @Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) 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; return false;
} }
if (args[0].equalsIgnoreCase("list")) switch (args[0].toLowerCase())
{
case "list" ->
{ {
msg("Muted players:"); msg("Muted players:");
FPlayer info; List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
int count = 0; plugin.pl.getPlayer(player).isMuted()).toList();
for (Player mp : server.getOnlinePlayers())
{ if (list.size() > 0)
info = plugin.pl.getPlayer(mp); list.forEach(player -> msg("- " + player.getName()));
if (info.isMuted()) else
{
msg("- " + mp.getName());
count++;
}
}
if (count == 0)
{
msg("- none"); msg("- none");
} }
case "purge" ->
return true;
}
if (args[0].equalsIgnoreCase("purge"))
{ {
FUtil.adminAction(sender.getName(), "Unmuting all players.", true); FUtil.adminAction(sender.getName(), "Unmuting all players", true);
FPlayer info; List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
int count = 0; plugin.pl.getPlayer(player).isMuted()).toList();
for (Player mp : server.getOnlinePlayers())
{
info = plugin.pl.getPlayer(mp);
if (info.isMuted())
{
info.setMuted(false);
mp.sendTitle(ChatColor.RED + "You've been unmuted.", ChatColor.YELLOW + "Be sure to follow the rules!", 20, 100, 60);
count++;
}
}
plugin.mu.MUTED_PLAYERS.clear();
msg("Unmuted " + count + " players.");
return true;
}
if (args[0].equalsIgnoreCase("all")) list.forEach(player ->
{
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);
});
msg("Unmuted " + list.size() + " player" + (list.size() != 1 ? "s" : "") + ".");
}
case "all" ->
{ {
FUtil.adminAction(sender.getName(), "Muting all non-admins", true); FUtil.adminAction(sender.getName(), "Muting all non-admins", true);
List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
!plugin.al.isAdmin(player)).toList();
FPlayer playerdata; list.forEach(player ->
int counter = 0;
for (Player player : server.getOnlinePlayers())
{ {
if (!plugin.al.isAdmin(player)) plugin.pl.getPlayer(player).setMuted(true);
{ player.sendTitle(ChatColor.RED + "You've been muted globally.",
player.sendTitle(ChatColor.RED + "You've been muted globally.", ChatColor.YELLOW + "Please be patient and you will be unmuted shortly.", 20, 100, 60); ChatColor.YELLOW + "Please be patient and you will be unmuted shortly.", 20, 100, 60);
playerdata = plugin.pl.getPlayer(player); });
playerdata.setMuted(true);
counter++;
}
}
msg("Muted " + counter + " players."); msg("Muted " + list.size() + " player" + (list.size() != 1 ? "s" : "") + ".");
return true;
} }
default ->
// -s option (smite)
boolean smite = args[0].equals("-s");
// -q option (shadowmute)
boolean quiet = args[0].equals("-q");
if (smite || quiet)
{ {
boolean quiet = args[0].equalsIgnoreCase("-q");
boolean smite = args[0].equalsIgnoreCase("-s");
// Handling the -q parameter
if (quiet || smite)
{
if (args.length == 1) return false;
args = ArrayUtils.subarray(args, 1, args.length); args = ArrayUtils.subarray(args, 1, args.length);
}
if (args.length < 1) // Handling the (optional) reason
String reason = args.length > 1 ? StringUtils.join(args, " ", 1, args.length) : null;
// Showtime
Optional.ofNullable(getPlayer(args[0])).ifPresentOrElse(player ->
{ {
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)) if (plugin.al.isAdmin(player))
{ {
msg(player.getName() + " is an admin, and can't be muted."); msg(player.getName() + " is an admin, and as such can't be muted.", ChatColor.RED);
return true; return;
}
else if (plugin.pl.getPlayer(player).isMuted())
{
msg(player.getName() + " is already muted.", ChatColor.RED);
return;
} }
if (!playerdata.isMuted()) // Don't broadcast the mute if it was quiet
if (!quiet)
{ {
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); FUtil.adminAction(sender.getName(), "Muting " + player.getName(), true);
if (reason != null)
{
msg(player, ChatColor.RED + "Reason: " + ChatColor.YELLOW + reason);
} }
// Smite the player if we're supposed to
if (smite) if (smite)
{ {
Command_smite.smite(sender, player, reason); Command_smite.smite(sender, player, reason, true, false);
} }
msg(player, "You have been muted by " + ChatColor.YELLOW + sender.getName(), ChatColor.RED); // Mutes the player
msg("Muted " + player.getName()); plugin.pl.getPlayer(player).setMuted(true);
plugin.pul.logPunishment(new Punishment(player.getName(), FUtil.getIp(player), sender.getName(), PunishmentType.MUTE, reason)); // Notify the player that they have been muted
} player.sendTitle(ChatColor.RED + "You've been muted.",
else 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(ChatColor.RED + "That player is already muted."); 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));
}
} }
return true; return true;

View File

@ -294,7 +294,6 @@ public class FPlayer
public void setMuted(boolean muted, int minutes) public void setMuted(boolean muted, int minutes)
{ {
FUtil.cancel(unmuteTask); FUtil.cancel(unmuteTask);
plugin.mu.MUTED_PLAYERS.remove(getPlayer().getName());
unmuteTask = null; unmuteTask = null;
if (!muted) if (!muted)
@ -307,8 +306,6 @@ public class FPlayer
return; return;
} }
plugin.mu.MUTED_PLAYERS.add(getPlayer().getName());
// TODO: Simplify this into a Consumer<BukkitTask> lambda? // TODO: Simplify this into a Consumer<BukkitTask> lambda?
unmuteTask = new BukkitRunnable() unmuteTask = new BukkitRunnable()
{ {
@ -323,7 +320,6 @@ public class FPlayer
else else
{ {
FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + getName(), false); FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + getName(), false);
plugin.mu.MUTED_PLAYERS.remove(getName());
} }
} }
}.runTaskLater(plugin, minutes * (60L * 20L)); }.runTaskLater(plugin, minutes * (60L * 20L));