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
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.
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<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.
*
@@ -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<Player> recipients;
public AsyncChatHookEvent(AsyncChatEvent event, Channel channel, Set<Player> recipients) {
super(event.isAsynchronous());
this.event = event;
this.changed = false;
this.cancelled = event.isCancelled();
public AsyncChatHookEvent(AsyncChatEvent e, Channel channel, Set<Player> 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<Player> 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;
}
}