From 94b098085269b86ae8f926fe64e06fa3fbe6a7ca Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Tue, 19 Sep 2017 08:00:56 +1000
Subject: [PATCH] SPIGOT-3571: Packet handling must be synchronous

---
 nms-patches/PlayerConnection.patch | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/nms-patches/PlayerConnection.patch b/nms-patches/PlayerConnection.patch
index 3ce25e5903..947c67e7e8 100644
--- a/nms-patches/PlayerConnection.patch
+++ b/nms-patches/PlayerConnection.patch
@@ -1519,7 +1519,15 @@
  
              tileentitysign.update();
              worldserver.notify(blockposition, iblockdata, iblockdata, 3);
-@@ -1016,11 +2089,27 @@
+@@ -999,6 +2072,7 @@
+     }
+ 
+     public void a(PacketPlayInKeepAlive packetplayinkeepalive) {
++        PlayerConnectionUtils.ensureMainThread(packetplayinkeepalive, this, this.player.x()); // CraftBukkit
+         if (this.g && packetplayinkeepalive.a() == this.h) {
+             int i = (int) (this.d() - this.f);
+ 
+@@ -1016,11 +2090,27 @@
  
      public void a(PacketPlayInAbilities packetplayinabilities) {
          PlayerConnectionUtils.ensureMainThread(packetplayinabilities, this, this.player.x());
@@ -1548,7 +1556,7 @@
          ArrayList arraylist = Lists.newArrayList();
          Iterator iterator = this.minecraftServer.tabCompleteCommand(this.player, packetplayintabcomplete.a(), packetplayintabcomplete.b(), packetplayintabcomplete.c()).iterator();
  
-@@ -1046,6 +2135,13 @@
+@@ -1046,6 +2136,13 @@
          ItemStack itemstack1;
  
          if ("MC|BEdit".equals(s)) {
@@ -1562,7 +1570,7 @@
              packetdataserializer = packetplayincustompayload.b();
  
              try {
-@@ -1064,15 +2160,25 @@
+@@ -1064,15 +2161,25 @@
                  }
  
                  if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) {
@@ -1588,7 +1596,7 @@
                  packetdataserializer = packetplayincustompayload.b();
  
                  try {
-@@ -1106,10 +2212,11 @@
+@@ -1106,10 +2213,11 @@
                          }
  
                          itemstack2.a("pages", (NBTBase) nbttaglist);
@@ -1601,7 +1609,7 @@
                  }
              } else if ("MC|TrSel".equals(s)) {
                  try {
-@@ -1121,6 +2228,7 @@
+@@ -1121,6 +2229,7 @@
                      }
                  } catch (Exception exception2) {
                      PlayerConnection.LOGGER.error("Couldn\'t select trade", exception2);
@@ -1609,7 +1617,7 @@
                  }
              } else {
                  TileEntity tileentity;
-@@ -1170,6 +2278,7 @@
+@@ -1170,6 +2279,7 @@
                          }
                      } catch (Exception exception3) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception3);
@@ -1617,7 +1625,7 @@
                      }
                  } else if ("MC|AutoCmd".equals(s)) {
                      if (!this.minecraftServer.getEnableCommandBlock()) {
-@@ -1237,6 +2346,7 @@
+@@ -1237,6 +2347,7 @@
                          }
                      } catch (Exception exception4) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception4);
@@ -1625,7 +1633,7 @@
                      }
                  } else {
                      int k;
-@@ -1260,6 +2370,7 @@
+@@ -1260,6 +2371,7 @@
                                  }
                              } catch (Exception exception5) {
                                  PlayerConnection.LOGGER.error("Couldn\'t set beacon", exception5);
@@ -1633,7 +1641,7 @@
                              }
                          }
                      } else if ("MC|ItemName".equals(s)) {
-@@ -1346,6 +2457,7 @@
+@@ -1346,6 +2458,7 @@
                              }
                          } catch (Exception exception6) {
                              PlayerConnection.LOGGER.error("Couldn\'t set structure block", exception6);
@@ -1641,7 +1649,7 @@
                          }
                      } else if ("MC|PickItem".equals(s)) {
                          packetdataserializer = packetplayincustompayload.b();
-@@ -1358,11 +2470,49 @@
+@@ -1358,11 +2471,49 @@
                              this.player.playerConnection.sendPacket(new PacketPlayOutHeldItemSlot(this.player.inventory.itemInHandIndex));
                          } catch (Exception exception7) {
                              PlayerConnection.LOGGER.error("Couldn\'t pick item", exception7);