From 8b61ed93aa625adb88baec1836ac6dc3987acb9c Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:04:09 +0700 Subject: [PATCH] port too many arguments back mess and i'm not sure if i did the correct numbers for every command because uh my brain is kinda broken --- .../chomens_bot/command/CommandContext.java | 10 ++++++ .../commands/BotVisibilityCommand.java | 2 ++ .../commands/ClearChatQueueCommand.java | 5 ++- .../chomens_bot/commands/CloopCommand.java | 6 ++++ .../chomens_bot/commands/ConsoleCommand.java | 2 ++ .../chomens_bot/commands/EndCommand.java | 5 ++- .../chomens_bot/commands/FilterCommand.java | 6 ++++ .../chomens_bot/commands/HelpCommand.java | 4 ++- .../chomens_bot/commands/IPFilterCommand.java | 6 ++++ .../chomens_bot/commands/InfoCommand.java | 2 ++ .../chomens_bot/commands/ListCommand.java | 5 ++- .../chomens_bot/commands/MailCommand.java | 4 +++ .../chomens_bot/commands/MusicCommand.java | 32 +++++++++++++++++-- .../commands/RandomTeleportCommand.java | 2 ++ .../commands/RefillCoreCommand.java | 5 ++- .../commands/ScreenshareCommand.java | 8 +++++ .../chomens_bot/commands/TPSBarCommand.java | 2 ++ .../commands/WhitelistCommand.java | 8 +++++ 18 files changed, 106 insertions(+), 8 deletions(-) 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 d13fad4..24dced9 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 @@ -35,6 +35,7 @@ public class CommandContext { public Component displayName () { return Component.empty(); } public void sendOutput (Component component) {} + // args parsing stuff private int argsPosition = 0; public String getString (boolean greedy, boolean required) throws CommandException { return getString(greedy, required, "string"); } @@ -128,4 +129,13 @@ public class CommandContext { throw new CommandException(Component.text("Invalid enum")); } } + + public void checkOverloadArgs (int maximumArgs) throws CommandException { + if (args.length > maximumArgs) throw new CommandException( + Component.translatable( + "Too many arguments, expected %s max", + Component.text(maximumArgs) + ) + ); + } } 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 7ae986f..fd9c291 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 @@ -23,6 +23,8 @@ public class BotVisibilityCommand extends Command { @Override public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final String action = context.getString(false, false, false); 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 73d2b83..fa73b70 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 @@ -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; @@ -19,7 +20,9 @@ public class ClearChatQueueCommand extends Command { } @Override - public Component execute(CommandContext context) { + public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(0); + 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 44a1cc7..ef55ded 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 @@ -48,6 +48,8 @@ public class CloopCommand extends Command { ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "remove" -> { + context.checkOverloadArgs(2); + try { final int index = context.getInteger(true); bot.cloop.remove(index); @@ -61,10 +63,14 @@ public class CloopCommand extends Command { } } case "clear" -> { + context.checkOverloadArgs(1); + bot.cloop.clear(); return Component.text("Cleared all cloops").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "list" -> { + context.checkOverloadArgs(1); + final List cloopsComponent = new ArrayList<>(); int index = 0; 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 e2490af..2946476 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 @@ -65,6 +65,8 @@ public class ConsoleCommand extends Command { } } case "logtoconsole" -> { + context.checkOverloadArgs(2); + final boolean bool = context.getBoolean(true); bot.logger.logToConsole = bool; 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 ffa904e..33fdbae 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 @@ -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; @@ -19,7 +20,9 @@ public class EndCommand extends Command { } @Override - public Component execute(CommandContext context) { + public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(0); + final Bot bot = context.bot; bot.session.disconnect("End command"); 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 6cf8c9d..9897987 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 @@ -77,6 +77,8 @@ public class FilterCommand extends Command { ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "remove" -> { + context.checkOverloadArgs(2); + try { final int index = context.getInteger(true); @@ -91,10 +93,14 @@ public class FilterCommand extends Command { } } case "clear" -> { + context.checkOverloadArgs(1); + bot.filter.clear(); return Component.text("Cleared the filter").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "list" -> { + context.checkOverloadArgs(1); + final List filtersComponents = new ArrayList<>(); int index = 0; 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 bb20809..5aac711 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 @@ -33,9 +33,11 @@ public class HelpCommand extends Command { @Override public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + this.context = context; - final String commandName = context.getString(true, false); + final String commandName = context.getString(false, false); if (commandName.isEmpty()) { return sendCommandList(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java index 523aba9..18b46c9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java @@ -50,6 +50,8 @@ public class IPFilterCommand extends Command { ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "remove" -> { + context.checkOverloadArgs(2); + try { final int index = context.getInteger(true); @@ -64,10 +66,14 @@ public class IPFilterCommand extends Command { } } case "clear" -> { + context.checkOverloadArgs(1); + bot.ipFilter.clear(); return Component.text("Cleared the filter").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "list" -> { + context.checkOverloadArgs(1); + final List filtersComponents = new ArrayList<>(); int index = 0; 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 ab8f86d..b17cc88 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 @@ -45,6 +45,8 @@ public class InfoCommand extends Command { @Override public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final String action = context.getString(false, true, true); 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 764fa1a..2b104df 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 @@ -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; @@ -28,7 +29,9 @@ public class ListCommand extends Command { } @Override - public Component execute(CommandContext context) { + public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(0); + 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 9a5955a..2deb163 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 @@ -83,6 +83,8 @@ public class MailCommand extends Command { return Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "sendselecteditem" -> { + context.checkOverloadArgs(1); + int senderMailsSentTotal = 0; for (JsonElement mailElement : MailPlugin.mails) { final Mail mail = gson.fromJson(mailElement, Mail.class); @@ -154,6 +156,8 @@ public class MailCommand extends Command { }); } case "read" -> { + context.checkOverloadArgs(1); + // TODO: use less for loops? int senderMailSize = 0; 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 3bba3a8..e84b85f 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 @@ -197,6 +197,8 @@ public class MusicCommand extends Command { } public Component playFromItem (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + // mail command lol final Bot bot = context.bot; @@ -258,6 +260,8 @@ public class MusicCommand extends Command { } public Component playSongPlayer (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + // dupe codes ?? final Bot bot = context.bot; @@ -318,7 +322,9 @@ public class MusicCommand extends Command { return null; } - public Component stop (CommandContext context) { + public Component stop (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; bot.music.stopPlaying(); bot.music.songQueue.clear(); @@ -327,6 +333,8 @@ public class MusicCommand extends Command { } public Component loop (CommandContext context) throws CommandException { + context.checkOverloadArgs(2); + final Bot bot = context.bot; final Loop loop = context.getEnum(Loop.class); @@ -427,6 +435,8 @@ public class MusicCommand extends Command { } public Component skip (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final MusicPlayerPlugin music = bot.music; if (music.currentSong == null) throw new CommandException(Component.text("No song is currently playing")); @@ -444,6 +454,8 @@ public class MusicCommand extends Command { } public Component nowplaying (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final Song song = bot.music.currentSong; if (song == null) throw new CommandException(Component.text("No song is currently playing")); @@ -454,7 +466,9 @@ public class MusicCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } - public Component queue (CommandContext context) { + public Component queue (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final List queue = bot.music.songQueue; @@ -493,6 +507,8 @@ public class MusicCommand extends Command { } public Component pitch (CommandContext context) throws CommandException { + context.checkOverloadArgs(2); + final Bot bot = context.bot; final float pitch = context.getFloat(true); @@ -506,6 +522,8 @@ public class MusicCommand extends Command { } public Component speed (CommandContext context) throws CommandException { + context.checkOverloadArgs(2); + final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; @@ -528,6 +546,8 @@ public class MusicCommand extends Command { } public Component amplify(CommandContext context) throws CommandException { + context.checkOverloadArgs(2); + final Bot bot = context.bot; final int amplify = context.getInteger(true); @@ -560,6 +580,8 @@ public class MusicCommand extends Command { } public Component pause (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; @@ -575,6 +597,8 @@ public class MusicCommand extends Command { } public Component info (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; @@ -594,7 +618,9 @@ public class MusicCommand extends Command { return Component.join(JoinConfiguration.newlines(), components); } - public Component testSong (CommandContext context) { + public Component testSong (CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final Song song = new Song( 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 8a3e356..13604cb 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 @@ -25,6 +25,8 @@ public class RandomTeleportCommand extends Command { @Override public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(0); + final Bot bot = context.bot; final PlayerEntry sender = context.sender; 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 f8ff378..0d2e8ec 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 @@ -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; @@ -19,7 +20,9 @@ public class RefillCoreCommand extends Command { } @Override - public Component execute(CommandContext context) { + public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(0); + 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 e960900..9cf16ce 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 @@ -35,6 +35,8 @@ public class ScreenshareCommand extends Command { try { switch (action) { case "start" -> { + context.checkOverloadArgs(4); + final int x = context.getInteger(true); final int y = context.getInteger(true); final int z = context.getInteger(true); @@ -46,6 +48,8 @@ public class ScreenshareCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "stop" -> { + context.checkOverloadArgs(1); + bot.screenshare.stop(); return Component @@ -53,6 +57,8 @@ public class ScreenshareCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "setres" -> { + context.checkOverloadArgs(3); + final int width = context.getInteger(true); final int height = context.getInteger(true); @@ -67,6 +73,8 @@ public class ScreenshareCommand extends Command { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "setfps" -> { + context.checkOverloadArgs(2); + final int fps = context.getInteger(true); bot.screenshare.fps = fps; 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 17e51ef..1f76f5f 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 @@ -23,6 +23,8 @@ public class TPSBarCommand extends Command { @Override public Component execute(CommandContext context) throws CommandException { + context.checkOverloadArgs(1); + final Bot bot = context.bot; final String action = context.getString(false, true, true); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java index dcea7b9..5aa2837 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/WhitelistCommand.java @@ -33,11 +33,15 @@ public class WhitelistCommand extends Command { switch (action) { case "enable" -> { + context.checkOverloadArgs(1); + bot.whitelist.enable(); return Component.text("Enabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "disable" -> { + context.checkOverloadArgs(1); + bot.whitelist.disable(); return Component.text("Disabled whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); @@ -67,11 +71,15 @@ public class WhitelistCommand extends Command { ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "clear" -> { + context.checkOverloadArgs(1); + bot.whitelist.clear(); return Component.text("Cleared the whitelist").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } case "list" -> { + context.checkOverloadArgs(1); + final List playersComponent = new ArrayList<>(); int index = 0;