From 50addd1abd8810ba461f7c8efb316e604065b72c Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 28 Mar 2025 20:34:12 +0700 Subject: [PATCH] fix: `/msg` outgoing message target fix fix: forgor to merge the namespace remover with the one moved to StringUtilities lol --- build-number.txt | 2 +- .../chomens_bot/plugins/ChatPlugin.java | 54 ++++++++++--------- .../chomens_bot/util/StringUtilities.java | 19 +++---- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/build-number.txt b/build-number.txt index 19261c26..0efdb05c 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2248 \ No newline at end of file +2252 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java index 98ffad7c..a6129af6 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java @@ -11,6 +11,7 @@ import me.chayapak1.chomens_bot.data.player.PlayerEntry; import me.chayapak1.chomens_bot.data.team.Team; import me.chayapak1.chomens_bot.util.ComponentUtilities; import me.chayapak1.chomens_bot.util.IllegalCharactersUtilities; +import me.chayapak1.chomens_bot.util.StringUtilities; import me.chayapak1.chomens_bot.util.UUIDUtilities; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; @@ -154,24 +155,37 @@ public class ChatPlugin extends Bot.Listener { } } - private Component getComponentByChatType (int chatType, Component name, Component message) { + private Component getComponentByChatType (int chatType, Component sender, Component content) { final Component type = chatTypes.get(chatType); if (type == null) return null; - final Team botTeam = bot.team.findTeamByMember(bot.profile.getName()); - final Component botTeamDisplayName = botTeam == null ? - Component.empty() : - Component - .translatable("chat.square_brackets") - .arguments(botTeam.displayName) - .style(botTeam.colorToStyle()); + Component target; + + // minecraft has the /msg target as the player target, + // that actually makes sense but i don't know how to + // properly implement this lol + if ( + type instanceof TranslatableComponent translatableComponent && + translatableComponent.key().equals("commands.message.display.outgoing") + ) { + target = sender; + } else { + final Team botTeam = bot.team.findTeamByMember(bot.profile.getName()); + + target = botTeam == null ? + Component.empty() : + Component + .translatable("chat.square_brackets") + .arguments(botTeam.displayName) + .style(botTeam.colorToStyle()); + } return CHAT_TYPE_COMPONENT_RENDERER.render( type, new ChatTypeContext( - botTeamDisplayName, - name, - message + target, + sender, + content ) ); } @@ -268,20 +282,12 @@ public class ChatPlugin extends Bot.Listener { if (message == null) return; if (message.startsWith("/")) { - String removedMessage = message.substring(1); + final String slashRemoved = message.substring(1); - if (!bot.serverFeatures.hasNamespaces) { - final String[] splittedSpace = removedMessage.split("\\s+"); // [minecraft:test, arg1, arg2, ...] - final String[] splittedColon = splittedSpace[0].split(":"); // [minecraft, test] - if (splittedColon.length >= 2) { - removedMessage = String.join(":", Arrays.copyOfRange(splittedColon, 1, splittedColon.length)); - - if (splittedSpace.length > 1) { - removedMessage += " "; - removedMessage += String.join(" ", Arrays.copyOfRange(splittedSpace, 1, splittedSpace.length)); - } - } - } + final String removedMessage = + bot.serverFeatures.hasNamespaces ? + slashRemoved : + StringUtilities.removeNamespace(slashRemoved); sendCommandInstantly(removedMessage); } else { diff --git a/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java b/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java index f7155da4..99e63c2b 100644 --- a/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java +++ b/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java @@ -11,21 +11,22 @@ import java.util.Map; public class StringUtilities { public static String removeNamespace (String command) { - String removedCommand = command; + final StringBuilder removedCommand = new StringBuilder(command); - final String[] splittedSpace = removedCommand.split("\\s+"); // [minecraft:test, arg1, arg2, ...] - final String[] splittedColon = splittedSpace[0].split(":"); // [minecraft, test] + final String[] splitSpace = command.split("\\s+"); // [minecraft:test, arg1, arg2, ...] + final String[] splitColon = splitSpace[0].split(":"); // [minecraft, test] - if (splittedColon.length >= 2) { - removedCommand = String.join(":", Arrays.copyOfRange(splittedColon, 1, splittedColon.length)); + if (splitColon.length >= 2) { + removedCommand.setLength(0); + removedCommand.append(String.join(":", Arrays.copyOfRange(splitColon, 1, splitColon.length))); - if (splittedSpace.length > 1) { - removedCommand += " "; - removedCommand += String.join(" ", Arrays.copyOfRange(splittedSpace, 1, splittedSpace.length)); + if (splitSpace.length > 1) { + removedCommand.append(' '); + removedCommand.append(String.join(" ", Arrays.copyOfRange(splitSpace, 1, splitSpace.length))); } } - return removedCommand; + return removedCommand.toString(); } // https://stackoverflow.com/a/35148974/18518424