From ec66a2c91cfb3da5ee4aa9ffb224ae308cddd426 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Tue, 26 Sep 2023 09:20:17 -0700
Subject: [PATCH] Fire PlayerConnectionCloseEvent for configuration packet
 listener

If the player moves to the configuration stage, we also need
to fire the event.
---
 patches/server/Add-PlayerConnectionCloseEvent.patch | 13 ++++++-------
 ...etwork-Manager-and-add-advanced-packet-sup.patch |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/patches/server/Add-PlayerConnectionCloseEvent.patch b/patches/server/Add-PlayerConnectionCloseEvent.patch
index 7db78b820e..067030f440 100644
--- a/patches/server/Add-PlayerConnectionCloseEvent.patch
+++ b/patches/server/Add-PlayerConnectionCloseEvent.patch
@@ -48,14 +48,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
                  this.pendingActions.clear(); // Free up packet queue.
 +                // Paper start - Add PlayerConnectionCloseEvent
 +                final PacketListener packetListener = this.getPacketListener();
-+                if (packetListener instanceof net.minecraft.server.network.ServerGamePacketListenerImpl) {
-+                    /* Player was logged in */
-+                    final net.minecraft.server.network.ServerGamePacketListenerImpl playerConnection = (net.minecraft.server.network.ServerGamePacketListenerImpl) packetListener;
-+                    new com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent(playerConnection.player.getUUID(),
-+                        playerConnection.player.getScoreboardName(), ((java.net.InetSocketAddress)address).getAddress(), false).callEvent();
-+                } else if (packetListener instanceof net.minecraft.server.network.ServerLoginPacketListenerImpl) {
++                if (packetListener instanceof net.minecraft.server.network.ServerCommonPacketListenerImpl commonPacketListener) {
++                    /* Player was logged in, either game listener or configuration listener */
++                    final com.mojang.authlib.GameProfile profile = commonPacketListener.getOwner();
++                    new com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent(profile.getId(),
++                        profile.getName(), ((java.net.InetSocketAddress)address).getAddress(), false).callEvent();
++                } else if (packetListener instanceof net.minecraft.server.network.ServerLoginPacketListenerImpl loginListener) {
 +                    /* Player is login stage */
-+                    final net.minecraft.server.network.ServerLoginPacketListenerImpl loginListener = (net.minecraft.server.network.ServerLoginPacketListenerImpl) packetListener;
 +                    switch (loginListener.state) {
 +                        case VERIFYING:
 +                        case WAITING_FOR_DUPE_DISCONNECT:
diff --git a/patches/server/Optimize-Network-Manager-and-add-advanced-packet-sup.patch b/patches/server/Optimize-Network-Manager-and-add-advanced-packet-sup.patch
index 5c02eb0e0e..78850587a9 100644
--- a/patches/server/Optimize-Network-Manager-and-add-advanced-packet-sup.patch
+++ b/patches/server/Optimize-Network-Manager-and-add-advanced-packet-sup.patch
@@ -237,7 +237,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +                this.clearPacketQueue(); // Paper - Optimize network
                  // Paper start - Add PlayerConnectionCloseEvent
                  final PacketListener packetListener = this.getPacketListener();
-                 if (packetListener instanceof net.minecraft.server.network.ServerGamePacketListenerImpl) {
+                 if (packetListener instanceof net.minecraft.server.network.ServerCommonPacketListenerImpl commonPacketListener) {
 @@ -0,0 +0,0 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
      public void setBandwidthLogger(SampleLogger log) {
          this.bandwidthDebugMonitor = new BandwidthDebugMonitor(log);