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).
This commit is contained in:
Video 2022-11-13 04:49:58 -07:00 committed by Paul Reilly
parent 5d4085ab07
commit 36663385ab

View File

@ -5,134 +5,106 @@ 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;
import me.totalfreedom.totalfreedommod.util.FUtil; 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.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List;
import java.util.Optional;
@CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH) @CommandPermissions(level = Rank.ADMIN, source = SourceType.BOTH)
@CommandParameters(description = "Restricts/unrestricts 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> [[-s] <player> [reason] | list | purge | all]")
public class Command_blockedit extends FreedomCommand public class Command_blockedit extends FreedomCommand
{ {
@Override @Override
public boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, String[] args, final boolean senderIsConsole) 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; switch (args[0].toLowerCase())
} {
case "list" ->
{
List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
plugin.pl.getPlayer(player).isEditBlocked()).sorted().toList();
if (args[0].equals("list")) // Oh dear god, why do I have to do it like this?
{ msg("There " + (list.size() != 1 ? "are " : "is ") + list.size() + " player"
msg("The following have block modification abilities restricted:"); + (list.size() != 1 ? "s" : "") + " online with restricted block modification abilities"
int count = 0; + (list.size() > 0 ? ":" : "."));
for (Player player : server.getOnlinePlayers())
{
final FPlayer info = plugin.pl.getPlayer(player);
if (info.isEditBlocked())
{
msg("- " + player.getName());
++count;
}
}
if (count == 0) list.forEach(player -> msg("- " + player.getName()));
}
case "purge" ->
{ {
msg("- none"); FUtil.adminAction(sender.getName(), "Restoring block modification abilities for all players", true);
}
return true;
}
if (args[0].equals("purge")) List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
{ plugin.pl.getPlayer(player).isEditBlocked()).toList();
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())
{
info.setEditBlocked(false);
++count;
}
}
msg("Unblocked all block modification abilities for " + count + " players.");
return true;
}
if (args[0].equals("all")) list.forEach(player ->
{ {
FUtil.adminAction(sender.getName(), "Blocking block modification abilities for all non-admins", true); plugin.pl.getPlayer(player).setEditBlocked(false);
int counter = 0; msg(player, "Your block modification abilities have been restored.", ChatColor.GREEN);
for (final Player player : this.server.getOnlinePlayers()) });
{
if (!plugin.al.isAdmin(player))
{
final FPlayer playerdata = plugin.pl.getPlayer(player);
playerdata.setEditBlocked(true);
++counter;
}
}
msg("Blocked block modification abilities for " + counter + " players."); msg("Restored block modification abilities for " + list.size() + " player"
return true; + (list.size() != 1 ? "s" : "") + ".");
} }
case "all", "-a" ->
{
FUtil.adminAction(sender.getName(), "Restricting block modification abilities for all non-admins", true);
final boolean smite = args[0].equals("-s"); List<? extends Player> list = server.getOnlinePlayers().stream().filter(player ->
if (smite) !plugin.al.isAdmin(player)).toList();
{
args = (String[])ArrayUtils.subarray(args, 1, args.length);
if (args.length < 1)
{
return false;
}
}
final Player player2 = getPlayer(args[0]); list.forEach(player ->
if (player2 == null)
{ {
msg(PLAYER_NOT_FOUND); plugin.pl.getPlayer(player).setEditBlocked(true);
return true; msg(player, "Your block modification abilities have been restricted.", ChatColor.RED);
} });
String reason = null; msg("Restricted block modification abilities for " + list.size() + " player"
if (args.length > 1) + (list.size() != 1 ? "s" : "") + ".");
{
reason = StringUtils.join(args, " ", 1, args.length);
} }
default -> Optional.ofNullable(getPlayer(args[0])).ifPresentOrElse(player ->
final FPlayer pd = plugin.pl.getPlayer(player2);
if (pd.isEditBlocked())
{ {
FUtil.adminAction(sender.getName(), "Unblocking block modification abilities for " + player2.getName(), true); FPlayer fPlayer = plugin.pl.getPlayer(player);
pd.setEditBlocked(false);
msg("Unblocking block modification abilities for " + player2.getName()); if (fPlayer.isEditBlocked())
msg(player2, "Your block modification abilities have been restored.", ChatColor.RED); {
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 else
{ {
if (plugin.al.isAdmin(player2)) if (plugin.al.isAdmin(player))
{ {
msg(player2.getName() + " is an admin, and cannot have their block edits blocked."); 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));
}
return true; return true;
} }
else
FUtil.adminAction(sender.getName(), "Blocking block modification abilities for " + player2.getName(), true);
pd.setEditBlocked(true);
if (smite)
{ {
Command_smite.smite(sender, player2, reason); return false;
} }
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 true;
} }
} }