From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Nassim Jahnke <nassim@njahnke.dev>
Date: Wed, 8 Jun 2022 21:30:34 +0200
Subject: [PATCH] Untrash chat handling

Vanilla technically allows chat messages with starting slashes now,
Spigot still accepts them as commands, most likely due to being too
lazy to properly differentiate between chat and command intent in
their implementation. This disallows modified clients to send chat
messages with slashes and makes sure chat validation always happens
on the netty event loop, rather than there and possibly being moved
to the main thread, thus having the delayed handling cause a bad
process order of message ids.

diff --git a/src/main/java/net/minecraft/network/protocol/game/ServerboundChatPacket.java b/src/main/java/net/minecraft/network/protocol/game/ServerboundChatPacket.java
index 3825aa2c381a3ee77e05bea520ff5fb828733857..4e9832d5753b98621e68246ffc5d80c86a3b5ed3 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ServerboundChatPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundChatPacket.java
@@ -40,24 +40,16 @@ public class ServerboundChatPacket implements Packet<ServerGamePacketListener> {
     }
 
     // Spigot Start
-    private static final java.util.concurrent.ExecutorService executors = java.util.concurrent.Executors.newCachedThreadPool(
+    // Paper start - untrash chat event handling
+    public static final java.util.concurrent.ExecutorService executors = java.util.concurrent.Executors.newCachedThreadPool(
             new com.google.common.util.concurrent.ThreadFactoryBuilder().setDaemon( true ).setNameFormat( "Async Chat Thread - #%d" ).setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER)).build() ); // Paper
     public void handle(final ServerGamePacketListener listener) {
-        if ( !this.message.startsWith("/") )
-        {
-            ServerboundChatPacket.executors.execute( new Runnable() // Paper - Use #execute to propagate exceptions up instead of swallowing them
-            {
-
-                @Override
-                public void run()
-                {
-                    listener.handleChat( ServerboundChatPacket.this );
-                }
-            } );
+        if (this.message.startsWith("/")) {
+            // Just don't allow this instead of creating gigantic diff when untrashing more Spigot code
             return;
         }
-        // Spigot End
         listener.handleChat(this);
+        // Paper end - untrash chat event handling
     }
 
     public String getMessage() {
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 28c76e7b3260ce56421057b88c3555f18b29da06..8e6045c51c46b8d8fa6f4b7fbe4b9478693bac82 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2104,11 +2104,6 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
 
     @Override
     public void handleChat(ServerboundChatPacket packet) {
-        // CraftBukkit start - async chat
-        // SPIGOT-3638
-        if (this.server.isStopped()) {
-            return;
-        }
         // CraftBukkit end
         if (ServerGamePacketListenerImpl.isChatMessageIllegal(packet.getMessage())) {
             this.server.scheduleOnMain(() -> { // Paper - push to main for event firing
@@ -2118,8 +2113,15 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
             if (this.tryHandleChat(packet.getMessage(), packet.getTimeStamp())) {
                 // CraftBukkit start
                 // this.filterTextPacket(packetplayinchat.getMessage(), (filteredtext) -> {
+                // Paper start - untrash chat handling
+                ServerboundChatPacket.executors.execute(() -> {
+                    if (this.server.isStopped()) {
+                        return;
+                    }
                     this.handleChat(packet, FilteredText.passThrough(packet.getMessage())); // CraftBukkit - filter NYI
                 // });
+                });
+                // Paper end - untrash chat handling
                 // CraftBukkit end
             }
 
@@ -2133,8 +2135,9 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
             this.disconnect(Component.translatable("multiplayer.disconnect.illegal_characters"));
             }); // Paper - push to main for event firing
         } else {
-            PacketUtils.ensureRunningOnSameThread(packet, this, this.player.getLevel());
+            // Paper start - untrash chat handling
             if (this.tryHandleChat(packet.command(), packet.timeStamp())) {
+                this.server.scheduleOnMain(() -> {
                 // CraftBukkit start
                 // CommandListenerWrapper commandlistenerwrapper = this.player.createCommandSourceStack().withSigningContext(serverboundchatcommandpacket.signingContext(this.player.getUUID()));
 
@@ -2147,6 +2150,8 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
                 }
                 this.detectRateSpam(true, "/" + packet.command()); // Spigot
                 // CraftBukkit end
+                });
+                // Paper end - untrash chat handling
             }
 
         }
