bug #5373 - fix AsyncChatEvent not being posted when processing a legacy APCE continuation

This commit is contained in:
Riley Park 2021-03-16 11:57:34 -07:00
parent 522ae1c51c
commit d4532f7e70
No known key found for this signature in database
GPG Key ID: D831AF236C834E45
2 changed files with 71 additions and 83 deletions

View File

@ -106,10 +106,10 @@ index 0000000000000000000000000000000000000000..a2acd31dce4461338a8baa96e03df36a
+} +}
diff --git a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java diff --git a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..8767c84e6417888cacf1ed76c7e3def73f873d70 index 0000000000000000000000000000000000000000..aa509a1f382991e0f5683b75b0af4094f84d2844
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java +++ b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java
@@ -0,0 +1,218 @@ @@ -0,0 +1,211 @@
+package io.papermc.paper.adventure; +package io.papermc.paper.adventure;
+ +
+import io.papermc.paper.chat.ChatFormatter; +import io.papermc.paper.chat.ChatFormatter;
@ -123,8 +123,6 @@ index 0000000000000000000000000000000000000000..8767c84e6417888cacf1ed76c7e3def7
+import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.TextReplacementConfig;
+import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.ClickEvent;
+import net.kyori.adventure.text.format.Style;
+import net.kyori.adventure.text.format.TextDecoration;
+import net.minecraft.server.EntityPlayer; +import net.minecraft.server.EntityPlayer;
+import net.minecraft.server.IChatBaseComponent; +import net.minecraft.server.IChatBaseComponent;
+import net.minecraft.server.MinecraftServer; +import net.minecraft.server.MinecraftServer;
@ -165,66 +163,80 @@ index 0000000000000000000000000000000000000000..8767c84e6417888cacf1ed76c7e3def7
+ this.async = async; + this.async = async;
+ } + }
+ +
+ @SuppressWarnings("deprecation") + @SuppressWarnings("CodeBlock2Expr")
+ public void process() { + public void process() {
+ final boolean listenersOnSyncEvent = anyListeners(ChatEvent.getHandlerList()); + this.processingLegacyFirst(
+
+ this.processLegacy(
+ // continuing from AsyncPlayerChatEvent (without PlayerChatEvent) + // continuing from AsyncPlayerChatEvent (without PlayerChatEvent)
+ event -> { + event -> {
+ final AsyncChatEvent ae = this.createAsync( + this.processModern(
+ legacyFormatter(event.getFormat(), legacyDisplayName((CraftPlayer) event.getPlayer()), event.getMessage()), + legacyFormatter(event.getFormat(), legacyDisplayName((CraftPlayer) event.getPlayer()), event.getMessage()),
+ event.getRecipients(), + event.getRecipients(),
+ PaperAdventure.LEGACY_SECTION_UXRC.deserialize(event.getMessage()) + PaperAdventure.LEGACY_SECTION_UXRC.deserialize(event.getMessage()),
+ event.isCancelled()
+ ); + );
+ ae.setCancelled(event.isCancelled()); // propagate cancelled state
+ post(ae);
+ if (listenersOnSyncEvent) {
+ this.continueWithSyncFromWhereAsyncLeftOff(ae);
+ } else {
+ this.complete(ae);
+ }
+ }, + },
+ // continuing from AsyncPlayerChatEvent and PlayerChatEvent + // continuing from AsyncPlayerChatEvent and PlayerChatEvent
+ event -> { + event -> {
+ this.queueIfAsyncOrRunImmediately(new Waitable<Void>() { + this.processModern(
+ @Override + legacyFormatter(event.getFormat(), legacyDisplayName((CraftPlayer) event.getPlayer()), event.getMessage()),
+ protected Void evaluate() { + event.getRecipients(),
+ final ChatEvent se = ChatProcessor.this.createSync( + PaperAdventure.LEGACY_SECTION_UXRC.deserialize(event.getMessage()),
+ legacyFormatter(event.getFormat(), legacyDisplayName((CraftPlayer) event.getPlayer()), event.getMessage()), + event.isCancelled()
+ event.getRecipients(), + );
+ PaperAdventure.LEGACY_SECTION_UXRC.deserialize(event.getMessage())
+ );
+ se.setCancelled(event.isCancelled()); // propagate cancelled state
+ post(se);
+ ChatProcessor.this.complete(se);
+ return null;
+ }
+ });
+ }, + },
+ // no legacy events called, all nice and fresh! + // no legacy events called, all nice and fresh!
+ () -> { + () -> {
+ final AsyncChatEvent ae = this.createAsync(ChatFormatter.DEFAULT, new LazyPlayerSet(this.server), Component.text(this.message).replaceText(URL_REPLACEMENT_CONFIG)); + this.processModern(
+ post(ae); + ChatFormatter.DEFAULT,
+ if (listenersOnSyncEvent) { + new LazyPlayerSet(this.server),
+ this.continueWithSyncFromWhereAsyncLeftOff(ae); + Component.text(this.message).replaceText(URL_REPLACEMENT_CONFIG),
+ } else { + false
+ this.complete(ae); + );
+ }
+ } + }
+ ); + );
+ } + }
+ +
+ private static String legacyDisplayName(final CraftPlayer player) { + @SuppressWarnings("deprecation")
+ return player.getDisplayName(); + private void processingLegacyFirst(
+ final Consumer<AsyncPlayerChatEvent> continueAfterAsync,
+ final Consumer<PlayerChatEvent> continueAfterAsyncAndSync,
+ final Runnable modernOnly
+ ) {
+ final boolean listenersOnAsyncEvent = anyListeners(AsyncPlayerChatEvent.getHandlerList());
+ final boolean listenersOnSyncEvent = anyListeners(PlayerChatEvent.getHandlerList());
+ if (listenersOnAsyncEvent || listenersOnSyncEvent) {
+ final CraftPlayer player = this.player.getBukkitEntity();
+ final AsyncPlayerChatEvent ae = new AsyncPlayerChatEvent(this.async, player, this.message, new LazyPlayerSet(this.server));
+ post(ae);
+ if (listenersOnSyncEvent) {
+ final PlayerChatEvent se = new PlayerChatEvent(player, ae.getMessage(), ae.getFormat(), ae.getRecipients());
+ se.setCancelled(ae.isCancelled()); // propagate cancelled state
+ this.queueIfAsyncOrRunImmediately(new Waitable<Void>() {
+ @Override
+ protected Void evaluate() {
+ post(se);
+ return null;
+ }
+ });
+ continueAfterAsyncAndSync.accept(se);
+ } else if (!ae.isCancelled()) {
+ continueAfterAsync.accept(ae);
+ }
+ } else {
+ modernOnly.run();
+ }
+ } + }
+ +
+ private static Component displayName(final CraftPlayer player) { + private void processModern(final ChatFormatter formatter, final Set<Player> recipients, final Component message, final boolean cancelled) {
+ return player.displayName(); + final AsyncChatEvent ae = this.createAsync(formatter, recipients, message);
+ } + ae.setCancelled(cancelled); // propagate cancelled state
+ + post(ae);
+ private static ChatFormatter legacyFormatter(final String format, final String legacyDisplayName, final String legacyMessage) { + final boolean listenersOnSyncEvent = anyListeners(ChatEvent.getHandlerList());
+ return (displayName, message) -> PaperAdventure.LEGACY_SECTION_UXRC.deserialize(String.format(format, legacyDisplayName, legacyMessage)).replaceText(URL_REPLACEMENT_CONFIG); + if (listenersOnSyncEvent) {
+ this.continueWithSyncFromWhereAsyncLeftOff(ae);
+ } else {
+ this.complete(ae);
+ }
+ } + }
+ +
+ private void continueWithSyncFromWhereAsyncLeftOff(final AsyncChatEvent ae) { + private void continueWithSyncFromWhereAsyncLeftOff(final AsyncChatEvent ae) {
@ -274,35 +286,16 @@ index 0000000000000000000000000000000000000000..8767c84e6417888cacf1ed76c7e3def7
+ return new ChatEvent(this.player.getBukkitEntity(), recipients, formatter, message); + return new ChatEvent(this.player.getBukkitEntity(), recipients, formatter, message);
+ } + }
+ +
+ @SuppressWarnings("deprecation") + private static String legacyDisplayName(final CraftPlayer player) {
+ public void processLegacy( + return player.getDisplayName();
+ final Consumer<AsyncPlayerChatEvent> continueAfterAsync, + }
+ final Consumer<PlayerChatEvent> continueAfterAsyncAndSync, +
+ final Runnable modernOnly + private static Component displayName(final CraftPlayer player) {
+ ) { + return player.displayName();
+ final boolean listenersOnAsyncEvent = anyListeners(AsyncPlayerChatEvent.getHandlerList()); + }
+ final boolean listenersOnSyncEvent = anyListeners(PlayerChatEvent.getHandlerList()); +
+ if (listenersOnAsyncEvent || listenersOnSyncEvent) { + private static ChatFormatter legacyFormatter(final String format, final String legacyDisplayName, final String legacyMessage) {
+ final CraftPlayer player = this.player.getBukkitEntity(); + return (displayName, message) -> PaperAdventure.LEGACY_SECTION_UXRC.deserialize(String.format(format, legacyDisplayName, legacyMessage)).replaceText(URL_REPLACEMENT_CONFIG);
+ final AsyncPlayerChatEvent ae = new AsyncPlayerChatEvent(this.async, player, this.message, new LazyPlayerSet(this.server));
+ post(ae);
+ if (listenersOnSyncEvent) {
+ final PlayerChatEvent se = new PlayerChatEvent(player, ae.getMessage(), ae.getFormat(), ae.getRecipients());
+ se.setCancelled(ae.isCancelled()); // propagate cancelled state
+ this.queueIfAsyncOrRunImmediately(new Waitable<Void>() {
+ @Override
+ protected Void evaluate() {
+ post(se);
+ return null;
+ }
+ });
+ continueAfterAsyncAndSync.accept(se);
+ } else if (!ae.isCancelled()) {
+ continueAfterAsync.accept(ae);
+ }
+ } else {
+ modernOnly.run();
+ }
+ } + }
+ +
+ private void queueIfAsyncOrRunImmediately(final Waitable<Void> waitable) { + private void queueIfAsyncOrRunImmediately(final Waitable<Void> waitable) {

View File

@ -26,15 +26,10 @@ index db2dddd12f54e6d15916c4cee623676541de37fb..1942f5224aaebb18adb591d6f70a419c
+ } + }
} }
diff --git a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java diff --git a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java
index 8767c84e6417888cacf1ed76c7e3def73f873d70..7a5ac38c490f5b886a0611b01bd36053bc54a85a 100644 index aa509a1f382991e0f5683b75b0af4094f84d2844..e8d699b01a3bae60720c81e4427a310e2a2b1640 100644
--- a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java --- a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java
+++ b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java +++ b/src/main/java/io/papermc/paper/adventure/ChatProcessor.java
@@ -11,12 +11,14 @@ import java.util.regex.Pattern; @@ -14,7 +14,11 @@ import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
-import net.kyori.adventure.text.format.Style;
-import net.kyori.adventure.text.format.TextDecoration;
import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPlayer;
import net.minecraft.server.IChatBaseComponent; import net.minecraft.server.IChatBaseComponent;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
@ -46,7 +41,7 @@ index 8767c84e6417888cacf1ed76c7e3def73f873d70..7a5ac38c490f5b886a0611b01bd36053
import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.util.LazyPlayerSet; import org.bukkit.craftbukkit.util.LazyPlayerSet;
import org.bukkit.craftbukkit.util.Waitable; import org.bukkit.craftbukkit.util.Waitable;
@@ -104,10 +106,22 @@ public final class ChatProcessor { @@ -175,10 +179,22 @@ public final class ChatProcessor {
} }
private static String legacyDisplayName(final CraftPlayer player) { private static String legacyDisplayName(final CraftPlayer player) {