From b7ff56e28d62391a1cc1e71bcab491aae6b6c80d Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 27 Aug 2023 10:12:19 +0700 Subject: [PATCH] console command --- .../chomens_bot/commands/ConsoleCommand.java | 81 +++++++++++++++++++ .../commands/ConsoleServerCommand.java | 59 -------------- .../plugins/CommandHandlerPlugin.java | 2 +- 3 files changed, 82 insertions(+), 60 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java delete mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleServerCommand.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java new file mode 100644 index 0000000..f3b8d0c --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java @@ -0,0 +1,81 @@ +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.TrustLevel; +import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ConsoleCommand extends Command { + public ConsoleCommand() { + super( + "console", + "Controls stuff about console", + new String[] { + " server <{server}>", + " logtoconsole " + }, + new String[] {}, + TrustLevel.OWNER, + true + ); + } + + @Override + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot; + + if (args.length < 2) return Component.text("Not enough arguments").color(NamedTextColor.RED); + + switch (args[0]) { + case "server" -> { + final List servers = new ArrayList<>(); + + for (Bot eachBot : bot.bots) { + servers.add(eachBot.host + ":" + eachBot.port); + } + + for (Bot eachBot : bot.bots) { + final String server = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + + if (server.equalsIgnoreCase("all")) { + eachBot.console.consoleServer = "all"; + + context.sendOutput(Component.text("Set the console server to all servers").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); + + continue; + } + + try { + // servers.find(server => server.toLowerCase().includes(args.join(' '))) in js i guess + eachBot.console.consoleServer = servers.stream() + .filter(eachServer -> eachServer.toLowerCase().contains(server)) + .toArray(String[]::new)[0]; + + context.sendOutput(Component.text("Set the console server to " + bot.console.consoleServer).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); + } catch (ArrayIndexOutOfBoundsException e) { + return Component.text("Invalid server: " + String.join(" ", args)).color(NamedTextColor.RED); + } + } + } + case "logtoconsole" -> { + final boolean bool = Boolean.parseBoolean(args[1]); + + bot.logger.logToConsole = bool; + + return Component.translatable( + "Logging to console is now %s", + bool ? Component.text("enabled").color(NamedTextColor.GREEN) : Component.text("disabled").color(NamedTextColor.RED) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + } + + return null; + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleServerCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleServerCommand.java deleted file mode 100644 index adb6cfa..0000000 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleServerCommand.java +++ /dev/null @@ -1,59 +0,0 @@ -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.TrustLevel; -import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; - -import java.util.ArrayList; -import java.util.List; - -public class ConsoleServerCommand extends Command { - public ConsoleServerCommand () { - super( - "consoleserver", - "Changes the console server", - new String[] { " <{server}>" }, - new String[] { "csvr" }, - TrustLevel.OWNER, - true - ); - } - - @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - final Bot bot = context.bot; - - final List servers = new ArrayList<>(); - - for (Bot eachBot : bot.bots) { - servers.add(eachBot.host + ":" + eachBot.port); - } - - for (Bot eachBot : bot.bots) { - if (String.join(" ", args).equalsIgnoreCase("all")) { - eachBot.console.consoleServer = "all"; - - context.sendOutput(Component.text("Set the console server to all servers").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); - - continue; - } - - try { - // servers.find(server => server.toLowerCase().includes(args.join(' '))) in js i guess - eachBot.console.consoleServer = servers.stream() - .filter(server -> server.toLowerCase().contains(String.join(" ", args))) - .toArray(String[]::new)[0]; - - context.sendOutput(Component.text("Set the console server to " + String.join(", ", bot.console.consoleServer)).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); - } catch (ArrayIndexOutOfBoundsException e) { - return Component.text("Invalid server: " + String.join(" ", args)).color(NamedTextColor.RED); - } - } - - return null; - } -} 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 25d78a8..f5de2e8 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 @@ -51,7 +51,7 @@ public class CommandHandlerPlugin { registerCommand(new MailCommand()); registerCommand(new EvalCommand()); registerCommand(new InfoCommand()); - registerCommand(new ConsoleServerCommand()); + registerCommand(new ConsoleCommand()); registerCommand(new PCrashCommand()); }