Skip to content

Commit 9d606ef

Browse files
Chat Preview API
1 parent 772760a commit 9d606ef

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: BillyGalbreath <blake.galbreath@gmail.com>
3+
Date: Mon, 11 Jul 2022 21:09:14 -0500
4+
Subject: [PATCH] Chat Preview API
5+
6+
7+
diff --git a/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java b/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java
8+
new file mode 100644
9+
index 0000000000000000000000000000000000000000..502896f69a139c9524625a2c0d9029f85276146b
10+
--- /dev/null
11+
+++ b/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java
12+
@@ -0,0 +1,90 @@
13+
+package org.purpurmc.purpur.event.player;
14+
+
15+
+import net.kyori.adventure.text.Component;
16+
+import org.bukkit.Bukkit;
17+
+import org.bukkit.entity.Player;
18+
+import org.bukkit.event.Cancellable;
19+
+import org.bukkit.event.HandlerList;
20+
+import org.bukkit.event.player.PlayerEvent;
21+
+import org.jetbrains.annotations.NotNull;
22+
+import org.jetbrains.annotations.Nullable;
23+
+
24+
+public class PlayerPreviewChatEvent extends PlayerEvent implements Cancellable {
25+
+ private static final HandlerList handlers = new HandlerList();
26+
+ private final String originalQuery;
27+
+ private Component query;
28+
+
29+
+ public PlayerPreviewChatEvent(@NotNull Player who, @NotNull String originalQuery, @Nullable Component query) {
30+
+ super(who, !Bukkit.isPrimaryThread());
31+
+ this.originalQuery = originalQuery;
32+
+ this.query = query;
33+
+ }
34+
+
35+
+ /**
36+
+ * The original query string as sent from the client
37+
+ *
38+
+ * @return The original query string
39+
+ */
40+
+ @NotNull
41+
+ public String getOriginalQuery() {
42+
+ return originalQuery;
43+
+ }
44+
+
45+
+ /**
46+
+ * Get the current query.
47+
+ * <p>
48+
+ * Null queries represent "no changes".
49+
+ *
50+
+ * @return Current query
51+
+ */
52+
+ @Nullable
53+
+ public Component getQuery() {
54+
+ return this.query;
55+
+ }
56+
+
57+
+ /**
58+
+ * Get the current query.
59+
+ * <p>
60+
+ * Null queries represent "no changes".
61+
+ *
62+
+ * @param query The current query
63+
+ */
64+
+ public void setQuery(@Nullable Component query) {
65+
+ this.query = query;
66+
+ }
67+
+
68+
+ /**
69+
+ * Gets the cancellation state of this event.
70+
+ * <p>
71+
+ * A cancelled event tells the client there are "no changes"
72+
+ * to the chat, the same as setting the query to null.
73+
+ *
74+
+ * @return true if this event is cancelled
75+
+ */
76+
+ @Override
77+
+ public boolean isCancelled() {
78+
+ return this.query == null;
79+
+ }
80+
+
81+
+ /**
82+
+ * Sets the cancellation state of this event.
83+
+ * <p>
84+
+ * A cancelled event tells the client there are "no changes"
85+
+ * to the chat, the same as setting the query to null.
86+
+ */
87+
+ @Override
88+
+ public void setCancelled(boolean cancel) {
89+
+ this.query = null;
90+
+ }
91+
+
92+
+ @Override
93+
+ @NotNull
94+
+ public HandlerList getHandlers() {
95+
+ return handlers;
96+
+ }
97+
+
98+
+ @NotNull
99+
+ public static HandlerList getHandlerList() {
100+
+ return handlers;
101+
+ }
102+
+}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: BillyGalbreath <blake.galbreath@gmail.com>
3+
Date: Mon, 11 Jul 2022 20:44:19 -0500
4+
Subject: [PATCH] Chat Preview API
5+
6+
7+
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
8+
index 3bb51f34be4341c991487a78d8793f074ffdcba4..31262e450e777a69b07faadfea7534cd39bd90a0 100644
9+
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
10+
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
11+
@@ -2310,7 +2310,12 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
12+
}
13+
14+
private void broadcastChatMessage(ServerboundChatPacket packetplayinchat, FilteredText<PlayerChatMessage> filteredtext) {
15+
- if (!((PlayerChatMessage) filteredtext.raw()).verify(this.player)) {
16+
+ /* Purpur start
17+
+ * I'm not real sure how else to handle this right now. so I just skip the
18+
+ * signature verification if this is from a client previewed message.
19+
+ * Sadly, this opens up the possibility for modded clients to lie here. */
20+
+ if (!packetplayinchat.signedPreview() && !((PlayerChatMessage) filteredtext.raw()).verify(this.player)) {
21+
+ // Purpur end
22+
ServerGamePacketListenerImpl.LOGGER.warn("{} sent message with invalid signature: '{}'", this.player.getName().getString(), ((PlayerChatMessage) filteredtext.raw()).signedContent().getString());
23+
} else {
24+
// CraftBukkit start
25+
@@ -2512,7 +2517,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
26+
27+
@Override
28+
public void handleChatPreview(ServerboundChatPreviewPacket packet) {
29+
- if (false && this.server.previewsChat()) { // CraftBukkit - preview NYI
30+
+ if (this.server.previewsChat()) { // CraftBukkit - preview NYI // Purpur - lets implement it already.. sheesh
31+
this.chatPreviewThrottler.schedule(() -> {
32+
int i = packet.queryId();
33+
String s = packet.query();
34+
@@ -2543,6 +2548,17 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
35+
private CompletableFuture<Component> queryChatPreview(String query) {
36+
MutableComponent ichatmutablecomponent = Component.literal(query);
37+
38+
+ // Purpur start
39+
+ net.kyori.adventure.text.Component adventureComponent = PaperAdventure.asAdventure(ichatmutablecomponent);
40+
+ org.purpurmc.purpur.event.player.PlayerPreviewChatEvent previewEvent = new org.purpurmc.purpur.event.player.PlayerPreviewChatEvent(getPlayer().getBukkitEntity(), query, adventureComponent);
41+
+ if (!previewEvent.callEvent()) {
42+
+ return CompletableFuture.completedFuture(null);
43+
+ }
44+
+ if (!previewEvent.getQuery().equals(adventureComponent)) {
45+
+ return CompletableFuture.completedFuture(PaperAdventure.asVanilla(previewEvent.getQuery()));
46+
+ }
47+
+ // Purpur end
48+
+
49+
return this.server.getChatDecorator().decorate(this.player, ichatmutablecomponent).thenApply((ichatbasecomponent) -> {
50+
return !ichatmutablecomponent.equals(ichatbasecomponent) ? ichatbasecomponent : null;
51+
});

0 commit comments

Comments
 (0)