From 5ec0f7836e9b3b0137cfb63fdf571bea00d630d7 Mon Sep 17 00:00:00 2001 From: darksaid98 Date: Thu, 11 Jul 2024 14:36:15 +0200 Subject: [PATCH] fix: AsyncChatHookEvent not working correctly --- .../TownyChat/channels/StandardChannel.java | 65 ++++++++----------- .../TownyChat/events/AsyncChatHookEvent.java | 25 ++++--- 2 files changed, 38 insertions(+), 52 deletions(-) diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index eadfedc..00cfd37 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -107,32 +107,47 @@ public class StandardChannel extends Channel { // Get recipients final Set recipients = getRecipients(p, town, nation, channelType); - final Audience recipientAudience = Audience.audience(recipients); // Try sending an alone message if the player is alone in a channel. trySendingAloneMessage(p, recipients); // Calculate recipients and set recipients - e.message(Component.empty()); + e.message(finalMessage); e.viewers().clear(); - e.viewers().add(recipientAudience); - e.viewers().add(Bukkit.getServer().getConsoleSender()); - e.renderer( - (source, sourceDisplayName, message, viewer) -> finalMessage - ); + e.viewers().add(Audience.audience(recipients)); // If the server has marked this Channel as hooked, fire the AsyncChatHookEvent. // If the event is cancelled, cancel the chat entirely. // Fires its own sendSpyMessage(). - if (isHooked()) - if (!sendOffHookedMessage(e, channelType, recipients)) { + if (isHooked()) { + AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(e, this, recipients); + Bukkit.getServer().getPluginManager().callEvent(hookEvent); + if (hookEvent.isCancelled()) { e.setCancelled(true); return; } - // Send spy message if this was never hooked. - if (!isHooked()) + e.viewers().clear(); + e.viewers().add(Audience.audience(hookEvent.getRecipients())); + + /* + * Send spy message before another plugin changes any of the recipients, so we + * know which people can see it. + */ sendSpyMessage(e, channelType, recipients); + } + + // Add console as listener and set custom rendered for message + e.viewers().add(Bukkit.getServer().getConsoleSender()); + e.renderer( + (source, sourceDisplayName, message, viewer) -> e.message() + ); + + /* + * Send spy message before another plugin changes any of the recipients, so we + * know which people can see it. + */ + sendSpyMessage(e, channelType, recipients); // Play the channel sound, if used. tryPlayChannelSound(recipients); @@ -220,34 +235,6 @@ public class StandardChannel extends Channel { TownyMessaging.sendMessage(sender, ChatSettings.getUsingAloneMessageString()); } - /** - * Send off TownyChat's {@link AsyncChatHookEvent} which allows other plugins to - * cancel or modify TownyChat's messaging. - * - * @param e {@link AsyncChatEvent} that has caused a message. - * @param channelType {@link ChannelTypes} which this message is being sent through. - * @return false if the AsyncChatHookEvent is cancelled. - */ - private boolean sendOffHookedMessage(AsyncChatEvent e, ChannelTypes channelType, Set recipients) { - AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(e, this, recipients); - Bukkit.getServer().getPluginManager().callEvent(hookEvent); - if (hookEvent.isCancelled()) - return false; - - /* - * Send spy message before another plugin changes any of the recipients, so we - * know which people can see it. - */ - sendSpyMessage(e, channelType, recipients); - - if (hookEvent.isChanged()) { - e.message(hookEvent.message()); - e.viewers().clear(); - e.viewers().addAll(hookEvent.getRecipients()); - } - return true; - } - /** * Sends messages to spies who have not already seen the message naturally. * diff --git a/src/com/palmergames/bukkit/TownyChat/events/AsyncChatHookEvent.java b/src/com/palmergames/bukkit/TownyChat/events/AsyncChatHookEvent.java index 5a615f0..cf8061a 100644 --- a/src/com/palmergames/bukkit/TownyChat/events/AsyncChatHookEvent.java +++ b/src/com/palmergames/bukkit/TownyChat/events/AsyncChatHookEvent.java @@ -32,6 +32,7 @@ import java.util.Set; */ public class AsyncChatHookEvent extends Event implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); + private String legacyMessage; private boolean cancelled; public static HandlerList getHandlerList() { @@ -50,20 +51,18 @@ public class AsyncChatHookEvent extends Event implements Cancellable { @Override public void setCancelled(boolean cancelled) { - changed = cancelled; this.cancelled = cancelled; } private final AsyncChatEvent event; - private boolean changed; private final Channel channel; private final Set recipients; - public AsyncChatHookEvent(AsyncChatEvent event, Channel channel, Set recipients) { - super(event.isAsynchronous()); - this.event = event; - this.changed = false; - this.cancelled = event.isCancelled(); + public AsyncChatHookEvent(AsyncChatEvent e, Channel channel, Set recipients) { + super(e.isAsynchronous()); + this.event = e; + this.cancelled = e.isCancelled(); + this.legacyMessage = Adventure.plainText().serialize(message()); this.channel = channel; this.recipients = recipients; @@ -84,12 +83,12 @@ public class AsyncChatHookEvent extends Event implements Cancellable { @Deprecated public String getMessage() { - return Adventure.plainText().serialize(message()); + return legacyMessage; } @Deprecated public void setMessage(String message) { - message(ColorParser.of(message).parseLegacy().build()); + legacyMessage = message; } public Component message() { @@ -97,7 +96,6 @@ public class AsyncChatHookEvent extends Event implements Cancellable { } public void message(Component message) { - changed = true; event.message(message); } @@ -106,7 +104,6 @@ public class AsyncChatHookEvent extends Event implements Cancellable { } public void setRecipients(Set recipients) { - changed = true; this.recipients.clear(); this.recipients.addAll(recipients); } @@ -122,14 +119,16 @@ public class AsyncChatHookEvent extends Event implements Cancellable { /* * Returns true if the hooked event was changed */ + @Deprecated(forRemoval = true) public boolean isChanged() { - return changed; + return true; } /* * Informs Chat if the event was changed or not */ + @Deprecated(forRemoval = true) public void setChanged(boolean changed) { - this.changed = changed; + } }