From bcadfcdadbeb0351635e867fb49f9815061c2159 Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 10:46:16 +0200 Subject: [PATCH 1/3] Resolve #218 --- .../chatchat/command/SwitchChannelCommand.java | 2 +- .../helpch/chatchat/listener/ChatListener.java | 14 +++++++++++++- .../helpch/chatchat/util/MessageProcessor.java | 18 +++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java index 9d0b038c..4784f6dc 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java @@ -57,6 +57,6 @@ public void switchChannel(final ChatUser user, @Join @Optional @NotNull final St return; } - MessageProcessor.process(plugin, user, channel, message, false); + MessageProcessor.process(plugin, user, channel, user.channel(), message, false); } } diff --git a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java index 9b096640..7b894a82 100644 --- a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java +++ b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java @@ -73,6 +73,11 @@ public void onChat(final AsyncPlayerChatEvent event) { final var consoleFormat = plugin.configManager().formats().consoleFormat(); + final var oldChannel = user.channel(); + + // We switch the user to the channel here so that the console can parse the correct channel prefix + user.channel(channel); + event.setMessage(LegacyComponentSerializer.legacySection().serialize( MessageProcessor.processMessage(plugin, user, ConsoleUser.INSTANCE, message) )); @@ -96,7 +101,14 @@ public void onChat(final AsyncPlayerChatEvent event) { // Cancel the event if the message doesn't end up being sent // This only happens if the message contains illegal characters or if the ChatChatEvent is canceled. - event.setCancelled(!MessageProcessor.process(plugin, user, channel, message, event.isAsynchronous())); + boolean cancelled = MessageProcessor.process(plugin, user, channel, oldChannel, message, event.isAsynchronous()); + + event.setCancelled(!cancelled); + + // If the event was cancelled, we need to reset the user back to their old channel + if(cancelled) { + user.channel(oldChannel); + } } private static String cleanseMessage(@NotNull final String message) { diff --git a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java index 3aec1eeb..5fd7d3c9 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java @@ -59,10 +59,23 @@ private MessageProcessor() { throw new AssertionError("Util classes are not to be instantiated!"); } + /** + * Process a message for a user and send it to the recipients. + * @param plugin The plugin instance. + * @param user The user sending the message. + * @param channel The channel the user is sending the message to. + * @param previousChannel The previous channel the user was in. Used to switch back to after sending the message. + * Used to switch back when sending a message to a channel with a prefix. + * @param message The message to send. + * @param async Whether to process the message asynchronously. + * + * @return Whether the message was sent successfully. + */ public static boolean process( @NotNull final ChatChatPlugin plugin, @NotNull final ChatUser user, @NotNull final Channel channel, + @NotNull final Channel previousChannel, @NotNull final String message, final boolean async ) { @@ -103,7 +116,6 @@ public static boolean process( return false; } - final var oldChannel = user.channel(); user.channel(channel); final var parsedMessage = chatEvent.message().compact(); @@ -173,7 +185,7 @@ public static boolean process( } if (!userIsTarget) { - user.channel(oldChannel); + user.channel(previousChannel); return true; } @@ -199,7 +211,7 @@ public static boolean process( user.playSound(mentions.sound()); } - user.channel(oldChannel); + user.channel(previousChannel); return true; } From 85eee876d30da12c5818678fb5ae5e5208990bf5 Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 20:30:12 +0200 Subject: [PATCH 2/3] Let MessageProcessor#process callers handle channel switching. --- .../helpch/chatchat/command/SwitchChannelCommand.java | 2 +- .../java/at/helpch/chatchat/listener/ChatListener.java | 10 ++-------- .../java/at/helpch/chatchat/util/MessageProcessor.java | 5 ----- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java index 4784f6dc..9d0b038c 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java @@ -57,6 +57,6 @@ public void switchChannel(final ChatUser user, @Join @Optional @NotNull final St return; } - MessageProcessor.process(plugin, user, channel, user.channel(), message, false); + MessageProcessor.process(plugin, user, channel, message, false); } } diff --git a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java index 7b894a82..2f72b42d 100644 --- a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java +++ b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java @@ -101,14 +101,8 @@ public void onChat(final AsyncPlayerChatEvent event) { // Cancel the event if the message doesn't end up being sent // This only happens if the message contains illegal characters or if the ChatChatEvent is canceled. - boolean cancelled = MessageProcessor.process(plugin, user, channel, oldChannel, message, event.isAsynchronous()); - - event.setCancelled(!cancelled); - - // If the event was cancelled, we need to reset the user back to their old channel - if(cancelled) { - user.channel(oldChannel); - } + event.setCancelled(!MessageProcessor.process(plugin, user, channel, message, event.isAsynchronous())); + user.channel(oldChannel); } private static String cleanseMessage(@NotNull final String message) { diff --git a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java index 5fd7d3c9..4daa79b9 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java @@ -64,8 +64,6 @@ private MessageProcessor() { * @param plugin The plugin instance. * @param user The user sending the message. * @param channel The channel the user is sending the message to. - * @param previousChannel The previous channel the user was in. Used to switch back to after sending the message. - * Used to switch back when sending a message to a channel with a prefix. * @param message The message to send. * @param async Whether to process the message asynchronously. * @@ -75,7 +73,6 @@ public static boolean process( @NotNull final ChatChatPlugin plugin, @NotNull final ChatUser user, @NotNull final Channel channel, - @NotNull final Channel previousChannel, @NotNull final String message, final boolean async ) { @@ -185,7 +182,6 @@ public static boolean process( } if (!userIsTarget) { - user.channel(previousChannel); return true; } @@ -211,7 +207,6 @@ public static boolean process( user.playSound(mentions.sound()); } - user.channel(previousChannel); return true; } From ba14975aca13621d2537ca8cf6f61e7447daff72 Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 20:33:21 +0200 Subject: [PATCH 3/3] Revert old logic. --- .../main/java/at/helpch/chatchat/util/MessageProcessor.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java index 4daa79b9..ca8a32f2 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java @@ -113,6 +113,7 @@ public static boolean process( return false; } + final var oldChannel = user.channel(); user.channel(channel); final var parsedMessage = chatEvent.message().compact(); @@ -182,6 +183,7 @@ public static boolean process( } if (!userIsTarget) { + user.channel(oldChannel); return true; } @@ -207,6 +209,7 @@ public static boolean process( user.playSound(mentions.sound()); } + user.channel(oldChannel); return true; }