From 824697879df9e3507538ab2f1aed0e64b0e86b03 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 18 Sep 2023 20:22:47 +0700 Subject: [PATCH] ***VERY EXPERIMENTAL*** args parser --- .../chayapak/chomens_bot/command/Command.java | 2 +- .../chomens_bot/command/CommandContext.java | 83 ++++- .../chomens_bot/command/CommandException.java | 11 + .../commands/BotVisibilityCommand.java | 11 +- .../chomens_bot/commands/BruhifyCommand.java | 11 +- .../commands/ClearChatCommand.java | 15 +- .../commands/ClearChatQueueCommand.java | 2 +- .../chomens_bot/commands/CloopCommand.java | 30 +- .../commands/CommandBlockCommand.java | 7 +- .../chomens_bot/commands/ConsoleCommand.java | 18 +- .../chomens_bot/commands/CowsayCommand.java | 7 +- .../chomens_bot/commands/DiscordCommand.java | 2 +- .../chomens_bot/commands/EchoCommand.java | 7 +- .../chomens_bot/commands/EndCommand.java | 2 +- .../chomens_bot/commands/EvalCommand.java | 18 +- .../chomens_bot/commands/FilterCommand.java | 41 +-- .../chomens_bot/commands/HelpCommand.java | 27 +- .../chomens_bot/commands/InfoCommand.java | 9 +- .../chomens_bot/commands/KickCommand.java | 9 +- .../chomens_bot/commands/ListCommand.java | 6 +- .../chomens_bot/commands/MailCommand.java | 338 +++++++++--------- .../chomens_bot/commands/MusicCommand.java | 159 ++++---- .../commands/NetMessageCommand.java | 7 +- .../chomens_bot/commands/PCrashCommand.java | 11 +- .../commands/RandomTeleportCommand.java | 5 +- .../commands/RefillCoreCommand.java | 2 +- .../commands/ScreenshareCommand.java | 6 +- .../commands/ServerEvalCommand.java | 9 +- .../chomens_bot/commands/TPSBarCommand.java | 9 +- .../chomens_bot/commands/TestCommand.java | 7 +- .../chomens_bot/commands/TimeCommand.java | 7 +- .../commands/TranslateCommand.java | 12 +- .../chomens_bot/commands/UUIDCommand.java | 13 +- .../chomens_bot/commands/UrbanCommand.java | 13 +- .../chomens_bot/commands/ValidateCommand.java | 9 +- .../chomens_bot/commands/WeatherCommand.java | 9 +- .../commands/WikipediaCommand.java | 7 +- .../plugins/ChatCommandHandlerPlugin.java | 12 +- .../plugins/CommandHandlerPlugin.java | 88 +---- 39 files changed, 533 insertions(+), 508 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandException.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/command/Command.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/Command.java index 28dad88..007ed8a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/command/Command.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/Command.java @@ -26,5 +26,5 @@ public abstract class Command { this.consoleOnly = consoleOnly; } - public abstract Component execute (CommandContext context, String[] args, String[] fullArgs) throws Exception; + public abstract Component execute (CommandContext context) throws Exception; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandContext.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandContext.java index f76ce58..397b4d8 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandContext.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandContext.java @@ -4,6 +4,8 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; import net.kyori.adventure.text.Component; +import java.util.Arrays; + public class CommandContext { public final Bot bot; @@ -15,7 +17,8 @@ public class CommandContext { public String commandName = null; - public String[] splitInput; + public String[] fullArgs; + public String[] args; public CommandContext(Bot bot, String prefix, PlayerEntry sender, boolean inGame) { this.bot = bot; @@ -26,4 +29,82 @@ public class CommandContext { public Component displayName () { return Component.empty(); } public void sendOutput (Component component) {} + + private int argsPosition = 0; + + public String getString (boolean greedy, boolean required) throws CommandException { return getString(greedy, required, "string"); } + private String getString (boolean greedy, boolean required, String type) throws CommandException { + if (args.length == 0 || args[Math.min(argsPosition, args.length - 1)] == null) { + if (required) { + throw new CommandException( + Component.translatable( + "Expected %s", + Component.text(type) + ) + ); + } else { + return ""; + } + } + + return greedy ? String.join(" ", Arrays.copyOfRange(args, argsPosition++, args.length)) : args[Math.min(argsPosition++, args.length - 1)]; + } + + public Integer getInteger (boolean required) throws CommandException { + final String string = getString(false, required, "integer"); + + if (string.isEmpty()) return null; + + try { + return Integer.parseInt(string); + } catch (NumberFormatException e) { + throw new CommandException(Component.text("Invalid integer")); + } + } + + public Double getDouble (boolean required) throws CommandException { + final String string = getString(false, required, "double"); + + if (string.isEmpty()) return null; + + try { + return Double.parseDouble(string); + } catch (NumberFormatException e) { + throw new CommandException(Component.text("Invalid double")); + } + } + + public Float getFloat (boolean required) throws CommandException { + final String string = getString(false, required, "float"); + + if (string.isEmpty()) return null; + + try { + return Float.parseFloat(string); + } catch (NumberFormatException e) { + throw new CommandException(Component.text("Invalid float")); + } + } + + public Boolean getBoolean (boolean required) throws CommandException { + final String string = getString(false, required, "boolean"); + + if (string.isEmpty()) return null; + + return switch (string) { + case "true" -> true; + case "false" -> false; + default -> throw new CommandException(Component.text("Invalid boolean")); + }; + } + + public > T getEnum (Class enumClass) throws CommandException { + final String string = getString(false, true, enumClass.getSimpleName()); + + try { + return Enum.valueOf(enumClass, string.toUpperCase()); + } catch (IllegalArgumentException | NullPointerException e) { + throw new CommandException(Component.text("Invalid enum")); + } + } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandException.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandException.java new file mode 100644 index 0000000..b905705 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/command/CommandException.java @@ -0,0 +1,11 @@ +package land.chipmunk.chayapak.chomens_bot.command; + +import net.kyori.adventure.text.Component; + +public class CommandException extends Exception { + public final Component message; + + public CommandException (Component message) { + this.message = message; + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BotVisibilityCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BotVisibilityCommand.java index f18f7a1..129fe3d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BotVisibilityCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BotVisibilityCommand.java @@ -3,6 +3,7 @@ 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; @@ -21,10 +22,12 @@ public class BotVisibilityCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length == 0) { + final String action = context.getString(false, false); + + if (action.isEmpty()) { final boolean visibility = bot.selfCare.visibility; bot.selfCare.visibility = !visibility; @@ -37,7 +40,7 @@ public class BotVisibilityCommand extends Command { .append(Component.text(visibleOrInvisible).color(greenOrGold)) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } else { - switch (args[0]) { + switch (action) { case "on", "true" -> { bot.selfCare.visibility = true; bot.chat.send("/essentials:vanish disable"); @@ -55,7 +58,7 @@ public class BotVisibilityCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BruhifyCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BruhifyCommand.java index cb22275..1324308 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BruhifyCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/BruhifyCommand.java @@ -3,6 +3,7 @@ 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 net.kyori.adventure.text.Component; @@ -11,7 +12,7 @@ public class BruhifyCommand extends Command { super( "bruhify", "RecycleBots bruhify but actionbar", - new String[] { "[{message}]" }, + new String[] { "[message]" }, new String[] {}, TrustLevel.PUBLIC, false @@ -19,14 +20,10 @@ public class BruhifyCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length == 0) { - bot.bruhify.bruhifyText = ""; - } else { - bot.bruhify.bruhifyText = String.join(" ", args); - } + bot.bruhify.bruhifyText = context.getString(true, false); return null; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatCommand.java index 23da7b6..d0e04c2 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatCommand.java @@ -1,10 +1,11 @@ package land.chipmunk.chayapak.chomens_bot.commands; import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; -import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; 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.data.chat.PlayerEntry; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -23,13 +24,15 @@ public class ClearChatCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length > 0) { - final PlayerEntry entry = bot.players.getEntry(String.join(" ", args)); + final String name = context.getString(true, false); - if (entry == null) return Component.text("Invalid player name").color(NamedTextColor.RED); + if (name.isEmpty()) { + final PlayerEntry entry = bot.players.getEntry(name); + + if (entry == null) throw new CommandException(Component.text("Invalid player name")); final UUID uuid = entry.profile.getId(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java index e3bd65d..73d2b83 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ClearChatQueueCommand.java @@ -19,7 +19,7 @@ public class ClearChatQueueCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) { final Bot bot = context.bot; bot.chat.clearQueue(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java index cd252d6..8a761b3 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java @@ -3,6 +3,7 @@ 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.data.CommandLoop; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -11,7 +12,6 @@ import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class CloopCommand extends Command { @@ -19,7 +19,7 @@ public class CloopCommand extends Command { super( "cloop", "Loop commands", - new String[] { " add <{command}>", " remove ", " clear", " list" }, + new String[] { " add ", " remove ", " clear", " list" }, new String[] { "commandloop" }, TrustLevel.TRUSTED, false @@ -27,21 +27,17 @@ public class CloopCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - switch (args[0]) { - case "add" -> { - if (args.length < 3) return Component.text("Please specify interval and command").color(NamedTextColor.RED); - int interval; - try { - interval = Integer.parseInt(args[1]); - if (interval < 1) interval = 1; - } catch (IllegalArgumentException ignored) { - return Component.text("Invalid interval").color(NamedTextColor.RED); - } + final String action = context.getString(false, true); - final String command = String.join(" ", Arrays.copyOfRange(args, 2, args.length)); + switch (action) { + case "add" -> { + int interval = context.getInteger(true); + if (interval < 1) interval = 1; + + final String command = context.getString(true, true); bot.cloop.add(interval, command); @@ -53,7 +49,7 @@ public class CloopCommand extends Command { } case "remove" -> { try { - final int index = Integer.parseInt(args[1]); + final int index = context.getInteger(true); bot.cloop.remove(index); return Component.translatable( @@ -61,7 +57,7 @@ public class CloopCommand extends Command { Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) { - return Component.text("Invalid index").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid index")); } } case "clear" -> { @@ -95,7 +91,7 @@ public class CloopCommand extends Command { ); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java index c482ca6..1504b49 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java @@ -5,6 +5,7 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag; 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; @@ -18,7 +19,7 @@ public class CommandBlockCommand extends Command { super( "cb", "Executes a command in the command core and return its output", - new String[] { "<{command}>" }, + new String[] { "" }, new String[] { "cmd", "commandblock", "run" }, TrustLevel.PUBLIC, false @@ -26,10 +27,10 @@ public class CommandBlockCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final CompletableFuture future = bot.core.runTracked(String.join(" ", args)); + final CompletableFuture future = bot.core.runTracked(context.getString(true, true)); if (future == null) return null; 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 index b6a4d2f..567fedf 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ConsoleCommand.java @@ -3,13 +3,13 @@ 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.format.NamedTextColor; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class ConsoleCommand extends Command { @@ -18,7 +18,7 @@ public class ConsoleCommand extends Command { "console", "Controls stuff about console", new String[] { - " server <{server}>", + " server ", " logtoconsole " }, new String[] {}, @@ -28,12 +28,12 @@ public class ConsoleCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length < 2) return Component.text("Not enough arguments").color(NamedTextColor.RED); + final String action = context.getString(false, true); - switch (args[0]) { + switch (action) { case "server" -> { final List servers = new ArrayList<>(); @@ -41,9 +41,9 @@ public class ConsoleCommand extends Command { servers.add(eachBot.host + ":" + eachBot.port); } - for (Bot eachBot : bot.bots) { - final String server = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + final String server = context.getString(true, true); + for (Bot eachBot : bot.bots) { if (server.equalsIgnoreCase("all")) { eachBot.console.consoleServer = "all"; @@ -60,12 +60,12 @@ public class ConsoleCommand extends Command { 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: " + server).color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid server: " + server)); } } } case "logtoconsole" -> { - final boolean bool = Boolean.parseBoolean(args[1]); + final boolean bool = context.getBoolean(true); bot.logger.logToConsole = bool; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CowsayCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CowsayCommand.java index c0eb1a5..18cc18d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CowsayCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CowsayCommand.java @@ -3,6 +3,7 @@ package land.chipmunk.chayapak.chomens_bot.commands; import com.github.ricksbrown.cowsay.plugin.CowExecutor; 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 net.kyori.adventure.text.Component; @@ -11,7 +12,7 @@ public class CowsayCommand extends Command { super( "cowsay", "Moo", - new String[] { "<{message}>" }, + new String[] { "" }, new String[] {}, TrustLevel.PUBLIC, false @@ -19,8 +20,8 @@ public class CowsayCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - final String message = String.join(" ", args); + public Component execute(CommandContext context) throws CommandException { + final String message = context.getString(true, true); final CowExecutor cowExecutor = new CowExecutor(); cowExecutor.setMessage(message); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/DiscordCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/DiscordCommand.java index aa984f6..a8b45fe 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/DiscordCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/DiscordCommand.java @@ -22,7 +22,7 @@ public class DiscordCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) { final Bot bot = context.bot; final String link = bot.config.discord.inviteLink; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EchoCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EchoCommand.java index df1c77b..e56106a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EchoCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EchoCommand.java @@ -3,6 +3,7 @@ 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 net.kyori.adventure.text.Component; @@ -11,7 +12,7 @@ public class EchoCommand extends Command { super( "echo", "Makes the bot say a message", - new String[] { "<{message}>" }, + new String[] { "" }, new String[] { "say" }, TrustLevel.PUBLIC, false @@ -19,10 +20,10 @@ public class EchoCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - bot.chat.send(String.join(" ", args)); + bot.chat.send(context.getString(true, true)); return null; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java index 15a9e23..de1ec42 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java @@ -19,7 +19,7 @@ public class EndCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) { final Bot bot = context.bot; bot.session.disconnect("End command"); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java index 52f1978..70f8dc9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java @@ -3,13 +3,13 @@ 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.data.EvalOutput; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import java.util.Arrays; import java.util.concurrent.CompletableFuture; public class EvalCommand extends Command { @@ -17,7 +17,7 @@ public class EvalCommand extends Command { super( "eval", "Evaluate JavaScript codes", - new String[] { "run <{code}>", "reset" }, + new String[] { "run ", "reset" }, new String[] {}, TrustLevel.PUBLIC, false @@ -25,16 +25,16 @@ public class EvalCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); - + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (!bot.eval.connected) return Component.text("Eval server is not online").color(NamedTextColor.RED); + if (!bot.eval.connected) throw new CommandException(Component.text("Eval server is not online")); - switch (args[0]) { + final String action = context.getString(false, true); + + switch (action) { case "run" -> { - final String command = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + final String command = context.getString(true, true); final CompletableFuture future = bot.eval.run(command); @@ -51,7 +51,7 @@ public class EvalCommand extends Command { return Component.text("Reset the eval worker").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java index af8d957..08e55ab 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/FilterCommand.java @@ -5,6 +5,7 @@ import com.google.gson.JsonElement; 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.data.FilteredPlayer; import land.chipmunk.chayapak.chomens_bot.plugins.FilterPlugin; @@ -14,7 +15,6 @@ import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class FilterCommand extends Command { @@ -23,10 +23,10 @@ public class FilterCommand extends Command { "filter", "Filter players", new String[] { - " add <{player}>", - " -ignorecase add <{player}>", - " -regex add <{player}>", - " -ignorecase -regex add <{player}>", + " add ", + " -ignorecase add ", + " -regex add ", + " -ignorecase -regex add ", " remove ", " clear", " list" @@ -38,36 +38,37 @@ public class FilterCommand extends Command { } // most of these codes are from cloop and greplog - public Component execute(CommandContext context, String[] _args, String[] fullArgs) { + @Override + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; boolean ignoreCase = false; boolean regex = false; - String[] args = _args; + String action = context.getString(false, true); // this is a mess - if (_args[0].equals("-ignorecase")) { + if (action.equals("-ignorecase")) { ignoreCase = true; - args = Arrays.copyOfRange(_args, 1, _args.length); - } else if (_args[0].equals("-regex")) { + action = context.getString(false, true); + } else if (action.equals("-regex")) { regex = true; - args = Arrays.copyOfRange(_args, 1, _args.length); + action = context.getString(false, true); } - if (_args.length > 1 && _args[1].equals("-ignorecase")) { + if (action.equals("-ignorecase")) { ignoreCase = true; - args = Arrays.copyOfRange(_args, 2, _args.length); - } else if (_args.length > 1 && _args[1].equals("-regex")) { + action = context.getString(false, true); + } else if (action.equals("-regex")) { regex = true; - args = Arrays.copyOfRange(_args, 2, _args.length); + action = context.getString(false, true); } final Gson gson = new Gson(); - switch (args[0]) { + switch (action) { case "add" -> { - final String player = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + final String player = context.getString(true, true); bot.filter.add(player, regex, ignoreCase); return Component.translatable( @@ -77,7 +78,7 @@ public class FilterCommand extends Command { } case "remove" -> { try { - final int index = Integer.parseInt(args[1]); + final int index = context.getInteger(true); final FilteredPlayer removed = bot.filter.remove(index); @@ -86,7 +87,7 @@ public class FilterCommand extends Command { Component.text(removed.playerName).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) { - return Component.text("Invalid index").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid index")); } } case "clear" -> { @@ -122,7 +123,7 @@ public class FilterCommand extends Command { ); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/HelpCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/HelpCommand.java index 2d74079..44fea10 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/HelpCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/HelpCommand.java @@ -3,6 +3,7 @@ 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.plugins.CommandHandlerPlugin; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -31,17 +32,19 @@ public class HelpCommand extends Command { private CommandContext context; @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { this.context = context; - if (args.length == 0) { + final String commandName = context.getString(true, false); + + if (commandName.isEmpty()) { return sendCommandList(); } else { - return sendUsages(context, args); + return sendUsages(context, commandName); } } - public Component sendCommandList () { + public Component sendCommandList () throws CommandException { final List list = new ArrayList<>(); list.addAll(getCommandListByTrustLevel(TrustLevel.PUBLIC)); list.addAll(getCommandListByTrustLevel(TrustLevel.TRUSTED)); @@ -60,7 +63,7 @@ public class HelpCommand extends Command { .append(Component.join(JoinConfiguration.separator(Component.space()), list)); } - public List getCommandListByTrustLevel(TrustLevel trustLevel) { + public List getCommandListByTrustLevel(TrustLevel trustLevel) throws CommandException { final List list = new ArrayList<>(); List commandNames = new ArrayList<>(); @@ -80,7 +83,7 @@ public class HelpCommand extends Command { .color(getColorByTrustLevel(trustLevel)) .hoverEvent( HoverEvent.showText( - sendUsages(context, new String[] { name }) + sendUsages(context, name) ) ) ); @@ -97,20 +100,20 @@ public class HelpCommand extends Command { }; } - public Component sendUsages (CommandContext context, String[] args) { + public Component sendUsages (CommandContext context, String commandName) throws CommandException { final Bot bot = context.bot; final String prefix = context.prefix; for (Command command : CommandHandlerPlugin.commands) { - if (!command.name.equals(args[0]) && !Arrays.stream(command.aliases).toList().contains(args[0])) continue; + if (!command.name.equals(commandName) && !Arrays.stream(command.aliases).toList().contains(commandName)) continue; - final String commandName = command.name; + final String actualCommandName = command.name; final List usages = new ArrayList<>(); usages.add( Component.empty() - .append(Component.text(prefix + commandName).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .append(Component.text(prefix + actualCommandName).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) .append(Component.text( (command.aliases.length > 0 && !command.aliases[0].equals("")) ? " (" + String.join(", ", command.aliases) + ")" : @@ -128,7 +131,7 @@ public class HelpCommand extends Command { for (String usage : command.usages) { usages.add( Component.empty() - .append(Component.text(prefix + commandName).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .append(Component.text(prefix + actualCommandName).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) .append(Component.text(" ")) .append(Component.text(usage).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))) ); @@ -137,6 +140,6 @@ public class HelpCommand extends Command { return Component.join(JoinConfiguration.separator(Component.newline()), usages); } - return Component.text("Unknown command").color(NamedTextColor.RED); + throw new CommandException(Component.text("Unknown command")); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java index e8e20e7..ea48981 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java @@ -3,6 +3,7 @@ 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; @@ -43,10 +44,12 @@ public class InfoCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - switch (args[0]) { + final String action = context.getString(false, true); + + switch (action) { case "creator" -> { return Component.empty() .append(Component.text("ChomeNS Bot ").color(ColorUtilities.getColorByString(bot.config.colorPalette.primary))) @@ -194,7 +197,7 @@ public class InfoCommand extends Command { ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/KickCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/KickCommand.java index 5103f78..834f169 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/KickCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/KickCommand.java @@ -3,6 +3,7 @@ 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.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -16,7 +17,7 @@ public class KickCommand extends Command { super( "kick", "Kicks a player", - new String[] { " <{player}>" }, + new String[] { " " }, new String[] {}, TrustLevel.TRUSTED, false @@ -24,12 +25,12 @@ public class KickCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final PlayerEntry entry = bot.players.getEntry(String.join(" ", args)); + final PlayerEntry entry = bot.players.getEntry(context.getString(true, true)); - if (entry == null) return Component.text("Invalid player name").color(NamedTextColor.RED); + if (entry == null) throw new CommandException(Component.text("Invalid player name")); final String name = entry.profile.getName(); final UUID uuid = entry.profile.getId(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ListCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ListCommand.java index 135237d..764fa1a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ListCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ListCommand.java @@ -1,10 +1,10 @@ package land.chipmunk.chayapak.chomens_bot.commands; import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; -import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; 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.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; @@ -28,7 +28,7 @@ public class ListCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) { final Bot bot = context.bot; final List list = bot.players.list; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MailCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MailCommand.java index bc10b9f..aeaae8c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MailCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MailCommand.java @@ -7,6 +7,7 @@ import com.google.gson.JsonElement; 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.data.Mail; import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; @@ -26,7 +27,6 @@ import org.joda.time.format.DateTimeFormatter; import java.time.Instant; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -35,7 +35,7 @@ public class MailCommand extends Command { super( "mail", "Sends a mail", - new String[] { "send <{message}>", "sendselecteditem ", "read" }, + new String[] { "send ", "sendselecteditem ", "read" }, new String[] {}, TrustLevel.PUBLIC, false @@ -43,9 +43,7 @@ public class MailCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); - + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; final PlayerEntry sender = context.sender; @@ -54,195 +52,191 @@ public class MailCommand extends Command { // kinda messy ngl - bot.executorService.submit(() -> { - switch (args[0]) { - case "send" -> { - int senderMailsSentTotal = 0; - for (JsonElement mailElement : MailPlugin.mails) { - final Mail mail = gson.fromJson(mailElement, Mail.class); + final String action = context.getString(false, true); - if (mail.sentBy == null) continue; + switch (action) { + case "send" -> { + int senderMailsSentTotal = 0; + for (JsonElement mailElement : MailPlugin.mails) { + final Mail mail = gson.fromJson(mailElement, Mail.class); - if (!mail.sentBy.equals(sender.profile.getName())) continue; - senderMailsSentTotal++; - } + if (mail.sentBy == null) continue; - if (senderMailsSentTotal > 256) { - context.sendOutput(Component.text("You are sending too many mails!").color(NamedTextColor.RED)); - - return; - } - - bot.mail.send( - new Mail( - sender.profile.getName(), - args[1], - Instant.now().toEpochMilli(), - bot.host + ":" + bot.port, - String.join(" ", Arrays.copyOfRange(args, 2, args.length)) - ) - ); - - context.sendOutput(Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); + if (!mail.sentBy.equals(sender.profile.getName())) continue; + senderMailsSentTotal++; } - case "sendselecteditem" -> { - int senderMailsSentTotal = 0; - for (JsonElement mailElement : MailPlugin.mails) { - final Mail mail = gson.fromJson(mailElement, Mail.class); - if (!mail.sentTo.equals(sender.profile.getName())) continue; - senderMailsSentTotal++; + if (senderMailsSentTotal > 256) { + throw new CommandException(Component.text("You are sending too many mails!")); + } + + bot.mail.send( + new Mail( + sender.profile.getName(), + context.getString(true, true), + Instant.now().toEpochMilli(), + bot.host + ":" + bot.port, + context.getString(true, true) + ) + ); + + return Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "sendselecteditem" -> { + int senderMailsSentTotal = 0; + for (JsonElement mailElement : MailPlugin.mails) { + final Mail mail = gson.fromJson(mailElement, Mail.class); + + if (!mail.sentTo.equals(sender.profile.getName())) continue; + senderMailsSentTotal++; + } + + if (senderMailsSentTotal > 256) { + throw new CommandException(Component.text("You are sending too many mails!")); + } + + final CompletableFuture future = bot.core.runTracked( + "minecraft:data get entity " + + UUIDUtilities.selector(sender.profile.getId()) + + " SelectedItem.tag.message" + ); + + if (future == null) { + throw new CommandException(Component.text("There was an error while sending your mail")); + } + + future.thenApply(tags -> { + if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags; + + final StringTag lastOutput = tags.get("LastOutput"); + + final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue()); + + final List children = output.children(); + + if ( + children.size() > 0 && + children.get(0).children().size() > 0 && + ((TranslatableComponent) children.get(0).children().get(0)) + .key() + .equals("arguments.nbtpath.nothing_found") + ) { + context.sendOutput(Component.text("Player has no `message` NBT tag in the selected item").color(NamedTextColor.RED)); + return tags; } - if (senderMailsSentTotal > 256) { - context.sendOutput(Component.text("You are sending too many mails!").color(NamedTextColor.RED)); + final String value = ComponentUtilities.stringify(((TranslatableComponent) children.get(0)).args().get(1)); - return; + if (!value.startsWith("\"") && !value.endsWith("\"") && !value.startsWith("'") && !value.endsWith("'")) { + context.sendOutput(Component.text("`message` NBT is not a string").color(NamedTextColor.RED)); + return tags; } - final CompletableFuture future = bot.core.runTracked( - "minecraft:data get entity " + - UUIDUtilities.selector(sender.profile.getId()) + - " SelectedItem.tag.message" - ); - - if (future == null) { - context.sendOutput(Component.text("There was an error while sending your mail").color(NamedTextColor.RED)); - - return; - } - - future.thenApply(tags -> { - if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags; - - final StringTag lastOutput = tags.get("LastOutput"); - - final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue()); - - final List children = output.children(); - - if ( - children.size() > 0 && - children.get(0).children().size() > 0 && - ((TranslatableComponent) children.get(0).children().get(0)) - .key() - .equals("arguments.nbtpath.nothing_found") - ) { - context.sendOutput(Component.text("Player has no `message` NBT tag in the selected item").color(NamedTextColor.RED)); - return tags; - } - - final String value = ComponentUtilities.stringify(((TranslatableComponent) children.get(0)).args().get(1)); - - if (!value.startsWith("\"") && !value.endsWith("\"") && !value.startsWith("'") && !value.endsWith("'")) { - context.sendOutput(Component.text("`message` NBT is not a string").color(NamedTextColor.RED)); - return tags; - } - + try { bot.mail.send( new Mail( sender.profile.getName(), - args[1], + context.getString(true, true), Instant.now().toEpochMilli(), bot.host + ":" + bot.port, value.substring(1).substring(0, value.length() - 2) ) ); - - context.sendOutput( - Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) - ); - - return tags; - }); - } - case "read" -> { - // TODO: use less for loops? - - int senderMailSize = 0; - for (JsonElement mailElement : MailPlugin.mails) { - final Mail mail = gson.fromJson(mailElement, Mail.class); - - if (!mail.sentTo.equals(sender.profile.getName())) continue; - senderMailSize++; + } catch (CommandException e) { + context.sendOutput(e.message.color(NamedTextColor.RED)); } - if (senderMailSize == 0) { - context.sendOutput(Component.text("You have no new mails").color(NamedTextColor.RED)); + context.sendOutput( + Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) + ); - return; - } - - final List mailsComponent = new ArrayList<>(); - - int i = 1; - for (JsonElement mailElement : MailPlugin.mails) { - final Mail mail = gson.fromJson(mailElement, Mail.class); - - if (!mail.sentTo.equals(sender.profile.getName())) continue; - - final DateTimeFormatter formatter = DateTimeFormat.forPattern("MMMM d, YYYY, hh:mm:ss a Z"); - final String formattedTime = formatter.print(mail.timeSent); - - mailsComponent.add( - Component.translatable( - """ - %s %s Sent by: %s %s - Contents: - %s""", - Component.text(i).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), - Component.text("-").color(NamedTextColor.DARK_GRAY), - - Component.text(mail.sentBy).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)), - Component - .text("[Hover here for more info]") - .color(NamedTextColor.GREEN) - .hoverEvent( - HoverEvent.showText( - Component.translatable( - """ - Time sent: %s - Server: %s""", - Component.text(formattedTime).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)), - Component.text(mail.server).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) - ).color(NamedTextColor.GREEN) - ) - ), - Component.text(mail.contents).color(NamedTextColor.WHITE) - ).color(NamedTextColor.GREEN) - ); - - i++; - } - - final Component component = Component.empty() - .append(Component.text("Mails ").color(NamedTextColor.GREEN)) - .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) - .append(Component.text(senderMailSize).color(NamedTextColor.GRAY)) - .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) - .append(Component.newline()) - .append(Component.join(JoinConfiguration.newlines(), mailsComponent)); - - if (context.inGame) { - bot.chat.tellraw( - component, - context.sender.profile.getId() - ); - } else { - context.sendOutput(component); - } - - for (JsonElement mailElement : MailPlugin.mails.deepCopy()) { - final Mail mail = gson.fromJson(mailElement, Mail.class); - - if (mail.sentTo.equals(sender.profile.getName())) MailPlugin.mails.remove(mailElement); - } - - PersistentDataUtilities.put("mails", MailPlugin.mails); - } - default -> context.sendOutput(Component.text("Invalid action").color(NamedTextColor.RED)); + return tags; + }); } - }); + case "read" -> { + // TODO: use less for loops? + + int senderMailSize = 0; + for (JsonElement mailElement : MailPlugin.mails) { + final Mail mail = gson.fromJson(mailElement, Mail.class); + + if (!mail.sentTo.equals(sender.profile.getName())) continue; + senderMailSize++; + } + + if (senderMailSize == 0) { + throw new CommandException(Component.text("You have no new mails")); + } + + final List mailsComponent = new ArrayList<>(); + + int i = 1; + for (JsonElement mailElement : MailPlugin.mails) { + final Mail mail = gson.fromJson(mailElement, Mail.class); + + if (!mail.sentTo.equals(sender.profile.getName())) continue; + + final DateTimeFormatter formatter = DateTimeFormat.forPattern("MMMM d, YYYY, hh:mm:ss a Z"); + final String formattedTime = formatter.print(mail.timeSent); + + mailsComponent.add( + Component.translatable( + """ + %s %s Sent by: %s %s + Contents: + %s""", + Component.text(i).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component.text("-").color(NamedTextColor.DARK_GRAY), + + Component.text(mail.sentBy).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)), + Component + .text("[Hover here for more info]") + .color(NamedTextColor.GREEN) + .hoverEvent( + HoverEvent.showText( + Component.translatable( + """ + Time sent: %s + Server: %s""", + Component.text(formattedTime).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)), + Component.text(mail.server).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) + ).color(NamedTextColor.GREEN) + ) + ), + Component.text(mail.contents).color(NamedTextColor.WHITE) + ).color(NamedTextColor.GREEN) + ); + + i++; + } + + final Component component = Component.empty() + .append(Component.text("Mails ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(senderMailSize).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append(Component.join(JoinConfiguration.newlines(), mailsComponent)); + + if (context.inGame) { + bot.chat.tellraw( + component, + context.sender.profile.getId() + ); + } else { + context.sendOutput(component); + } + + for (JsonElement mailElement : MailPlugin.mails.deepCopy()) { + final Mail mail = gson.fromJson(mailElement, Mail.class); + + if (mail.sentTo.equals(sender.profile.getName())) MailPlugin.mails.remove(mailElement); + } + + PersistentDataUtilities.put("mails", MailPlugin.mails); + } + default -> context.sendOutput(Component.text("Invalid action").color(NamedTextColor.RED)); + } return null; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index 89b92dd..c1b8c41 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -4,6 +4,7 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Main; 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.plugins.MusicPlayerPlugin; import land.chipmunk.chayapak.chomens_bot.song.Instrument; @@ -37,10 +38,10 @@ public class MusicCommand extends Command { "music", "Plays music", new String[] { - "play <{song|URL}>", + "play ", "stop", "loop ", - "list [{directory}]", + "list [directory]", "skip", "nowplaying", "queue", @@ -61,26 +62,26 @@ public class MusicCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); - + public Component execute(CommandContext context) throws CommandException { ratelimit++; if (ratelimit > 10) return null; + final String action = context.getString(false, true); + root = MusicPlayerPlugin.SONG_DIR; - return switch (args[0]) { - case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args); + return switch (action) { + case "play", "playurl", "playnbs", "playnbsurl" -> play(context); case "stop" -> stop(context); - case "loop" -> loop(context, args); - case "list" -> list(context, args); + case "loop" -> loop(context); + case "list" -> list(context); case "skip" -> skip(context); case "nowplaying" -> nowplaying(context); case "queue" -> queue(context); - case "goto" -> goTo(context, args); - case "pitch" -> pitch(context, args); - case "speed" -> speed(context, args); - case "noteinstrument" -> noteInstrument(context, args); + case "goto" -> goTo(context); + case "pitch" -> pitch(context); + case "speed" -> speed(context); + case "noteinstrument" -> noteInstrument(context); case "pause", "resume" -> pause(context); case "info" -> info(context); case "testsong" -> testSong(context); @@ -88,22 +89,22 @@ public class MusicCommand extends Command { }; } - public Component play (CommandContext context, String[] args) { + public Component play (CommandContext context) throws CommandException { final MusicPlayerPlugin player = context.bot.music; String _path; Path path; try { - _path = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + _path = context.getString(true, true); - if (_path.isBlank()) return Component.text("No song specified").color(NamedTextColor.RED); +// if (_path.isBlank()) throw new CommandException(Component.text("No song specified")); path = Path.of(root.toString(), _path); if (path.toString().contains("http")) player.loadSong(new URL(_path)); else { // among us protection!!!11 - if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED); + if (!path.normalize().startsWith(root.toString())) throw new CommandException(Component.text("no")); // ignore my ohio code for autocomplete final String separator = File.separator; // how do i do this with the new Files? @@ -118,7 +119,7 @@ public class MusicCommand extends Command { final String[] songs = realPath.toFile().list(); - if (songs == null) return Component.text("Directory does not exist").color(NamedTextColor.RED); + if (songs == null) throw new CommandException(Component.text("Directory does not exist")); final String lowerCaseFile = pathSplitted[pathSplitted.length - 1].toLowerCase(); @@ -140,11 +141,11 @@ public class MusicCommand extends Command { } } } catch (MalformedURLException e) { - return Component.text("Invalid URL").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid URL")); } catch (IndexOutOfBoundsException e) { - return Component.text("Song not found").color(NamedTextColor.RED); + throw new CommandException(Component.text("Song not found")); } catch (Exception e) { - return Component.text(e.toString()).color(NamedTextColor.RED); + throw new CommandException(Component.text(e.toString())); } return null; @@ -158,58 +159,50 @@ public class MusicCommand extends Command { return Component.text("Cleared the song queue").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } - public Component loop (CommandContext context, String[] args) { + public Component loop (CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length < 2) return Component.text("Invalid loop").color(NamedTextColor.RED); - - Loop loop; - switch (args[1]) { - case "off" -> { - loop = Loop.OFF; - context.sendOutput( - Component.empty() - .append(Component.text("Looping is now ")) - .append(Component.text("disabled").color(NamedTextColor.RED)) - .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) - ); - } - case "current" -> { - loop = Loop.CURRENT; - context.sendOutput( - Component.empty() - .append(Component.text("Now looping ")) - .append(Component.text(bot.music.currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) - .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) - ); - } - case "all" -> { - loop = Loop.ALL; - context.sendOutput(Component.text("Now looping every song").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))); - } - default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); - } - } + final Loop loop = context.getEnum(Loop.class); bot.music.loop = loop; - return null; + switch (loop) { + case OFF -> { + return Component.empty() + .append(Component.text("Looping is now ")) + .append(Component.text("disabled").color(NamedTextColor.RED)) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case CURRENT -> { + return Component.empty() + .append(Component.text("Now looping ")) + .append(Component.text(bot.music.currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case ALL -> { + return Component.text("Now looping every song").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + default -> { + throw new CommandException(Component.text("Invalid action")); + } + } } - public Component list (CommandContext context, String[] args) { + public Component list (CommandContext context) throws CommandException { final Bot bot = context.bot; final String prefix = context.prefix; - final Path path = (args.length < 2) ? + final String stringPathIfExists = context.getString(true, false); + + final Path path = (stringPathIfExists.isEmpty()) ? root : Path.of( root.toString(), - String.join(" ", Arrays.copyOfRange(args, 1, args.length)) + stringPathIfExists ); - if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED); + if (!path.normalize().startsWith(root.toString())) throw new CommandException(Component.text("no")); try (DirectoryStream stream = Files.newDirectoryStream(path)) { final List paths = new ArrayList<>(); @@ -238,7 +231,7 @@ public class MusicCommand extends Command { location = Paths.get(""); // wtf mabe } - final String joinedPath = (args.length < 2) ? eachPath.getFileName().toString() : Paths.get(location.getFileName().toString(), eachPath.getFileName().toString()).toString(); + final String joinedPath = stringPathIfExists.isEmpty() ? eachPath.getFileName().toString() : Paths.get(location.getFileName().toString(), eachPath.getFileName().toString()).toString(); fullList.add( Component @@ -269,7 +262,7 @@ public class MusicCommand extends Command { list.clear(); } } catch (NoSuchFileException e) { - return Component.text("Directory doesn't exist").color(NamedTextColor.RED); + throw new CommandException(Component.text("Directory doesn't exist")); } catch (IOException e) { e.printStackTrace(); } @@ -277,10 +270,10 @@ public class MusicCommand extends Command { return null; } - public Component skip (CommandContext context) { + public Component skip (CommandContext context) throws CommandException { final Bot bot = context.bot; final MusicPlayerPlugin music = bot.music; - if (music.currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); + if (music.currentSong == null) throw new CommandException(Component.text("No song is currently playing")); context.sendOutput( Component.empty() @@ -294,10 +287,10 @@ public class MusicCommand extends Command { return null; } - public Component nowplaying (CommandContext context) { + public Component nowplaying (CommandContext context) throws CommandException { final Bot bot = context.bot; final Song song = bot.music.currentSong; - if (song == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); + if (song == null) throw new CommandException(Component.text("No song is currently playing")); return Component.empty() .append(Component.text("Now playing ")) @@ -323,17 +316,17 @@ public class MusicCommand extends Command { } // lazy fix for java using "goto" as keyword real - public Component goTo (CommandContext context, String[] args) { + public Component goTo (CommandContext context) throws CommandException { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - final String input = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + final String input = context.getString(true, true); final long timestamp = TimestampUtilities.parseTimestamp(input); - if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); + if (currentSong == null) throw new CommandException(Component.text("No song is currently playing")); - if (timestamp < 0 || timestamp > currentSong.length) return Component.text("Invalid timestamp").color(NamedTextColor.RED); + if (timestamp < 0 || timestamp > currentSong.length) throw new CommandException(Component.text("Invalid timestamp")); currentSong.setTime(timestamp); @@ -343,15 +336,10 @@ public class MusicCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } - public Component pitch (CommandContext context, String[] args) { + public Component pitch (CommandContext context) throws CommandException { final Bot bot = context.bot; - float pitch; - try { - pitch = Float.parseFloat(args[1]); - } catch (IllegalArgumentException ignored) { - return Component.text("Invalid pitch").color(NamedTextColor.RED); - } + final float pitch = context.getFloat(true); bot.music.pitch = pitch; @@ -361,18 +349,13 @@ public class MusicCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } - public Component speed (CommandContext context, String[] args) { + public Component speed (CommandContext context) throws CommandException { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - float speed; - try { - speed = Float.parseFloat(args[1]); - } catch (IllegalArgumentException ignored) { - return Component.text("Invalid speed").color(NamedTextColor.RED); - } + final float speed = context.getFloat(true); - if (speed > 5) return Component.text("Too fast").color(NamedTextColor.RED); + if (speed > 5) throw new CommandException(Component.text("Too fast!")); long oldTime = -1; @@ -388,10 +371,10 @@ public class MusicCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } - public Component noteInstrument (CommandContext context, String[] args) { + public Component noteInstrument (CommandContext context) throws CommandException { final Bot bot = context.bot; - final String instrument = args[1]; + final String instrument = context.getString(true, true); bot.music.instrument = instrument; @@ -405,11 +388,11 @@ public class MusicCommand extends Command { } } - public Component pause (CommandContext context) { + public Component pause (CommandContext context) throws CommandException { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); + if (currentSong == null) throw new CommandException(Component.text("No song is currently playing")); if (currentSong.paused) { currentSong.play(); @@ -420,11 +403,11 @@ public class MusicCommand extends Command { } } - public Component info (CommandContext context) { + public Component info (CommandContext context) throws CommandException { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); + if (currentSong == null) throw new CommandException(Component.text("No song is currently playing")); // ig very code yup final String title = currentSong.originalName; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/NetMessageCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/NetMessageCommand.java index 808380b..ba93c57 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/NetMessageCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/NetMessageCommand.java @@ -3,6 +3,7 @@ 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -16,7 +17,7 @@ public class NetMessageCommand extends Command { super( "netmsg", "Broadcasts a message to all of the servers that the bot is connected", - new String[] { "<{message}>" }, + new String[] { "" }, new String[] { "networkmessage", "irc" }, TrustLevel.PUBLIC, false @@ -24,7 +25,7 @@ public class NetMessageCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; final List bots = bot.bots; @@ -47,7 +48,7 @@ public class NetMessageCommand extends Command { Component.text(" "), context.sender.displayName == null ? Component.text(context.sender.profile.getName()).color(NamedTextColor.GRAY) : context.sender.displayName.color(NamedTextColor.GRAY), Component.text(" "), - Component.text(String.join(" ", args)).color(NamedTextColor.GRAY) + Component.text(context.getString(true, true)).color(NamedTextColor.GRAY) ).color(NamedTextColor.DARK_GRAY); for (Bot eachBot : bots) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/PCrashCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/PCrashCommand.java index 2b2c0bc..58dc93a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/PCrashCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/PCrashCommand.java @@ -3,6 +3,7 @@ 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.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -14,7 +15,7 @@ public class PCrashCommand extends Command { super( "pcrash", "Crashes a player using particle", - new String[] { " <{player}>" }, + new String[] { " " }, new String[] { "particlecrash" }, TrustLevel.TRUSTED, false @@ -22,14 +23,12 @@ public class PCrashCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { - if (args.length == 0) return Component.text("Not enough arguments").color(NamedTextColor.RED); - + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final PlayerEntry player = bot.players.getEntry(args[0]); + final PlayerEntry player = bot.players.getEntry(context.getString(true, true)); - if (player == null) return Component.text("Invalid player name").color(NamedTextColor.RED); + if (player == null) throw new CommandException(Component.text("Invalid player name")); bot.exploits.pcrash(player.profile.getId()); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RandomTeleportCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RandomTeleportCommand.java index 1697b11..8a3e356 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RandomTeleportCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RandomTeleportCommand.java @@ -3,6 +3,7 @@ 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.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -23,14 +24,14 @@ public class RandomTeleportCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; final PlayerEntry sender = context.sender; final int positionX = MathUtilities.between(-1_000_000, 1_000_000); final int positionZ = MathUtilities.between(-1_000_000, 1_000_000); - final String stringPosition = positionX + " 100 " + positionZ; // is harding the y to 100 a great idea? + final String stringPosition = positionX + " 100 " + positionZ; // is hardcoding the y to 100 a great idea? bot.core.run("essentials:teleport " + sender.profile.getIdAsString() + " " + stringPosition); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RefillCoreCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RefillCoreCommand.java index de0798a..f8ff378 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RefillCoreCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/RefillCoreCommand.java @@ -19,7 +19,7 @@ public class RefillCoreCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) { final Bot bot = context.bot; bot.core.reset(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ScreenshareCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ScreenshareCommand.java index 4b94a85..5ce615c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ScreenshareCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ScreenshareCommand.java @@ -18,7 +18,7 @@ public class ScreenshareCommand { // extends Command { // } // @Override -// public Component execute(CommandContext context, String[] args, String[] fullArgs) { +// public Component execute(CommandContext context) throws CommandException { // final Bot bot = context.bot; // // try { @@ -66,11 +66,11 @@ public class ScreenshareCommand { // extends Command { // .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); // } // default -> { -// return Component.text("Invalid action").color(NamedTextColor.RED); +// throw new CommandException(Component.text("Invalid action")); // } // } // } catch (NumberFormatException e) { -// return Component.text("Invalid integer").color(NamedTextColor.RED); +// throw new CommandException(Component.text("Invalid integer")); // } // } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ServerEvalCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ServerEvalCommand.java index bcbefc2..96788f5 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ServerEvalCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ServerEvalCommand.java @@ -3,6 +3,7 @@ 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -16,7 +17,7 @@ public class ServerEvalCommand extends Command { super( "servereval", "Evaluate codes using LuaJ", - new String[] { " <{code}>" }, + new String[] { " " }, new String[] {}, TrustLevel.OWNER, false @@ -24,7 +25,7 @@ public class ServerEvalCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { try { final Bot bot = context.bot; @@ -34,13 +35,13 @@ public class ServerEvalCommand extends Command { globals.set("class", CoerceJavaToLua.coerce(Class.class)); globals.set("context", CoerceJavaToLua.coerce(context)); - LuaValue chunk = globals.load(String.join(" ", args)); + LuaValue chunk = globals.load(context.getString(true, true)); final LuaValue output = chunk.call(); return Component.text(output.toString()).color(NamedTextColor.GREEN); } catch (Exception e) { - return Component.text(e.toString()).color(NamedTextColor.RED); + throw new CommandException(Component.text(e.toString())); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TPSBarCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TPSBarCommand.java index 959a4cb..89d8fb1 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TPSBarCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TPSBarCommand.java @@ -3,6 +3,7 @@ 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; @@ -21,10 +22,12 @@ public class TPSBarCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - switch (args[0]) { + final String action = context.getString(false, true); + + switch (action) { case "on" -> { bot.tps.on(); return Component.empty() @@ -40,7 +43,7 @@ public class TPSBarCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } default -> { - return Component.text("Invalid action").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid action")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TestCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TestCommand.java index 9bca867..93d27cf 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TestCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TestCommand.java @@ -2,6 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.commands; 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -11,7 +12,7 @@ public class TestCommand extends Command { super( "test", "Tests if the bot is working", - new String[] { "[{args}]" }, + new String[] { "[args]" }, new String[] {}, TrustLevel.PUBLIC, false @@ -19,13 +20,13 @@ public class TestCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { return Component.translatable( "Hello, World! Username: %s, Sender UUID: %s, Prefix: %s, Args: %s", Component.text(context.sender.profile.getName()), Component.text(context.sender.profile.getIdAsString()), Component.text(context.prefix), - Component.text(String.join(", ", args)) + Component.text(context.getString(true, false)) ).color(NamedTextColor.GREEN); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TimeCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TimeCommand.java index c8e9d2f..2b484ef 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TimeCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TimeCommand.java @@ -3,6 +3,7 @@ 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; @@ -25,16 +26,16 @@ public class TimeCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final String timezone = args[0]; + final String timezone = context.getString(true, true); DateTimeZone zone; try { zone = DateTimeZone.forID(timezone); } catch (IllegalArgumentException ignored) { - return Component.text("Invalid timezone (case-sensitive)").color(NamedTextColor.RED); + throw new CommandException(Component.text("Invalid timezone (case-sensitive)")); } final DateTime dateTime = new DateTime(zone); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TranslateCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TranslateCommand.java index c218ae4..b2d13b2 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TranslateCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/TranslateCommand.java @@ -6,6 +6,7 @@ import com.google.gson.JsonObject; 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 land.chipmunk.chayapak.chomens_bot.util.HttpUtilities; @@ -15,14 +16,13 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.Arrays; public class TranslateCommand extends Command { public TranslateCommand () { super( "translate", "Translates a message using Google Translate", - new String[] { " <{message}>" }, + new String[] { " " }, new String[] {}, TrustLevel.PUBLIC, false @@ -30,13 +30,13 @@ public class TranslateCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final String from = args[0]; - final String to = args[1]; + final String from = context.getString(false, true); + final String to = context.getString(false, true); - final String message = String.join(" ", Arrays.copyOfRange(args, 2, args.length)); + final String message = context.getString(true, true); final Gson gson = new Gson(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UUIDCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UUIDCommand.java index b0a9f2d..94aba32 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UUIDCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UUIDCommand.java @@ -3,6 +3,7 @@ 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.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; @@ -16,7 +17,7 @@ public class UUIDCommand extends Command { super( "uuid", "Shows your UUID or other player's UUID", - new String[] { "[{username}]" }, + new String[] { "[username]" }, new String[] {}, TrustLevel.PUBLIC, false @@ -24,13 +25,15 @@ public class UUIDCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - if (args.length > 0) { - final PlayerEntry entry = bot.players.getEntry(String.join(" ", args)); + final String player = context.getString(true, false); - if (entry == null) return Component.text("Invalid player name").color(NamedTextColor.RED); + if (!player.isEmpty()) { + final PlayerEntry entry = bot.players.getEntry(player); + + if (entry == null) throw new CommandException(Component.text("Invalid player name")); final String name = entry.profile.getName(); final String uuid = entry.profile.getIdAsString(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java index dc46ccb..e18b0fb 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java @@ -6,10 +6,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Main; -import land.chipmunk.chayapak.chomens_bot.command.Command; -import land.chipmunk.chayapak.chomens_bot.command.CommandContext; -import land.chipmunk.chayapak.chomens_bot.command.DiscordCommandContext; -import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; +import land.chipmunk.chayapak.chomens_bot.command.*; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities; import net.kyori.adventure.text.Component; @@ -32,7 +29,7 @@ public class UrbanCommand extends Command { super( "urban", "Urban Dictionary in Minecraft", - new String[] { "<{term}>" }, + new String[] { "" }, new String[] {}, TrustLevel.PUBLIC, false @@ -41,14 +38,14 @@ public class UrbanCommand extends Command { Main.executor.scheduleAtFixedRate(() -> requestsPerSecond = 0, 0, 1, TimeUnit.SECONDS); } - public Component execute (CommandContext context, String[] args, String[] fullArgs) { - if (requestsPerSecond > 3) return Component.text("Too many requests").color(NamedTextColor.RED); + public Component execute (CommandContext context) throws CommandException { + if (requestsPerSecond > 3) throw new CommandException(Component.text("Too many requests")); final Bot bot = context.bot; final boolean discord = context instanceof DiscordCommandContext; - final String term = String.join(" ", args); + final String term = context.getString(true, true); final Gson gson = new Gson(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java index ea1775a..10abfda 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java @@ -3,6 +3,7 @@ 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -20,13 +21,13 @@ public class ValidateCommand extends Command { } @Override - public Component execute(CommandContext context, String[] args, String[] fullArgs) { + public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; - final String hash = fullArgs[0]; + final String hash = context.fullArgs[0]; - if (bot.hashing.isCorrectHash(hash, context.splitInput[0], context.sender)) return Component.text("Valid hash").color(NamedTextColor.GREEN); - else if (bot.hashing.isCorrectOwnerHash(hash, context.splitInput[0], context.sender)) return Component.text("Valid OwnerHash").color(NamedTextColor.GREEN); + if (bot.hashing.isCorrectHash(hash, context.commandName, context.sender)) return Component.text("Valid hash").color(NamedTextColor.GREEN); + else if (bot.hashing.isCorrectOwnerHash(hash, context.commandName, context.sender)) return Component.text("Valid OwnerHash").color(NamedTextColor.GREEN); return null; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WeatherCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WeatherCommand.java index 1eaa9dd..bb76475 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WeatherCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WeatherCommand.java @@ -5,6 +5,7 @@ import com.google.gson.JsonObject; 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 land.chipmunk.chayapak.chomens_bot.util.HttpUtilities; @@ -24,17 +25,17 @@ public class WeatherCommand extends Command { super( "weather", "Shows the weather in a place", - new String[] { "<{location}>" }, + new String[] { "" }, new String[] {}, TrustLevel.PUBLIC, false ); } - public Component execute (CommandContext context, String[] args, String[] fullArgs) { + public Component execute (CommandContext context) throws CommandException { final Bot bot = context.bot; - final String location = String.join(" ", args); + final String location = context.getString(true, true); final Gson gson = new Gson(); @@ -107,7 +108,7 @@ public class WeatherCommand extends Command { Component.text(time).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } catch (Exception e) { - return Component.text("Location \"" + location + "\" not found").color(NamedTextColor.RED); + throw new CommandException(Component.text("Location \"" + location + "\" not found")); } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WikipediaCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WikipediaCommand.java index 15368cb..806f100 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WikipediaCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WikipediaCommand.java @@ -5,6 +5,7 @@ import com.google.gson.JsonObject; 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.HttpUtilities; import net.kyori.adventure.text.Component; @@ -20,17 +21,17 @@ public class WikipediaCommand extends Command { super( "wikipedia", "Wikipedia in Minecraft", - new String[] { "<{page}>" }, + new String[] { "" }, new String[] { "wiki" }, TrustLevel.PUBLIC, false ); } - public Component execute (CommandContext context, String[] args, String[] fullArgs) { + public Component execute (CommandContext context) throws CommandException { final Bot bot = context.bot; - final String page = String.join(" ", args); + final String page = context.getString(true, true); final Gson gson = new Gson(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java index 9259a2e..e1e4d08 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java @@ -58,9 +58,11 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener { final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, "@a", message.sender); - final Component output = bot.commandHandler.executeCommand(commandString, context, null); + bot.executorService.submit(() -> { + final Component output = bot.commandHandler.executeCommand(commandString, context, null); - if (output != null) context.sendOutput(output); + if (output != null) context.sendOutput(output); + }); } public void commandSpyMessageReceived (PlayerEntry sender, String command) { @@ -90,8 +92,10 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener { final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, sender); - final Component output = bot.commandHandler.executeCommand(commandString, context, null); + bot.executorService.submit(() -> { + final Component output = bot.commandHandler.executeCommand(commandString, context, null); - if (output != null) context.sendOutput(output); + if (output != null) context.sendOutput(output); + }); } } 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 386152d..90bc0df 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 @@ -100,24 +100,13 @@ public class CommandHandlerPlugin { final TrustLevel trustLevel = command.trustLevel; - final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length); - - // TODO: improve these minimum args and maximum args stuff, the current one really sucks.,., - final int shortestUsageIndex = getShortestUsageIndex(command.usages); - final int longestUsageIndex = getLongestUsageIndex(command.usages); - final String shortestUsage = shortestUsageIndex == 0 && command.usages.length == 0 ? "" : command.usages[shortestUsageIndex]; - final String longestUsage = longestUsageIndex == 0 && command.usages.length == 0 ? "" : command.usages[longestUsageIndex]; - - final int minimumArgs = getMinimumArgs(shortestUsage, inGame, command.trustLevel); - final int maximumArgs = getMaximumArgs(longestUsage, inGame, command.trustLevel); - if (fullArgs.length < minimumArgs) return Component.text("Excepted minimum of " + minimumArgs + " argument(s), got " + fullArgs.length).color(NamedTextColor.RED); - if (fullArgs.length > maximumArgs && !longestUsage.contains("{")) return Component.text("Too many arguments, expected " + maximumArgs + " max").color(NamedTextColor.RED); - if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame) return Component.text("Please provide a hash").color(NamedTextColor.RED); String userHash = ""; if (trustLevel != TrustLevel.PUBLIC && inGame) userHash = splitInput[1]; + final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length); + final String[] args = Arrays.copyOfRange(splitInput, (trustLevel != TrustLevel.PUBLIC && inGame) ? 2 : 1, splitInput.length); if (command.trustLevel != TrustLevel.PUBLIC && !console) { @@ -157,11 +146,15 @@ public class CommandHandlerPlugin { if (!console && command.consoleOnly) return Component.text("This command can only be ran via console").color(NamedTextColor.RED); - context.splitInput = splitInput; + // should these be here? + context.fullArgs = fullArgs; + context.args = args; context.commandName = command.name; try { - return command.execute(context, args, fullArgs); + return command.execute(context); + } catch (CommandException e) { + return e.message.color(NamedTextColor.RED); } catch (Exception e) { e.printStackTrace(); @@ -176,7 +169,8 @@ public class CommandHandlerPlugin { .text(stackTrace) .color(NamedTextColor.RED) ) - ); + ) + .color(NamedTextColor.RED); } else { return Component.text(stackTrace).color(NamedTextColor.RED); } @@ -197,66 +191,4 @@ public class CommandHandlerPlugin { } return null; } - - private int getLongestUsageIndex(String[] usages) { - int longestIndex = 0; - int maxLength = 0; - - final int usagesSize = usages.length; - - for (int i = 0; i < usagesSize; i++) { - String[] args = usages[i].split("\\s+"); - if (args.length > maxLength) { - longestIndex = i; - maxLength = args.length; - } - } - return longestIndex; - } - - private int getShortestUsageIndex(String[] usages) { - int shortestIndex = 0; - int minLength = Integer.MAX_VALUE; - - final int usagesSize = usages.length; - - for (int i = 0; i < usagesSize; i++) { - String[] args = usages[i].split("\\s+"); - if (args.length < minLength) { - shortestIndex = i; - minLength = args.length; - } - } - - return shortestIndex; - } - - private int getMinimumArgs(String usage, boolean inGame, TrustLevel trustLevel) { - int count = 0; - - final int usageLength = usage.length(); - - for (int i = 0; i < usageLength; i++) { - if (usage.charAt(i) == '<') { - count++; - } - } - if (usage.contains("")) count--; // bad fix? - if ((!inGame && trustLevel != TrustLevel.PUBLIC)) count--; - return count; - } - - private int getMaximumArgs(String usage, boolean inGame, TrustLevel trustLevel) { - int count = 0; - - final int usageLength = usage.length(); - - for (int i = 0; i < usageLength; i++) { - if (usage.charAt(i) == '<' || usage.charAt(i) == '[') { - count++; - } - } - if (!inGame && trustLevel != TrustLevel.PUBLIC) count++; - return count; - } }