From 3e3bbd42724e3c5e691f4aebfbdf5e1e784c1f68 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Wed, 8 Nov 2017 10:36:11 +1100
Subject: [PATCH] SPIGOT-3638: Don't process async chat on shutting down server

---
 nms-patches/PlayerConnection.patch | 55 ++++++++++++++++--------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/nms-patches/PlayerConnection.patch b/nms-patches/PlayerConnection.patch
index bf80617dde..06ad6f64d9 100644
--- a/nms-patches/PlayerConnection.patch
+++ b/nms-patches/PlayerConnection.patch
@@ -755,7 +755,7 @@
          try {
              this.networkManager.sendPacket(packet);
          } catch (Throwable throwable) {
-@@ -652,17 +1121,32 @@
+@@ -652,17 +1121,37 @@
  
      public void a(PacketPlayInHeldItemSlot packetplayinhelditemslot) {
          PlayerConnectionUtils.ensureMainThread(packetplayinhelditemslot, this, this.player.x());
@@ -781,6 +781,11 @@
 -        PlayerConnectionUtils.ensureMainThread(packetplayinchat, this, this.player.x());
 -        if (this.player.getChatFlags() == EntityHuman.EnumChatVisibility.HIDDEN) {
 +        // CraftBukkit start - async chat
++        // SPIGOT-3638
++        if (this.minecraftServer.isStopped()) {
++            return;
++        }
++
 +        boolean isSync = packetplayinchat.a().startsWith("/");
 +        if (packetplayinchat.a().startsWith("/")) {
 +            PlayerConnectionUtils.ensureMainThread(packetplayinchat, this, this.player.x());
@@ -790,7 +795,7 @@
              ChatMessage chatmessage = new ChatMessage("chat.cannotSend", new Object[0]);
  
              chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
-@@ -675,39 +1159,249 @@
+@@ -675,39 +1164,249 @@
  
              for (int i = 0; i < s.length(); ++i) {
                  if (!SharedConstants.isAllowedChatCharacter(s.charAt(i))) {
@@ -1047,7 +1052,7 @@
          this.player.resetIdleTimer();
          IJumpable ijumpable;
  
-@@ -779,6 +1473,7 @@
+@@ -779,6 +1478,7 @@
  
      public void a(PacketPlayInUseEntity packetplayinuseentity) {
          PlayerConnectionUtils.ensureMainThread(packetplayinuseentity, this, this.player.x());
@@ -1055,7 +1060,7 @@
          WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
          Entity entity = packetplayinuseentity.a((World) worldserver);
  
-@@ -794,20 +1489,68 @@
+@@ -794,20 +1494,68 @@
              if (this.player.h(entity) < d0) {
                  EnumHand enumhand;
  
@@ -1125,7 +1130,7 @@
                  }
              }
          }
-@@ -823,7 +1566,8 @@
+@@ -823,7 +1571,8 @@
          case PERFORM_RESPAWN:
              if (this.player.viewingCredits) {
                  this.player.viewingCredits = false;
@@ -1135,7 +1140,7 @@
                  CriterionTriggers.u.a(this.player, DimensionManager.THE_END, DimensionManager.OVERWORLD);
              } else {
                  if (this.player.getHealth() > 0.0F) {
-@@ -846,14 +1590,20 @@
+@@ -846,14 +1595,20 @@
  
      public void a(PacketPlayInCloseWindow packetplayinclosewindow) {
          PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.x());
@@ -1158,7 +1163,7 @@
                  NonNullList nonnulllist = NonNullList.a();
  
                  for (int i = 0; i < this.player.activeContainer.slots.size(); ++i) {
-@@ -862,8 +1612,274 @@
+@@ -862,8 +1617,274 @@
  
                  this.player.a(this.player.activeContainer, nonnulllist);
              } else {
@@ -1341,7 +1346,7 @@
 +                    default:
 +                        break;
 +                }
-+
+ 
 +                if (packetplayinwindowclick.f() != InventoryClickType.QUICK_CRAFT) {
 +                    if (click == ClickType.NUMBER_KEY) {
 +                        event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.b(), click, action, packetplayinwindowclick.c());
@@ -1423,7 +1428,7 @@
 +                            }
 +                            return;
 +                    }
- 
++
 +                    if (event instanceof CraftItemEvent) {
 +                        // Need to update the inventory on crafting to
 +                        // correctly support custom recipes
@@ -1434,7 +1439,7 @@
                  if (ItemStack.matches(packetplayinwindowclick.e(), itemstack)) {
                      this.player.playerConnection.sendPacket(new PacketPlayOutTransaction(packetplayinwindowclick.a(), packetplayinwindowclick.d(), true));
                      this.player.f = true;
-@@ -900,6 +1916,7 @@
+@@ -900,6 +1921,7 @@
  
      public void a(PacketPlayInEnchantItem packetplayinenchantitem) {
          PlayerConnectionUtils.ensureMainThread(packetplayinenchantitem, this, this.player.x());
@@ -1442,7 +1447,7 @@
          this.player.resetIdleTimer();
          if (this.player.activeContainer.windowId == packetplayinenchantitem.a() && this.player.activeContainer.c(this.player) && !this.player.isSpectator()) {
              this.player.activeContainer.a(this.player, packetplayinenchantitem.b());
-@@ -933,7 +1950,45 @@
+@@ -933,7 +1955,45 @@
              }
  
              boolean flag1 = packetplayinsetcreativeslot.a() >= 1 && packetplayinsetcreativeslot.a() <= 45;
@@ -1489,7 +1494,7 @@
  
              if (flag1 && flag2) {
                  if (itemstack.isEmpty()) {
-@@ -957,6 +2012,7 @@
+@@ -957,6 +2017,7 @@
  
      public void a(PacketPlayInTransaction packetplayintransaction) {
          PlayerConnectionUtils.ensureMainThread(packetplayintransaction, this, this.player.x());
@@ -1497,7 +1502,7 @@
          Short oshort = (Short) this.k.get(this.player.activeContainer.windowId);
  
          if (oshort != null && packetplayintransaction.b() == oshort.shortValue() && this.player.activeContainer.windowId == packetplayintransaction.a() && !this.player.activeContainer.c(this.player) && !this.player.isSpectator()) {
-@@ -967,6 +2023,7 @@
+@@ -967,6 +2028,7 @@
  
      public void a(PacketPlayInUpdateSign packetplayinupdatesign) {
          PlayerConnectionUtils.ensureMainThread(packetplayinupdatesign, this, this.player.x());
@@ -1505,7 +1510,7 @@
          this.player.resetIdleTimer();
          WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
          BlockPosition blockposition = packetplayinupdatesign.a();
-@@ -983,14 +2040,30 @@
+@@ -983,14 +2045,30 @@
  
              if (!tileentitysign.a() || tileentitysign.e() != this.player) {
                  this.minecraftServer.warning("Player " + this.player.getName() + " just tried to change non-editable sign");
@@ -1537,7 +1542,7 @@
  
              tileentitysign.update();
              worldserver.notify(blockposition, iblockdata, iblockdata, 3);
-@@ -999,6 +2072,7 @@
+@@ -999,6 +2077,7 @@
      }
  
      public void a(PacketPlayInKeepAlive packetplayinkeepalive) {
@@ -1545,7 +1550,7 @@
          if (this.g && packetplayinkeepalive.a() == this.h) {
              int i = (int) (this.d() - this.f);
  
-@@ -1016,11 +2090,27 @@
+@@ -1016,11 +2095,27 @@
  
      public void a(PacketPlayInAbilities packetplayinabilities) {
          PlayerConnectionUtils.ensureMainThread(packetplayinabilities, this, this.player.x());
@@ -1574,7 +1579,7 @@
          ArrayList arraylist = Lists.newArrayList();
          Iterator iterator = this.minecraftServer.tabCompleteCommand(this.player, packetplayintabcomplete.a(), packetplayintabcomplete.b(), packetplayintabcomplete.c()).iterator();
  
-@@ -1046,6 +2136,13 @@
+@@ -1046,6 +2141,13 @@
          ItemStack itemstack1;
  
          if ("MC|BEdit".equals(s)) {
@@ -1588,7 +1593,7 @@
              packetdataserializer = packetplayincustompayload.b();
  
              try {
-@@ -1064,15 +2161,25 @@
+@@ -1064,15 +2166,25 @@
                  }
  
                  if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) {
@@ -1614,7 +1619,7 @@
                  packetdataserializer = packetplayincustompayload.b();
  
                  try {
-@@ -1106,10 +2213,11 @@
+@@ -1106,10 +2218,11 @@
                          }
  
                          itemstack2.a("pages", (NBTBase) nbttaglist);
@@ -1627,7 +1632,7 @@
                  }
              } else if ("MC|TrSel".equals(s)) {
                  try {
-@@ -1121,6 +2229,7 @@
+@@ -1121,6 +2234,7 @@
                      }
                  } catch (Exception exception2) {
                      PlayerConnection.LOGGER.error("Couldn\'t select trade", exception2);
@@ -1635,7 +1640,7 @@
                  }
              } else {
                  TileEntity tileentity;
-@@ -1170,6 +2279,7 @@
+@@ -1170,6 +2284,7 @@
                          }
                      } catch (Exception exception3) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception3);
@@ -1643,7 +1648,7 @@
                      }
                  } else if ("MC|AutoCmd".equals(s)) {
                      if (!this.minecraftServer.getEnableCommandBlock()) {
-@@ -1237,6 +2347,7 @@
+@@ -1237,6 +2352,7 @@
                          }
                      } catch (Exception exception4) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception4);
@@ -1651,7 +1656,7 @@
                      }
                  } else {
                      int k;
-@@ -1260,6 +2371,7 @@
+@@ -1260,6 +2376,7 @@
                                  }
                              } catch (Exception exception5) {
                                  PlayerConnection.LOGGER.error("Couldn\'t set beacon", exception5);
@@ -1659,7 +1664,7 @@
                              }
                          }
                      } else if ("MC|ItemName".equals(s)) {
-@@ -1346,6 +2458,7 @@
+@@ -1346,6 +2463,7 @@
                              }
                          } catch (Exception exception6) {
                              PlayerConnection.LOGGER.error("Couldn\'t set structure block", exception6);
@@ -1667,7 +1672,7 @@
                          }
                      } else if ("MC|PickItem".equals(s)) {
                          packetdataserializer = packetplayincustompayload.b();
-@@ -1358,11 +2471,49 @@
+@@ -1358,11 +2476,49 @@
                              this.player.playerConnection.sendPacket(new PacketPlayOutHeldItemSlot(this.player.inventory.itemInHandIndex));
                          } catch (Exception exception7) {
                              PlayerConnection.LOGGER.error("Couldn\'t pick item", exception7);