From 884c525f2ef3ffc52ac7d9fcd54a37efc96da1ef Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 3 Apr 2025 10:50:41 +0700 Subject: [PATCH] refactor: make `executeCommand` in CommandHandlerPlugin return void instead of a `Component` --- build-number.txt | 2 +- .../chomens_bot/chomeNSMod/PacketHandler.java | 4 +- .../plugins/ChatCommandHandlerPlugin.java | 4 +- .../plugins/CommandHandlerPlugin.java | 101 +++++++++++------- .../chomens_bot/plugins/ConsolePlugin.java | 6 +- .../chomens_bot/plugins/DiscordPlugin.java | 6 +- .../chomens_bot/plugins/IRCPlugin.java | 4 +- 7 files changed, 68 insertions(+), 59 deletions(-) diff --git a/build-number.txt b/build-number.txt index 91ab6862..4c50c5be 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2350 \ No newline at end of file +2358 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/chomeNSMod/PacketHandler.java b/src/main/java/me/chayapak1/chomens_bot/chomeNSMod/PacketHandler.java index 0d843eb4..3afb69a4 100644 --- a/src/main/java/me/chayapak1/chomens_bot/chomeNSMod/PacketHandler.java +++ b/src/main/java/me/chayapak1/chomens_bot/chomeNSMod/PacketHandler.java @@ -58,8 +58,6 @@ public class PacketHandler { player ); - final Component component = bot.commandHandler.executeCommand(input, context, null); - - if (component != null) context.sendOutput(component); + bot.commandHandler.executeCommand(input, context, null); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java index ba53e2e9..7f1876c2 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java @@ -82,8 +82,6 @@ public class ChatCommandHandlerPlugin implements ChatPlugin.Listener, CommandSpy final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, sender); - final Component output = bot.commandHandler.executeCommand(commandString, context, null); - - if (output != null) context.sendOutput(output); + bot.commandHandler.executeCommand(commandString, context, null); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java index 5500f698..42d74338 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java @@ -105,7 +105,10 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { // BETTER QUALITY than the js version // BUT still not the best - public Component executeCommand ( + // and also not really optimized + // (sometimes execution time can be as high as 19 ms, + // though it can also be as low as 4000 ns) + public void executeCommand ( String input, CommandContext context, MessageReceivedEvent event @@ -115,30 +118,37 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { final boolean bypass = context instanceof ConsoleCommandContext || context instanceof ChomeNSModCommandContext; - if (commandPerSecond > 100) return null; + if (commandPerSecond > 100) return; commandPerSecond++; final String[] splitInput = input.trim().split("\\s+"); - if (splitInput.length == 0) return null; + if (splitInput.length == 0) return; final String commandName = splitInput[0]; final Command command = findCommand(commandName); // I think this is kinda annoying when you correct spelling mistakes or something, - // so I made it return nothing if it's in game - if (command == null && !inGame) - return Component.text("Unknown command: " + commandName).color(NamedTextColor.RED); - else if (command == null) return null; + // so I made it return nothing if we're in game + if (command == null) { + if (!inGame) context.sendOutput(Component.text("Unknown command: " + commandName).color(NamedTextColor.RED)); - if (!bypass && disabled) return Component.text("ChomeNS Bot is currently disabled").color(NamedTextColor.RED); + return; + } + + if (!bypass && disabled) { + context.sendOutput(Component.text("ChomeNS Bot is currently disabled").color(NamedTextColor.RED)); + return; + } final TrustLevel trustLevel = command.trustLevel; - if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame) - return Component.text("Please provide a hash").color(NamedTextColor.RED); + if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame) { + context.sendOutput(Component.text("Please provide a hash").color(NamedTextColor.RED)); + return; + } String userHash = ""; if (trustLevel != TrustLevel.PUBLIC && inGame) userHash = splitInput[1]; @@ -151,7 +161,7 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { if (discord) { final Member member = event.getMember(); - if (member == null) return null; + if (member == null) return; final List roles = member.getRoles(); @@ -171,12 +181,15 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { } if (trustLevel.level > userTrustLevel.level) { - return Component - .translatable( - "Your current roles don't allow you to execute %s commands!", - Component.text(trustLevel.name()) - ) - .color(NamedTextColor.RED); + context.sendOutput( + Component + .translatable( + "Your current roles don't allow you to execute %s commands!", + Component.text(trustLevel.name()) + ) + .color(NamedTextColor.RED) + ); + return; } context.trustLevel = userTrustLevel; @@ -184,12 +197,15 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { final TrustLevel userTrustLevel = bot.hashing.getTrustLevel(userHash, splitInput[0], context.sender); if (trustLevel.level > userTrustLevel.level) { - return Component - .translatable( - "Invalid %s hash", - Component.text(trustLevel.name()) - ) - .color(NamedTextColor.RED); + context.sendOutput( + Component + .translatable( + "Invalid %s hash", + Component.text(trustLevel.name()) + ) + .color(NamedTextColor.RED) + ); + return; } context.trustLevel = userTrustLevel; @@ -197,8 +213,10 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { } // should i give access to all bypass contexts instead of only console? - if (!bypass && command.consoleOnly) - return Component.text("This command can only be run via console").color(NamedTextColor.RED); + if (!bypass && command.consoleOnly) { + context.sendOutput(Component.text("This command can only be run via console").color(NamedTextColor.RED)); + return; + } // should these be here? context.fullArgs = fullArgs; @@ -207,28 +225,33 @@ public class CommandHandlerPlugin implements TickPlugin.Listener { context.userInputCommandName = commandName; try { - return command.execute(context); + final Component output = command.execute(context); + + if (output != null) context.sendOutput(output); } catch (CommandException e) { - return e.message.color(NamedTextColor.RED); + context.sendOutput(e.message.color(NamedTextColor.RED)); } catch (Exception e) { bot.logger.error(e); final String stackTrace = ExceptionUtilities.getStacktrace(e); if (inGame) { if (bot.options.useChat || !bot.options.useCore) - return Component.text(e.toString()).color(NamedTextColor.RED); - return Component - .text("An error occurred while trying to execute the command, hover here for stacktrace", NamedTextColor.RED) - .hoverEvent( - HoverEvent.showText( - Component - .text(stackTrace) - .color(NamedTextColor.RED) - ) - ) - .color(NamedTextColor.RED); + context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED)); + else + context.sendOutput( + Component + .text("An error occurred while trying to execute the command, hover here for stacktrace", NamedTextColor.RED) + .hoverEvent( + HoverEvent.showText( + Component + .text(stackTrace) + .color(NamedTextColor.RED) + ) + ) + .color(NamedTextColor.RED) + ); } else { - return Component.text(stackTrace).color(NamedTextColor.RED); + context.sendOutput(Component.text(stackTrace).color(NamedTextColor.RED)); } } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java index a1b87417..2552b465 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java @@ -97,11 +97,7 @@ public class ConsolePlugin implements Completer { if (line.startsWith(prefix)) { final ConsoleCommandContext context = new ConsoleCommandContext(bot, prefix); - final Component output = bot.commandHandler.executeCommand(line.substring(prefix.length()), context, null); - - if (output != null) { - context.sendOutput(output); - } + bot.commandHandler.executeCommand(line.substring(prefix.length()), context, null); continue; } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java index e0b0dfc3..1ad9fc54 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java @@ -176,11 +176,7 @@ public class DiscordPlugin extends ListenerAdapter { if (message.startsWith(prefix)) { final DiscordCommandContext context = new DiscordCommandContext(bot, prefix, event); - final Component output = bot.commandHandler.executeCommand(message.substring(prefix.length()), context, event); - - if (output != null) { - context.sendOutput(output); - } + bot.commandHandler.executeCommand(message.substring(prefix.length()), context, event); return; } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/IRCPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/IRCPlugin.java index 17957cb8..07f829df 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/IRCPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/IRCPlugin.java @@ -123,9 +123,7 @@ public class IRCPlugin extends ListenerAdapter { final IRCCommandContext context = new IRCCommandContext(serverBot, commandPrefix, name); - final Component output = serverBot.commandHandler.executeCommand(noPrefix, context, null); - - if (output != null) context.sendOutput(output); + serverBot.commandHandler.executeCommand(noPrefix, context, null); return; }