Expose signed message in chat events (#8694)

This commit is contained in:
Noah van der Aa 2022-12-23 16:54:04 +01:00
parent 424df4f743
commit c2bee2c705
2 changed files with 27 additions and 8 deletions

View File

@ -204,15 +204,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ @@ -0,0 +0,0 @@
+package io.papermc.paper.event.player; +package io.papermc.paper.event.player;
+ +
+import java.util.Set;
+import io.papermc.paper.chat.ChatRenderer; +import io.papermc.paper.chat.ChatRenderer;
+import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.Audience;
+import net.kyori.adventure.chat.SignedMessage;
+import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.Component;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable; +import org.bukkit.event.Cancellable;
+import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerEvent;
+import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.NotNull;
+ +
+import java.util.Set;
+
+import static java.util.Objects.requireNonNull; +import static java.util.Objects.requireNonNull;
+ +
+/** +/**
@ -221,16 +223,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+public abstract class AbstractChatEvent extends PlayerEvent implements Cancellable { +public abstract class AbstractChatEvent extends PlayerEvent implements Cancellable {
+ private final Set<Audience> viewers; + private final Set<Audience> viewers;
+ private final Component originalMessage; + private final Component originalMessage;
+ private final SignedMessage signedMessage;
+ private ChatRenderer renderer; + private ChatRenderer renderer;
+ private Component message; + private Component message;
+ private boolean cancelled = false; + private boolean cancelled = false;
+ +
+ AbstractChatEvent(final boolean async, final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage) { + AbstractChatEvent(final boolean async, final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage, final @NotNull SignedMessage signedMessage) {
+ super(player, async); + super(player, async);
+ this.viewers = viewers; + this.viewers = viewers;
+ this.renderer = renderer; + this.renderer = renderer;
+ this.message = message; + this.message = message;
+ this.originalMessage = originalMessage; + this.originalMessage = originalMessage;
+ this.signedMessage = signedMessage;
+ } + }
+ +
+ /** + /**
@ -304,6 +308,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return this.originalMessage; + return this.originalMessage;
+ } + }
+ +
+ /**
+ * Gets the signed message.
+ * Changes made in this event will <b>not</b> update
+ * the signed message.
+ *
+ * @return the signed message
+ */
+ @NotNull
+ public final SignedMessage signedMessage() {
+ return this.signedMessage;
+ }
+
+ @Override + @Override
+ public final boolean isCancelled() { + public final boolean isCancelled() {
+ return this.cancelled; + return this.cancelled;
@ -481,6 +497,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.util.Set; +import java.util.Set;
+import io.papermc.paper.chat.ChatRenderer; +import io.papermc.paper.chat.ChatRenderer;
+import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.Audience;
+import net.kyori.adventure.chat.SignedMessage;
+import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.Component;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList; +import org.bukkit.event.HandlerList;
@ -492,8 +509,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+public final class AsyncChatEvent extends AbstractChatEvent { +public final class AsyncChatEvent extends AbstractChatEvent {
+ private static final HandlerList HANDLERS = new HandlerList(); + private static final HandlerList HANDLERS = new HandlerList();
+ +
+ public AsyncChatEvent(final boolean async, final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage) { + public AsyncChatEvent(final boolean async, final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage, final @NotNull SignedMessage signedMessage) {
+ super(async, player, viewers, renderer, message, originalMessage); + super(async, player, viewers, renderer, message, originalMessage, signedMessage);
+ } + }
+ +
+ @NotNull + @NotNull
@ -518,6 +535,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.util.Set; +import java.util.Set;
+import io.papermc.paper.chat.ChatRenderer; +import io.papermc.paper.chat.ChatRenderer;
+import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.Audience;
+import net.kyori.adventure.chat.SignedMessage;
+import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.Component;
+import org.bukkit.Warning; +import org.bukkit.Warning;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
@ -534,8 +552,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+public final class ChatEvent extends AbstractChatEvent { +public final class ChatEvent extends AbstractChatEvent {
+ private static final HandlerList HANDLERS = new HandlerList(); + private static final HandlerList HANDLERS = new HandlerList();
+ +
+ public ChatEvent(final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage) { + public ChatEvent(final @NotNull Player player, final @NotNull Set<Audience> viewers, final @NotNull ChatRenderer renderer, final @NotNull Component message, final @NotNull Component originalMessage, final @NotNull SignedMessage signedMessage) {
+ super(false, player, viewers, renderer, message, originalMessage); + super(false, player, viewers, renderer, message, originalMessage, signedMessage);
+ } + }
+ +
+ @NotNull + @NotNull

View File

@ -427,7 +427,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ +
+ private void processModern(final ChatRenderer renderer, final Set<Audience> viewers, final Component message, final Player player, final boolean cancelled) { + private void processModern(final ChatRenderer renderer, final Set<Audience> viewers, final Component message, final Player player, final boolean cancelled) {
+ final AsyncChatEvent ae = new AsyncChatEvent(this.async, player, viewers, renderer, message, this.paper$originalMessage); + final PlayerChatMessage.AdventureView signedMessage = this.message.adventureView();
+ final AsyncChatEvent ae = new AsyncChatEvent(this.async, player, viewers, renderer, message, this.paper$originalMessage, signedMessage);
+ ae.setCancelled(cancelled); // propagate cancelled state + ae.setCancelled(cancelled); // propagate cancelled state
+ this.post(ae); + this.post(ae);
+ final boolean listenersOnSyncEvent = canYouHearMe(ChatEvent.getHandlerList()); + final boolean listenersOnSyncEvent = canYouHearMe(ChatEvent.getHandlerList());
@ -435,7 +436,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ this.queueIfAsyncOrRunImmediately(new Waitable<Void>() { + this.queueIfAsyncOrRunImmediately(new Waitable<Void>() {
+ @Override + @Override
+ protected Void evaluate() { + protected Void evaluate() {
+ final ChatEvent se = new ChatEvent(player, ae.viewers(), ae.renderer(), ae.message(), ChatProcessor.this.paper$originalMessage/*, ae.usePreviewComponent()*/); + final ChatEvent se = new ChatEvent(player, ae.viewers(), ae.renderer(), ae.message(), ChatProcessor.this.paper$originalMessage/*, ae.usePreviewComponent()*/, signedMessage);
+ se.setCancelled(ae.isCancelled()); // propagate cancelled state + se.setCancelled(ae.isCancelled()); // propagate cancelled state
+ ChatProcessor.this.post(se); + ChatProcessor.this.post(se);
+ ChatProcessor.this.readModernModifications(se, renderer); + ChatProcessor.this.readModernModifications(se, renderer);