fix: AsyncChatHookEvent not working correctly

This commit is contained in:
darksaid98 committed 2024-07-11 14:36:15 +02:00
1 parent 1bb5a5430b
commit 5ec0f7836e
2 files changed
+38 -52

No files matched your search

@@ -107,32 +107,47 @@ public class StandardChannel extends Channel {
// Get recipients // Get recipients
final Set<Player> recipients = getRecipients(p, town, nation, channelType); final Set<Player> 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. // Try sending an alone message if the player is alone in a channel.
trySendingAloneMessage(p, recipients); trySendingAloneMessage(p, recipients);
// Calculate recipients and set recipients // Calculate recipients and set recipients
e.message(Component.empty()); e.message(finalMessage);
e.viewers().clear(); e.viewers().clear();
e.viewers().add(recipientAudience); e.viewers().add(Audience.audience(recipients));
e.viewers().add(Bukkit.getServer().getConsoleSender());
e.renderer(
(source, sourceDisplayName, message, viewer) -> finalMessage
);
// If the server has marked this Channel as hooked, fire the AsyncChatHookEvent. // If the server has marked this Channel as hooked, fire the AsyncChatHookEvent.
// If the event is cancelled, cancel the chat entirely. // If the event is cancelled, cancel the chat entirely.
// Fires its own sendSpyMessage(). // Fires its own sendSpyMessage().
if (isHooked()) if (isHooked()) {
if (!sendOffHookedMessage(e, channelType, recipients)) { AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(e, this, recipients);
Bukkit.getServer().getPluginManager().callEvent(hookEvent);
if (hookEvent.isCancelled()) {
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
// Send spy message if this was never hooked. e.viewers().clear();
if (!isHooked()) 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); 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. // Play the channel sound, if used.
tryPlayChannelSound(recipients); tryPlayChannelSound(recipients);
@@ -220,34 +235,6 @@ public class StandardChannel extends Channel {
TownyMessaging.sendMessage(sender, ChatSettings.getUsingAloneMessageString()); 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<Player> 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. * Sends messages to spies who have not already seen the message naturally.
* *
@@ -32,6 +32,7 @@ import java.util.Set;
*/ */
public class AsyncChatHookEvent extends Event implements Cancellable { public class AsyncChatHookEvent extends Event implements Cancellable {
private static final HandlerList HANDLER_LIST = new HandlerList(); private static final HandlerList HANDLER_LIST = new HandlerList();
private String legacyMessage;
private boolean cancelled; private boolean cancelled;
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
@@ -50,20 +51,18 @@ public class AsyncChatHookEvent extends Event implements Cancellable {
@Override @Override
public void setCancelled(boolean cancelled) { public void setCancelled(boolean cancelled) {
changed = cancelled;
this.cancelled = cancelled; this.cancelled = cancelled;
} }
private final AsyncChatEvent event; private final AsyncChatEvent event;
private boolean changed;
private final Channel channel; private final Channel channel;
private final Set<Player> recipients; private final Set<Player> recipients;
public AsyncChatHookEvent(AsyncChatEvent event, Channel channel, Set<Player> recipients) { public AsyncChatHookEvent(AsyncChatEvent e, Channel channel, Set<Player> recipients) {
super(event.isAsynchronous()); super(e.isAsynchronous());
this.event = event; this.event = e;
this.changed = false; this.cancelled = e.isCancelled();
this.cancelled = event.isCancelled(); this.legacyMessage = Adventure.plainText().serialize(message());
this.channel = channel; this.channel = channel;
this.recipients = recipients; this.recipients = recipients;
@@ -84,12 +83,12 @@ public class AsyncChatHookEvent extends Event implements Cancellable {
@Deprecated @Deprecated
public String getMessage() { public String getMessage() {
return Adventure.plainText().serialize(message()); return legacyMessage;
} }
@Deprecated @Deprecated
public void setMessage(String message) { public void setMessage(String message) {
message(ColorParser.of(message).parseLegacy().build()); legacyMessage = message;
} }
public Component message() { public Component message() {
@@ -97,7 +96,6 @@ public class AsyncChatHookEvent extends Event implements Cancellable {
} }
public void message(Component message) { public void message(Component message) {
changed = true;
event.message(message); event.message(message);
} }
@@ -106,7 +104,6 @@ public class AsyncChatHookEvent extends Event implements Cancellable {
} }
public void setRecipients(Set<Player> recipients) { public void setRecipients(Set<Player> recipients) {
changed = true;
this.recipients.clear(); this.recipients.clear();
this.recipients.addAll(recipients); this.recipients.addAll(recipients);
} }
@@ -122,14 +119,16 @@ public class AsyncChatHookEvent extends Event implements Cancellable {
/* /*
* Returns true if the hooked event was changed * Returns true if the hooked event was changed
*/ */
@Deprecated(forRemoval = true)
public boolean isChanged() { public boolean isChanged() {
return changed; return true;
} }
/* /*
* Informs Chat if the event was changed or not * Informs Chat if the event was changed or not
*/ */
@Deprecated(forRemoval = true)
public void setChanged(boolean changed) { public void setChanged(boolean changed) {
this.changed = changed;
} }
} }