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 797fc746d0
commit 8b1e661370
2 changed files with 67 additions and 79 deletions

View file

@ -123,8 +123,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+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..00000000000000000000000000000000
+ 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..00000000000000000000000000000000
+ 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

@ -29,12 +29,7 @@ diff --git a/src/main/java/io/papermc/paper/adventure/ChatProcessor.java b/src/m
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 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
@@ -0,0 +0,0 @@ import java.util.regex.Pattern; @@ -0,0 +0,0 @@ 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;