From 627b18ebf941be758b41ee0f730e84a60c742ee9 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 14 Nov 2024 16:11:00 +0700 Subject: [PATCH] refactor: make whitelist remove by index like in the usage --- .../commands/WhitelistCommand.java | 20 +++++++++---------- .../chomens_bot/plugins/WhitelistPlugin.java | 8 +------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java index 5a291bf..82fcf63 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java @@ -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")); + final String player = bot.whitelist.remove(index); - if (player.equals(bot.profile.getName())) throw new CommandException(Component.text("Cannot remove the bot")); - - bot.whitelist.remove(player); - - 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)); + 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); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/WhitelistPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/WhitelistPlugin.java index c89d09f..afc8f7e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/WhitelistPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/WhitelistPlugin.java @@ -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())); }