From dd7806d0f975d471721bc77bd6d318c4b211737e Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 25 Sep 2023 18:15:44 +0700 Subject: [PATCH] whitelist --- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../commands/WhitelistCommand.java | 103 ++++++++++++++++++ .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/FilterPlugin.java | 31 +++--- .../chomens_bot/plugins/WhitelistPlugin.java | 79 ++++++++++++++ 5 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WhitelistPlugin.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index eed5ba6..b343f03 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -79,6 +79,7 @@ public class Bot { public AuthPlugin auth; public ScreensharePlugin screenshare; public FormatCheckerPlugin formatChecker; + public WhitelistPlugin whitelist; public Bot (Configuration.BotOption botOption, List bots, Configuration config) { this.host = botOption.host; @@ -129,6 +130,7 @@ public class Bot { this.auth = new AuthPlugin(this); // this.screenshare = new ScreensharePlugin(this); this.formatChecker = new FormatCheckerPlugin(this); + this.whitelist = new WhitelistPlugin(this); for (Listener listener : listeners) listener.loadedPlugins(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java new file mode 100644 index 0000000..ef0dbc4 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java @@ -0,0 +1,103 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import land.chipmunk.chayapak.chomens_bot.command.CommandException; +import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; +import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class WhitelistCommand extends Command { + public WhitelistCommand () { + super( + "whitelist", + "Manages whitelist", + new String[] { " enable", " disable", " add ", " remove ", " clear", " list" }, + new String[] {}, + TrustLevel.OWNER, + false + ); + } + + @Override + public Component execute(CommandContext context) throws CommandException { + final Bot bot = context.bot; + + final String action = context.getString(false, true); + + switch (action) { + case "enable" -> { + bot.whitelist.enable(); + + return Component.text("Enabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "disable" -> { + bot.whitelist.disable(); + + return Component.text("Disabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "add" -> { + final String player = context.getString(true, true); + + bot.whitelist.add(player); + + return Component.translatable( + "Added %s to the whitelist", + Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "remove" -> { + final String player = context.getString(true, 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); + + 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)); + } + case "clear" -> { + bot.whitelist.clear(); + + return Component.text("Cleared the whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "list" -> { + final List playersComponent = new ArrayList<>(); + + int index = 0; + for (String player : bot.whitelist.list) { + playersComponent.add( + Component.translatable( + "%s › %s", + Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(NamedTextColor.DARK_GRAY) + ); + + index++; + } + + return Component.empty() + .append(Component.text("Whitelisted players ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(bot.whitelist.list.size()).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append( + Component.join(JoinConfiguration.newlines(), playersComponent) + ); + } + default -> throw new CommandException(Component.text("Invalid action")); + } + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index 068c7ee..6a275e8 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -53,6 +53,7 @@ public class CommandHandlerPlugin { registerCommand(new ConsoleCommand()); registerCommand(new PCrashCommand()); // registerCommand(new ScreenshareCommand()); + registerCommand(new WhitelistCommand()); } public boolean disabled = false; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java index 2d4c6a4..f1cb9ec 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java @@ -44,7 +44,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { bot.commandSpy.addListener(new CommandSpyPlugin.Listener() { @Override public void commandReceived(PlayerEntry sender, String command) { - FilterPlugin.this.commandSpyMessageReceived(sender, command); + FilterPlugin.this.commandSpyMessageReceived(sender); } }); @@ -100,21 +100,15 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (player == null) return; - deOp(target); - mute(target); - gamemode(target); - - bot.exploits.kick(target.profile.getId()); + doAll(target); } - public void commandSpyMessageReceived (PlayerEntry sender, String command) { + public void commandSpyMessageReceived (PlayerEntry sender) { final FilteredPlayer player = getPlayer(sender.profile.getName()); if (player == null) return; - deOp(sender); - gamemode(sender); - mute(sender); + doAll(sender); } public void playerMessageReceived (PlayerMessage message) { @@ -124,9 +118,14 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (player == null || message.sender.profile.getId().equals(new UUID(0L, 0L))) return; - deOp(message.sender); - mute(message.sender); - gamemode(message.sender); + doAll(message.sender); + } + + public void doAll (PlayerEntry entry) { + mute(entry); + deOp(entry); + gameMode(entry); + bot.exploits.kick(entry.profile.getId()); } public void mute (PlayerEntry target) { mute(target, ""); } @@ -138,7 +137,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId())); } - public void gamemode (PlayerEntry target) { + public void gameMode(PlayerEntry target) { bot.core.run("minecraft:gamemode adventure " + UUIDUtilities.selector(target.profile.getId())); } @@ -161,9 +160,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (target == null) return; - deOp(target); - mute(target); - gamemode(target); + doAll(target); } public FilteredPlayer remove (int index) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WhitelistPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WhitelistPlugin.java new file mode 100644 index 0000000..689c1c0 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/WhitelistPlugin.java @@ -0,0 +1,79 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; +import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage; + +import java.util.ArrayList; +import java.util.List; + +public class WhitelistPlugin extends PlayersPlugin.Listener { + private final Bot bot; + + public final List list = new ArrayList<>(); + + private boolean enabled = false; + + public WhitelistPlugin (Bot bot) { + this.bot = bot; + + bot.players.addListener(this); + + bot.chat.addListener(new ChatPlugin.Listener() { + @Override + public void playerMessageReceived(PlayerMessage message) { + WhitelistPlugin.this.playerMessageReceived(message); + } + }); + + bot.commandSpy.addListener(new CommandSpyPlugin.Listener() { + @Override + public void commandReceived(PlayerEntry sender, String command) { + WhitelistPlugin.this.commandReceived(sender); + } + }); + } + + public void enable () { + enabled = true; + + for (PlayerEntry entry : bot.players.list) { + if (list.contains(entry.profile.getName())) continue; + + list.add(entry.profile.getName()); + } + } + + public void disable () { + enabled = false; + } + + public void add (String player) { list.add(player); } + public void remove (String player) { + list.removeIf(eachPlayer -> eachPlayer.equals(player)); + + handle(bot.players.getEntry(player)); + } + public void clear () { + list.removeIf(eachPlayer -> !eachPlayer.equals(bot.profile.getName())); + } + + @Override + public void playerJoined(PlayerEntry target) { + handle(target); + } + + public void playerMessageReceived (PlayerMessage message) { + handle(message.sender); + } + + public void commandReceived (PlayerEntry sender) { + handle(sender); + } + + private void handle (PlayerEntry entry) { + if (!enabled || list.contains(entry.profile.getName())) return; + + bot.filter.doAll(entry); + } +}