From 8e5e90d5eba3253fc7d77943b64a71d50ff1422c Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Wed, 19 Oct 2016 20:43:47 +1100
Subject: [PATCH] SPIGOT-1903: Only issue player location update after tick

Previously after a player tick, the player's location would be reset back to the location which they had prior to the tick, only to be (hopefully) corrected at a later point, after the client had attempted (and failed) to acknowledge the location change.
---
 nms-patches/PlayerConnection.patch | 101 +++++++++++++++--------------
 1 file changed, 54 insertions(+), 47 deletions(-)

diff --git a/nms-patches/PlayerConnection.patch b/nms-patches/PlayerConnection.patch
index 8a7ef1f0ff..e45bb73f9c 100644
--- a/nms-patches/PlayerConnection.patch
+++ b/nms-patches/PlayerConnection.patch
@@ -70,7 +70,7 @@
  
      public PlayerConnection(MinecraftServer minecraftserver, NetworkManager networkmanager, EntityPlayer entityplayer) {
          this.minecraftServer = minecraftserver;
-@@ -58,7 +105,32 @@
+@@ -58,11 +105,38 @@
          networkmanager.setPacketListener(this);
          this.player = entityplayer;
          entityplayer.playerConnection = this;
@@ -102,8 +102,15 @@
 +    // CraftBukkit end
  
      public void E_() {
-         this.d();
-@@ -110,15 +182,21 @@
+-        this.d();
++        // CraftBukkit start - SPIGOT-1903: reverse order of location update
+         this.player.k_();
++        this.d();
++        // CraftBukkit end
+         this.player.setLocation(this.l, this.m, this.n, this.player.yaw, this.player.pitch);
+         ++this.e;
+         this.G = this.F;
+@@ -110,15 +184,21 @@
          }
  
          this.minecraftServer.methodProfiler.b();
@@ -125,7 +132,7 @@
              this.disconnect("You have been idle for too long!");
          }
  
-@@ -138,19 +216,40 @@
+@@ -138,19 +218,40 @@
      }
  
      public void disconnect(String s) {
@@ -169,7 +176,7 @@
      }
  
      public void a(PacketPlayInSteerVehicle packetplayinsteervehicle) {
-@@ -189,7 +288,34 @@
+@@ -189,7 +290,34 @@
                  double d9 = entity.motX * entity.motX + entity.motY * entity.motY + entity.motZ * entity.motZ;
                  double d10 = d6 * d6 + d7 * d7 + d8 * d8;
  
@@ -205,7 +212,7 @@
                      PlayerConnection.LOGGER.warn("{} (vehicle of {}) moved too quickly! {},{},{}", new Object[] { entity.getName(), this.player.getName(), Double.valueOf(d6), Double.valueOf(d7), Double.valueOf(d8)});
                      this.networkManager.sendPacket(new PacketPlayOutVehicleMove(entity));
                      return;
-@@ -227,6 +353,62 @@
+@@ -227,6 +355,62 @@
                      return;
                  }
  
@@ -268,7 +275,7 @@
                  this.minecraftServer.getPlayerList().d(this.player);
                  this.player.checkMovement(this.player.locX - d0, this.player.locY - d1, this.player.locZ - d2);
                  this.D = d11 >= -0.03125D && !this.minecraftServer.getAllowFlight() && !worldserver.d(entity.getBoundingBox().g(0.0625D).a(0.0D, -0.55D, 0.0D));
-@@ -261,7 +443,7 @@
+@@ -261,7 +445,7 @@
          } else {
              WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
  
@@ -277,7 +284,7 @@
                  if (this.e == 0) {
                      this.d();
                  }
-@@ -271,13 +453,21 @@
+@@ -271,13 +455,21 @@
                          this.A = this.e;
                          this.a(this.teleportPos.x, this.teleportPos.y, this.teleportPos.z, this.player.yaw, this.player.pitch);
                      }
@@ -300,7 +307,7 @@
                          double d0 = this.player.locX;
                          double d1 = this.player.locY;
                          double d2 = this.player.locZ;
-@@ -296,15 +486,33 @@
+@@ -296,15 +488,33 @@
                          ++this.F;
                          int i = this.F - this.G;
  
@@ -337,7 +344,7 @@
                                  PlayerConnection.LOGGER.warn("{} moved too quickly! {},{},{}", new Object[] { this.player.getName(), Double.valueOf(d7), Double.valueOf(d8), Double.valueOf(d9)});
                                  this.a(this.player.locX, this.player.locY, this.player.locZ, this.player.yaw, this.player.pitch);
                                  return;
-@@ -350,6 +558,69 @@
+@@ -350,6 +560,69 @@
                              }
                          }
  
@@ -407,7 +414,7 @@
                          this.B = d12 >= -0.03125D;
                          this.B &= !this.minecraftServer.getAllowFlight() && !this.player.abilities.canFly;
                          this.B &= !this.player.hasEffect(MobEffects.LEVITATION) && !this.player.cG() && !worldserver.d(this.player.getBoundingBox().g(0.0625D).a(0.0D, -0.55D, 0.0D));
-@@ -366,15 +637,79 @@
+@@ -366,15 +639,79 @@
      }
  
      public void a(double d0, double d1, double d2, float f, float f1) {
@@ -492,7 +499,7 @@
          float f2 = f;
          float f3 = f1;
  
-@@ -386,6 +721,14 @@
+@@ -386,6 +723,14 @@
              f3 = f1 + this.player.pitch;
          }
  
@@ -507,7 +514,7 @@
          if (++this.teleportAwait == Integer.MAX_VALUE) {
              this.teleportAwait = 0;
          }
-@@ -397,37 +740,61 @@
+@@ -397,37 +742,61 @@
  
      public void a(PacketPlayInBlockDig packetplayinblockdig) {
          PlayerConnectionUtils.ensureMainThread(packetplayinblockdig, this, this.player.x());
@@ -575,7 +582,7 @@
              this.player.clearActiveItem();
              itemstack = this.player.getItemInMainHand();
              if (itemstack != null && itemstack.count == 0) {
-@@ -436,9 +803,9 @@
+@@ -436,9 +805,9 @@
  
              return;
  
@@ -588,7 +595,7 @@
              double d0 = this.player.locX - ((double) blockposition.getX() + 0.5D);
              double d1 = this.player.locY - ((double) blockposition.getY() + 0.5D) + 1.5D;
              double d2 = this.player.locZ - ((double) blockposition.getZ() + 0.5D);
-@@ -453,7 +820,15 @@
+@@ -453,7 +822,15 @@
                      if (!this.minecraftServer.a(worldserver, blockposition, this.player) && worldserver.getWorldBorder().a(blockposition)) {
                          this.player.playerInteractManager.a(blockposition, packetplayinblockdig.b());
                      } else {
@@ -604,7 +611,7 @@
                      }
                  } else {
                      if (packetplayinblockdig.c() == PacketPlayInBlockDig.EnumPlayerDigType.STOP_DESTROY_BLOCK) {
-@@ -473,10 +848,12 @@
+@@ -473,10 +850,12 @@
          default:
              throw new IllegalArgumentException("Invalid player action");
          }
@@ -617,7 +624,7 @@
          WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
          EnumHand enumhand = packetplayinuseitem.c();
          ItemStack itemstack = this.player.b(enumhand);
-@@ -490,6 +867,13 @@
+@@ -490,6 +869,13 @@
              chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
              this.player.playerConnection.sendPacket(new PacketPlayOutChat(chatmessage));
          } else if (this.teleportPos == null && this.player.e((double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.5D, (double) blockposition.getZ() + 0.5D) < 64.0D && !this.minecraftServer.a(worldserver, blockposition, this.player) && worldserver.getWorldBorder().a(blockposition)) {
@@ -631,7 +638,7 @@
              this.player.playerInteractManager.a(this.player, worldserver, itemstack, enumhand, blockposition, enumdirection, packetplayinuseitem.d(), packetplayinuseitem.e(), packetplayinuseitem.f());
          }
  
-@@ -504,19 +888,55 @@
+@@ -504,19 +890,55 @@
  
      public void a(PacketPlayInBlockPlace packetplayinblockplace) {
          PlayerConnectionUtils.ensureMainThread(packetplayinblockplace, this, this.player.x());
@@ -692,7 +699,7 @@
          }
      }
  
-@@ -527,8 +947,8 @@
+@@ -527,8 +949,8 @@
              WorldServer[] aworldserver = this.minecraftServer.worldServer;
              int i = aworldserver.length;
  
@@ -703,7 +710,7 @@
  
                  if (worldserver != null) {
                      entity = packetplayinspectate.a(worldserver);
-@@ -541,6 +961,8 @@
+@@ -541,6 +963,8 @@
              if (entity != null) {
                  this.player.setSpectatorTarget(this.player);
                  this.player.stopRiding();
@@ -712,7 +719,7 @@
                  if (entity.world == this.player.world) {
                      this.player.enderTeleportTo(entity.locX, entity.locY, entity.locZ);
                  } else {
-@@ -566,12 +988,19 @@
+@@ -566,12 +990,19 @@
                      this.minecraftServer.getPlayerList().b(this.player, worldserver2);
                      this.minecraftServer.getPlayerList().updateClient(this.player);
                  }
@@ -733,7 +740,7 @@
  
      public void a(PacketPlayInBoatMove packetplayinboatmove) {
          PlayerConnectionUtils.ensureMainThread(packetplayinboatmove, this, this.player.x());
-@@ -584,14 +1013,29 @@
+@@ -584,14 +1015,29 @@
      }
  
      public void a(IChatBaseComponent ichatbasecomponent) {
@@ -765,7 +772,7 @@
          if (this.minecraftServer.R() && this.player.getName().equals(this.minecraftServer.Q())) {
              PlayerConnection.LOGGER.info("Stopping singleplayer server as player logged out");
              this.minecraftServer.safeShutdown();
-@@ -613,6 +1057,15 @@
+@@ -613,6 +1059,15 @@
              }
          }
  
@@ -781,7 +788,7 @@
          try {
              this.networkManager.sendPacket(packet);
          } catch (Throwable throwable) {
-@@ -634,17 +1087,32 @@
+@@ -634,17 +1089,32 @@
  
      public void a(PacketPlayInHeldItemSlot packetplayinhelditemslot) {
          PlayerConnectionUtils.ensureMainThread(packetplayinhelditemslot, this, this.player.x());
@@ -816,7 +823,7 @@
              ChatMessage chatmessage = new ChatMessage("chat.cannotSend", new Object[0]);
  
              chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
-@@ -657,39 +1125,249 @@
+@@ -657,39 +1127,249 @@
  
              for (int i = 0; i < s.length(); ++i) {
                  if (!SharedConstants.isAllowedChatCharacter(s.charAt(i))) {
@@ -1074,7 +1081,7 @@
          this.player.resetIdleTimer();
          IJumpable ijumpable;
  
-@@ -759,6 +1437,7 @@
+@@ -759,6 +1439,7 @@
  
      public void a(PacketPlayInUseEntity packetplayinuseentity) {
          PlayerConnectionUtils.ensureMainThread(packetplayinuseentity, this, this.player.x());
@@ -1082,7 +1089,7 @@
          WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
          Entity entity = packetplayinuseentity.a((World) worldserver);
  
-@@ -775,22 +1454,72 @@
+@@ -775,22 +1456,72 @@
                  EnumHand enumhand;
                  ItemStack itemstack;
  
@@ -1156,7 +1163,7 @@
                  }
              }
          }
-@@ -806,7 +1535,8 @@
+@@ -806,7 +1537,8 @@
          case 1:
              if (this.player.viewingCredits) {
                  this.player.viewingCredits = false;
@@ -1166,7 +1173,7 @@
              } else {
                  if (this.player.getHealth() > 0.0F) {
                      return;
-@@ -832,14 +1562,20 @@
+@@ -832,14 +1564,20 @@
  
      public void a(PacketPlayInCloseWindow packetplayinclosewindow) {
          PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.x());
@@ -1188,7 +1195,7 @@
                  ArrayList arraylist = Lists.newArrayList();
  
                  for (int i = 0; i < this.player.activeContainer.c.size(); ++i) {
-@@ -848,8 +1584,279 @@
+@@ -848,8 +1586,279 @@
  
                  this.player.a(this.player.activeContainer, (List) arraylist);
              } else {
@@ -1200,7 +1207,7 @@
 +
 +                InventoryView inventory = this.player.activeContainer.getBukkitView();
 +                SlotType type = CraftInventoryView.getSlotType(inventory, packetplayinwindowclick.b());
- 
++
 +                InventoryClickEvent event;
 +                ClickType click = ClickType.UNKNOWN;
 +                InventoryAction action = InventoryAction.UNKNOWN;
@@ -1376,7 +1383,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());
@@ -1469,7 +1476,7 @@
                  if (ItemStack.matches(packetplayinwindowclick.e(), itemstack)) {
                      this.player.playerConnection.sendPacket(new PacketPlayOutTransaction(packetplayinwindowclick.a(), packetplayinwindowclick.d(), true));
                      this.player.f = true;
-@@ -878,6 +1885,7 @@
+@@ -878,6 +1887,7 @@
  
      public void a(PacketPlayInEnchantItem packetplayinenchantitem) {
          PlayerConnectionUtils.ensureMainThread(packetplayinenchantitem, this, this.player.x());
@@ -1477,7 +1484,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());
-@@ -911,8 +1919,48 @@
+@@ -911,8 +1921,48 @@
              }
  
              boolean flag1 = packetplayinsetcreativeslot.a() >= 1 && packetplayinsetcreativeslot.a() <= 45;
@@ -1527,7 +1534,7 @@
  
              if (flag1 && flag2 && flag3) {
                  if (itemstack == null) {
-@@ -936,6 +1984,7 @@
+@@ -936,6 +1986,7 @@
  
      public void a(PacketPlayInTransaction packetplayintransaction) {
          PlayerConnectionUtils.ensureMainThread(packetplayintransaction, this, this.player.x());
@@ -1535,7 +1542,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()) {
-@@ -946,6 +1995,7 @@
+@@ -946,6 +1997,7 @@
  
      public void a(PacketPlayInUpdateSign packetplayinupdatesign) {
          PlayerConnectionUtils.ensureMainThread(packetplayinupdatesign, this, this.player.x());
@@ -1543,7 +1550,7 @@
          this.player.resetIdleTimer();
          WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
          BlockPosition blockposition = packetplayinupdatesign.a();
-@@ -962,14 +2012,30 @@
+@@ -962,14 +2014,30 @@
  
              if (!tileentitysign.d() || tileentitysign.e() != this.player) {
                  this.minecraftServer.warning("Player " + this.player.getName() + " just tried to change non-editable sign");
@@ -1575,7 +1582,7 @@
  
              tileentitysign.update();
              worldserver.notify(blockposition, iblockdata, iblockdata, 3);
-@@ -992,11 +2058,27 @@
+@@ -992,11 +2060,27 @@
  
      public void a(PacketPlayInAbilities packetplayinabilities) {
          PlayerConnectionUtils.ensureMainThread(packetplayinabilities, this, this.player.x());
@@ -1604,7 +1611,7 @@
          ArrayList arraylist = Lists.newArrayList();
          Iterator iterator = this.minecraftServer.tabCompleteCommand(this.player, packetplayintabcomplete.a(), packetplayintabcomplete.b(), packetplayintabcomplete.c()).iterator();
  
-@@ -1040,10 +2122,13 @@
+@@ -1040,10 +2124,13 @@
                  }
  
                  if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) {
@@ -1618,7 +1625,7 @@
              }
          } else {
              String s1;
-@@ -1067,6 +2152,7 @@
+@@ -1067,6 +2154,7 @@
                      }
  
                      if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack1.getItem() == Items.WRITABLE_BOOK) {
@@ -1626,7 +1633,7 @@
                          itemstack1.a("author", (NBTBase) (new NBTTagString(this.player.getName())));
                          itemstack1.a("title", (NBTBase) (new NBTTagString(itemstack.getTag().getString("title"))));
                          NBTTagList nbttaglist = itemstack.getTag().getList("pages", 8);
-@@ -1081,9 +2167,11 @@
+@@ -1081,9 +2169,11 @@
  
                          itemstack1.a("pages", (NBTBase) nbttaglist);
                          itemstack1.setItem(Items.WRITTEN_BOOK);
@@ -1638,7 +1645,7 @@
                  }
              } else if ("MC|TrSel".equals(s)) {
                  try {
-@@ -1095,6 +2183,7 @@
+@@ -1095,6 +2185,7 @@
                      }
                  } catch (Exception exception2) {
                      PlayerConnection.LOGGER.error("Couldn\'t select trade", exception2);
@@ -1646,7 +1653,7 @@
                  }
              } else {
                  TileEntity tileentity;
-@@ -1144,6 +2233,7 @@
+@@ -1144,6 +2235,7 @@
                          }
                      } catch (Exception exception3) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception3);
@@ -1654,7 +1661,7 @@
                      }
                  } else if ("MC|AutoCmd".equals(s)) {
                      if (!this.minecraftServer.getEnableCommandBlock()) {
-@@ -1211,6 +2301,7 @@
+@@ -1211,6 +2303,7 @@
                          }
                      } catch (Exception exception4) {
                          PlayerConnection.LOGGER.error("Couldn\'t set command block", exception4);
@@ -1662,7 +1669,7 @@
                      }
                  } else {
                      int k;
-@@ -1234,6 +2325,7 @@
+@@ -1234,6 +2327,7 @@
                                  }
                              } catch (Exception exception5) {
                                  PlayerConnection.LOGGER.error("Couldn\'t set beacon", exception5);
@@ -1670,7 +1677,7 @@
                              }
                          }
                      } else if ("MC|ItemName".equals(s)) {
-@@ -1320,6 +2412,7 @@
+@@ -1320,6 +2414,7 @@
                              }
                          } catch (Exception exception6) {
                              PlayerConnection.LOGGER.error("Couldn\'t set structure block", exception6);
@@ -1678,7 +1685,7 @@
                          }
                      } else if ("MC|PickItem".equals(s)) {
                          packetdataserializer = packetplayincustompayload.b();
-@@ -1332,14 +2425,37 @@
+@@ -1332,14 +2427,37 @@
                              this.player.playerConnection.sendPacket(new PacketPlayOutHeldItemSlot(this.player.inventory.itemInHandIndex));
                          } catch (Exception exception7) {
                              PlayerConnection.LOGGER.error("Couldn\'t pick item", exception7);