From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 8 Apr 2021 06:51:07 -0700
Subject: [PATCH] Stop large move vectors in player packet handling from
 killing the server

Looks like we need to check three vectors, not two. fun.

diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java
index 878c5342ca524768fbe2874b54bc12574852cefa..80b77cdffd64690bb9066b8745ea0ee08d60a7a4 100644
--- a/src/main/java/net/minecraft/server/network/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java
@@ -542,6 +542,12 @@ public class PlayerConnection implements PacketListenerPlayIn {
                 double currDeltaZ = toZ - fromZ;
                 double d10 = Math.max(d6 * d6 + d7 * d7 + d8 * d8, (currDeltaX * currDeltaX + currDeltaY * currDeltaY + currDeltaZ * currDeltaZ) - 1);
                 // Paper end - fix large move vectors killing the server
+                // Tuinity start - fix large move vectors killing the server
+                double otherFieldX = d3 - this.v;
+                double otherFieldY = d4 - this.w - 1.0E-6D;
+                double otherFieldZ = d5 - this.x;
+                d10 = Math.max(d10, (otherFieldX * otherFieldX + otherFieldY * otherFieldY + otherFieldZ * otherFieldZ) - 1);
+                // Tuinity end - fix large move vectors killing the server
 
 
                 // CraftBukkit start - handle custom speeds and skipped ticks
@@ -588,9 +594,9 @@ public class PlayerConnection implements PacketListenerPlayIn {
                 //boolean flag = worldserver.getCubes(entity, entity.getBoundingBox().shrink(0.0625D)); // Tuinity - replace with different checks
                 AxisAlignedBB oldBox = entity.getBoundingBox(); // Tuinity - copy from player movement packet
 
-                d6 = d3 - this.v;
-                d7 = d4 - this.w - 1.0E-6D;
-                d8 = d5 - this.x;
+                d6 = d3 - this.v; // Tuinity - diff on change, used for checking large move vectors above
+                d7 = d4 - this.w - 1.0E-6D; // Tuinity - diff on change, used for checking large move vectors above
+                d8 = d5 - this.x; // Tuinity - diff on change, used for checking large move vectors above
                 entity.move(EnumMoveType.PLAYER, new Vec3D(d6, d7, d8));
                 boolean didCollide = toX != entity.locX() || toY != entity.locY() || toZ != entity.locZ(); // Tuinity - needed here as the difference in Y can be reset - also note: this is only a guess at whether collisions took place, floating point errors can make this true when it shouldn't be...
                 double d11 = d7;
@@ -1295,6 +1301,12 @@ public class PlayerConnection implements PacketListenerPlayIn {
                         double currDeltaZ = toZ - prevZ;
                         double d11 = Math.max(d7 * d7 + d8 * d8 + d9 * d9, (currDeltaX * currDeltaX + currDeltaY * currDeltaY + currDeltaZ * currDeltaZ) - 1);
                         // Paper end - fix large move vectors killing the server
+                        // Tuinity start - fix large move vectors killing the server
+                        double otherFieldX = d4 - this.o;
+                        double otherFieldY = d5 - this.p;
+                        double otherFieldZ = d6 - this.q;
+                        d11 = Math.max(d11, (otherFieldX * otherFieldX + otherFieldY * otherFieldY + otherFieldZ * otherFieldZ) - 1);
+                        // Tuinity end - fix large move vectors killing the server
 
                         if (this.player.isSleeping()) {
                             if (d11 > 1.0D) {
@@ -1346,9 +1358,9 @@ public class PlayerConnection implements PacketListenerPlayIn {
 
                             AxisAlignedBB axisalignedbb = this.player.getBoundingBox(); // Tuinity - diff on change, should be old AABB
 
-                            d7 = d4 - this.o;
-                            d8 = d5 - this.p;
-                            d9 = d6 - this.q;
+                            d7 = d4 - this.o; // Tuinity - diff on change, used for checking large move vectors above
+                            d8 = d5 - this.p; // Tuinity - diff on change, used for checking large move vectors above
+                            d9 = d6 - this.q; // Tuinity - diff on change, used for checking large move vectors above
                             boolean flag = d8 > 0.0D;
 
                             if (this.player.isOnGround() && !packetplayinflying.b() && flag) {
