refactor: make whitelist remove by index like in the usage

This commit is contained in:
Chayapak 2024-11-14 16:11:00 +07:00
parent 66dc3b97ec
commit 627b18ebf9
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
2 changed files with 11 additions and 17 deletions

View file

@ -57,18 +57,18 @@ public class WhitelistCommand extends Command {
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "remove" -> {
final String player = context.getString(true, true);
try {
final int index = context.getInteger(true);
if (!bot.whitelist.list.contains(player)) throw new CommandException(Component.text("Player doesn't exist in the list"));
if (player.equals(bot.profile.getName())) throw new CommandException(Component.text("Cannot remove the bot"));
bot.whitelist.remove(player);
final String player = bot.whitelist.remove(index);
return Component.translatable(
"Removed %s from the whitelist",
Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
} catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) {
throw new CommandException(Component.text("Invalid index"));
}
}
case "clear" -> {
context.checkOverloadArgs(1);

View file

@ -51,13 +51,7 @@ public class WhitelistPlugin extends PlayersPlugin.Listener {
}
public void add (String player) { list.add(player); }
public void remove (String player) {
list.removeIf(eachPlayer -> eachPlayer.equals(player));
final PlayerEntry entry = bot.players.getEntry(player);
if (entry != null) handle(entry);
}
public String remove (int index) { return list.remove(index); }
public void clear () {
list.removeIf(eachPlayer -> !eachPlayer.equals(bot.profile.getName()));
}