From 63967e407d76f59b2f65ade83529bbae6c17a1a9 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 1 Oct 2023 17:35:33 +0700 Subject: [PATCH] add alias to chome ns bot suggestions --- .../chipmunkmod/data/ChomeNSBotCommand.java | 4 +++ .../chipmunkmod/mixin/ChatScreenMixin.java | 19 ++++++++++++-- .../modules/ChomeNSBotCommandSuggestions.java | 25 ++++++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/data/ChomeNSBotCommand.java b/src/main/java/land/chipmunk/chipmunkmod/data/ChomeNSBotCommand.java index 978e705..93a671f 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/data/ChomeNSBotCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/data/ChomeNSBotCommand.java @@ -1,8 +1,12 @@ package land.chipmunk.chipmunkmod.data; +import java.util.ArrayList; +import java.util.List; + public class ChomeNSBotCommand { public final String name; public final TrustLevel trustLevel; + public final List aliases = new ArrayList<>(); public ChomeNSBotCommand ( String name, diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java index 81f84eb..c10f7dd 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java @@ -23,6 +23,7 @@ import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.OutputStream; import java.net.URL; +import java.util.ArrayList; import java.util.List; @Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class) @@ -104,9 +105,21 @@ public class ChatScreenMixin extends Screen { .map((command) -> command.name.toLowerCase()) .toList(); - if (moreOrTrustedCommands.contains(chatText.toLowerCase().split("\\s")[0])) { + final List aliases = new ArrayList<>(); + for (ChomeNSBotCommand command : commands) { + aliases.addAll(command.aliases); + } + + final String chatCommand = chatText.toLowerCase().split("\\s")[0]; + + final int prefixLength = ChipmunkMod.CONFIG.bots.chomens.prefix.length(); + + if ( + moreOrTrustedCommands.contains(chatCommand) || + aliases.contains(chatCommand.substring(prefixLength)) + ) { try { - BotValidationUtilities.chomens(chatText.substring(ChipmunkMod.CONFIG.bots.chomens.prefix.length())); + BotValidationUtilities.chomens(chatText.substring(prefixLength)); cir.setReturnValue(true); cir.cancel(); @@ -116,6 +129,8 @@ public class ChatScreenMixin extends Screen { } } + if (client == null) return; + if (chatText.startsWith("/")) { client.player.networkHandler.sendChatCommand(chatText.substring(1)); } else { diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSBotCommandSuggestions.java b/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSBotCommandSuggestions.java index 32a28b2..91bd70c 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSBotCommandSuggestions.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSBotCommandSuggestions.java @@ -13,7 +13,6 @@ import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.text.Text; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class ChomeNSBotCommandSuggestions extends Listener { @@ -59,7 +58,7 @@ public class ChomeNSBotCommandSuggestions extends Listener { final List children = component.children(); - if (children.size() == 0) return; + if (children.isEmpty()) return; final TextComponent textComponent = (TextComponent) children.get(0); @@ -68,10 +67,24 @@ public class ChomeNSBotCommandSuggestions extends Listener { commands = children.subList(1, children.size()) .stream() .map( - (eachCum) -> new ChomeNSBotCommand( - ChipmunkMod.CONFIG.bots.chomens.prefix + ((TextComponent) eachCum).content(), - ChomeNSBotCommand.TrustLevel.valueOf(((TextComponent) eachCum.children().get(0)).content()) - ) + (eachComponent) -> { + final ChomeNSBotCommand command = new ChomeNSBotCommand( + ChipmunkMod.CONFIG.bots.chomens.prefix + ((TextComponent) eachComponent).content(), + ChomeNSBotCommand.TrustLevel.valueOf(((TextComponent) eachComponent.children().get(0)).content()) + ); + + if (!Boolean.parseBoolean(((TextComponent) eachComponent.children().get(1)).content())) return command; + + final List subList = eachComponent.children().subList(2, eachComponent.children().size()); + + for (Component aliasComponent : subList) { + final String alias = ((TextComponent) aliasComponent).content(); + + command.aliases.add(alias); + } + + return command; + } ) .toList(); } catch (Exception ignored) {}