From 36663385abc47501cdafea4d058a6304ff615641 Mon Sep 17 00:00:00 2001 From: Video Date: Sun, 13 Nov 2022 04:49:58 -0700 Subject: [PATCH] Resolves FS-316 by aggressively rewriting /blockedit Disclaimer: I did this whilst sleep deprvied. I didn't know how the fuck NPath is calculated so to play it safe I aggressively rewrote the command. I also took the opportunity to make some of the command's messages more consistent with eachother (using restrict/restore instead of block/unblock). --- .../command/Command_blockedit.java | 178 ++++++++---------- 1 file changed, 75 insertions(+), 103 deletions(-) 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 cba22999..e5283368 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_blockedit.java @@ -5,134 +5,106 @@ import me.totalfreedom.totalfreedommod.punishments.Punishment; import me.totalfreedom.totalfreedommod.punishments.PunishmentType; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FUtil; -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.List; +import java.util.Optional; + @CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH) -@CommandParameters(description = "Restricts/unrestricts 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 = "/ [[-s] [reason] | list | purge | all]") public class Command_blockedit extends FreedomCommand { - @Override public boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, String[] args, final boolean senderIsConsole) { - if (args.length == 0) + if (args.length > 0) { - return false; - } - - if (args[0].equals("list")) - { - msg("The following have block modification abilities restricted:"); - int count = 0; - for (Player player : server.getOnlinePlayers()) + switch (args[0].toLowerCase()) { - final FPlayer info = plugin.pl.getPlayer(player); - if (info.isEditBlocked()) + case "list" -> { - msg("- " + player.getName()); - ++count; + List list = server.getOnlinePlayers().stream().filter(player -> + plugin.pl.getPlayer(player).isEditBlocked()).sorted().toList(); + + // Oh dear god, why do I have to do it like this? + msg("There " + (list.size() != 1 ? "are " : "is ") + list.size() + " player" + + (list.size() != 1 ? "s" : "") + " online with restricted block modification abilities" + + (list.size() > 0 ? ":" : ".")); + + list.forEach(player -> msg("- " + player.getName())); } - } - - if (count == 0) - { - msg("- none"); - } - return true; - } - - if (args[0].equals("purge")) - { - FUtil.adminAction(sender.getName(), "Unblocking block modification abilities for all players", true); - int count = 0; - for (final Player player : this.server.getOnlinePlayers()) - { - final FPlayer info = plugin.pl.getPlayer(player); - if (info.isEditBlocked()) + case "purge" -> { - info.setEditBlocked(false); - ++count; - } - } - msg("Unblocked all block modification abilities for " + count + " players."); - return true; - } + FUtil.adminAction(sender.getName(), "Restoring block modification abilities for all players", true); - if (args[0].equals("all")) - { - FUtil.adminAction(sender.getName(), "Blocking block modification abilities for all non-admins", true); - int counter = 0; - for (final Player player : this.server.getOnlinePlayers()) - { - if (!plugin.al.isAdmin(player)) + List list = server.getOnlinePlayers().stream().filter(player -> + plugin.pl.getPlayer(player).isEditBlocked()).toList(); + + list.forEach(player -> + { + plugin.pl.getPlayer(player).setEditBlocked(false); + msg(player, "Your block modification abilities have been restored.", ChatColor.GREEN); + }); + + msg("Restored block modification abilities for " + list.size() + " player" + + (list.size() != 1 ? "s" : "") + "."); + } + case "all", "-a" -> { - final FPlayer playerdata = plugin.pl.getPlayer(player); - playerdata.setEditBlocked(true); - ++counter; + FUtil.adminAction(sender.getName(), "Restricting block modification abilities for all non-admins", true); + + List list = server.getOnlinePlayers().stream().filter(player -> + !plugin.al.isAdmin(player)).toList(); + + list.forEach(player -> + { + plugin.pl.getPlayer(player).setEditBlocked(true); + msg(player, "Your block modification abilities have been restricted.", ChatColor.RED); + }); + + msg("Restricted block modification abilities for " + list.size() + " player" + + (list.size() != 1 ? "s" : "") + "."); } + default -> Optional.ofNullable(getPlayer(args[0])).ifPresentOrElse(player -> + { + FPlayer fPlayer = plugin.pl.getPlayer(player); + + if (fPlayer.isEditBlocked()) + { + FUtil.adminAction(sender.getName(), "Restoring block modification abilities for " + player.getName(), true); + fPlayer.setEditBlocked(false); + msg("Restored block modification abilities for " + player.getName() + "."); + msg(player, "Your block modification abilities have been restored.", ChatColor.GREEN); + } + else + { + if (plugin.al.isAdmin(player)) + { + msg(player.getName() + " is an admin, and as such cannot have their block modification abilities restricted.", ChatColor.RED); + } + else + { + FUtil.adminAction(sender.getName(), "Restricting block modification abilities for " + player.getName(), true); + fPlayer.setEditBlocked(true); + msg("Restricted block modification abilities for " + player.getName() + "."); + msg(player, "Your block modification abilities have been restricted.", ChatColor.RED); + + plugin.pul.logPunishment(new Punishment(player.getName(), FUtil.getIp(player), + sender.getName(), PunishmentType.BLOCKEDIT, null)); + } + } + + }, () -> msg(PLAYER_NOT_FOUND)); } - msg("Blocked block modification abilities for " + counter + " players."); return true; } - - final boolean smite = args[0].equals("-s"); - if (smite) - { - args = (String[])ArrayUtils.subarray(args, 1, args.length); - if (args.length < 1) - { - return false; - } - } - - final Player player2 = getPlayer(args[0]); - if (player2 == null) - { - msg(PLAYER_NOT_FOUND); - return true; - } - - String reason = null; - if (args.length > 1) - { - reason = StringUtils.join(args, " ", 1, args.length); - } - - final FPlayer pd = plugin.pl.getPlayer(player2); - if (pd.isEditBlocked()) - { - FUtil.adminAction(sender.getName(), "Unblocking block modification abilities for " + player2.getName(), true); - pd.setEditBlocked(false); - msg("Unblocking block modification abilities for " + player2.getName()); - msg(player2, "Your block modification abilities have been restored.", ChatColor.RED); - } else { - if (plugin.al.isAdmin(player2)) - { - msg(player2.getName() + " is an admin, and cannot have their block edits blocked."); - return true; - } - - FUtil.adminAction(sender.getName(), "Blocking block modification abilities for " + player2.getName(), true); - pd.setEditBlocked(true); - - if (smite) - { - Command_smite.smite(sender, player2, reason); - } - - msg(player2, "Your block modification abilities have been blocked.", ChatColor.RED); - msg("Blocked all block modification abilities for " + player2.getName()); - - plugin.pul.logPunishment(new Punishment(player2.getName(), FUtil.getIp(player2), sender.getName(), PunishmentType.BLOCKEDIT, null)); + return false; } - return true; } } \ No newline at end of file