aboutsummaryrefslogtreecommitdiffhomepage
path: root/paper-server/patches/features/0030-Optimise-collision-checking-in-player-move-packet-ha.patch
blob: dbc8cdd071e330094f9e5304b47668c6cd20d740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 2 Jul 2020 12:02:43 -0700
Subject: [PATCH] Optimise collision checking in player move packet handling

Move collision logic to just the hasNewCollision call instead of getCubes + hasNewCollision

diff --git a/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 650126e6445c0458c6a6649c235908bfeea428cd..d248671b2e1c6256fc4d74320bdb29ca078bad0b 100644
--- a/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -561,7 +561,7 @@ public class ServerGamePacketListenerImpl
                     return;
                 }
 
-                boolean flag = serverLevel.noCollision(rootVehicle, rootVehicle.getBoundingBox().deflate(0.0625));
+                AABB oldBox = rootVehicle.getBoundingBox(); // Paper - copy from player movement packet
                 d3 = d - this.vehicleLastGoodX; // Paper - diff on change, used for checking large move vectors above
                 d4 = d1 - this.vehicleLastGoodY; // Paper - diff on change, used for checking large move vectors above
                 d5 = d2 - this.vehicleLastGoodZ; // Paper - diff on change, used for checking large move vectors above
@@ -571,6 +571,7 @@ public class ServerGamePacketListenerImpl
                 }
 
                 rootVehicle.move(MoverType.PLAYER, new Vec3(d3, d4, d5));
+                boolean didCollide = toX != rootVehicle.getX() || toY != rootVehicle.getY() || toZ != rootVehicle.getZ(); // Paper - 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 verticalDelta = d4; // Paper - Decompile fix, was named d11 previously, is now gone in the source
                 d3 = d - rootVehicle.getX();
                 d4 = d1 - rootVehicle.getY();
@@ -582,14 +583,22 @@ public class ServerGamePacketListenerImpl
                 d7 = d3 * d3 + d4 * d4 + d5 * d5;
                 boolean flag2 = false;
                 if (d7 > org.spigotmc.SpigotConfig.movedWronglyThreshold) { // Spigot
-                    flag2 = true;
+                    flag2 = true; // Paper - diff on change, this should be moved wrongly
                     LOGGER.warn("{} (vehicle of {}) moved wrongly! {}", rootVehicle.getName().getString(), this.player.getName().getString(), Math.sqrt(d7));
                 }
 
                 rootVehicle.absMoveTo(d, d1, d2, f, f1);
                 this.player.absMoveTo(d, d1, d2, this.player.getYRot(), this.player.getXRot()); // CraftBukkit
-                boolean flag3 = serverLevel.noCollision(rootVehicle, rootVehicle.getBoundingBox().deflate(0.0625));
-                if (flag && (flag2 || !flag3)) {
+                // Paper start - optimise out extra getCubes
+                boolean teleportBack = flag2; // violating this is always a fail
+                if (!teleportBack) {
+                    // note: only call after setLocation, or else getBoundingBox is wrong
+                    AABB newBox = rootVehicle.getBoundingBox();
+                    if (didCollide || !oldBox.equals(newBox)) {
+                        teleportBack = this.hasNewCollision(serverLevel, rootVehicle, oldBox, newBox);
+                    } // else: no collision at all detected, why do we care?
+                }
+                if (teleportBack) { // Paper end - optimise out extra getCubes
                     rootVehicle.absMoveTo(x, y, z, f, f1);
                     this.player.absMoveTo(x, y, z, this.player.getYRot(), this.player.getXRot()); // CraftBukkit
                     this.send(ClientboundMoveVehiclePacket.fromEntity(rootVehicle));
@@ -667,9 +676,32 @@ public class ServerGamePacketListenerImpl
     }
 
     private boolean noBlocksAround(Entity entity) {
-        return entity.level()
-            .getBlockStates(entity.getBoundingBox().inflate(0.0625).expandTowards(0.0, -0.55, 0.0))
-            .allMatch(BlockBehaviour.BlockStateBase::isAir);
+        // Paper start - stop using streams, this is already a known fixed problem in Entity#move
+        AABB box = entity.getBoundingBox().inflate(0.0625).expandTowards(0.0, -0.55, 0.0);
+        int minX = Mth.floor(box.minX);
+        int minY = Mth.floor(box.minY);
+        int minZ = Mth.floor(box.minZ);
+        int maxX = Mth.floor(box.maxX);
+        int maxY = Mth.floor(box.maxY);
+        int maxZ = Mth.floor(box.maxZ);
+
+        Level level = entity.level();
+        BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
+
+        for (int y = minY; y <= maxY; ++y) {
+            for (int z = minZ; z <= maxZ; ++z) {
+                for (int x = minX; x <= maxX; ++x) {
+                    pos.set(x, y, z);
+                    BlockState blockState = level.getBlockStateIfLoaded(pos);
+                    if (blockState != null && !blockState.isAir()) {
+                        return false;
+                    }
+                }
+            }
+        }
+
+        return true;
+        // Paper end - stop using streams, this is already a known fixed problem in Entity#move
     }
 
     @Override
@@ -1361,7 +1393,7 @@ public class ServerGamePacketListenerImpl
                                 }
                             }
 
-                            AABB boundingBox = this.player.getBoundingBox();
+                            AABB boundingBox = this.player.getBoundingBox(); // Paper - diff on change, should be old AABB
                             d3 = d - this.lastGoodX; // Paper - diff on change, used for checking large move vectors above
                             d4 = d1 - this.lastGoodY; // Paper - diff on change, used for checking large move vectors above
                             d5 = d2 - this.lastGoodZ; // Paper - diff on change, used for checking large move vectors above
@@ -1400,6 +1432,7 @@ public class ServerGamePacketListenerImpl
                             boolean flag1 = this.player.verticalCollisionBelow;
                             this.player.move(MoverType.PLAYER, new Vec3(d3, d4, d5));
                             this.player.onGround = packet.isOnGround(); // CraftBukkit - SPIGOT-5810, SPIGOT-5835, SPIGOT-6828: reset by this.player.move
+                            boolean didCollide = toX != this.player.getX() || toY != this.player.getY() || toZ != this.player.getZ(); // Paper - 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...
                             // Paper start - prevent position desync
                             if (this.awaitingPositionFromClient != null) {
                                 return; // ... thanks Mojang for letting move calls teleport across dimensions.
@@ -1432,7 +1465,17 @@ public class ServerGamePacketListenerImpl
                             }
 
                             // Paper start - Add fail move event
-                            boolean teleportBack = !this.player.noPhysics && !this.player.isSleeping() && (movedWrongly && serverLevel.noCollision(this.player, boundingBox) || this.isPlayerCollidingWithAnythingNew(serverLevel, boundingBox, d, d1, d2));
+                            // Paper start - optimise out extra getCubes
+                            boolean teleportBack = !this.player.noPhysics && !this.player.isSleeping() && movedWrongly;
+                            this.player.absMoveTo(d, d1, d2, f, f1); // prevent desync by tping to the set position, dropped for unknown reasons by mojang
+                            if (!this.player.noPhysics && !this.player.isSleeping() && !teleportBack) {
+                                AABB newBox = this.player.getBoundingBox();
+                                if (didCollide || !boundingBox.equals(newBox)) {
+                                    // note: only call after setLocation, or else getBoundingBox is wrong
+                                    teleportBack = this.hasNewCollision(serverLevel, this.player, boundingBox, newBox);
+                                } // else: no collision at all detected, why do we care?
+                            }
+                            // Paper end - optimise out extra getCubes
                             if (teleportBack) {
                                 io.papermc.paper.event.player.PlayerFailMoveEvent event = fireFailMove(io.papermc.paper.event.player.PlayerFailMoveEvent.FailReason.CLIPPED_INTO_BLOCK,
                                     toX, toY, toZ, toYaw, toPitch, false);
@@ -1568,7 +1611,7 @@ public class ServerGamePacketListenerImpl
 
     private boolean updateAwaitingTeleport() {
         if (this.awaitingPositionFromClient != null) {
-            if (this.tickCount - this.awaitingTeleportTime > 20) {
+            if (false && this.tickCount - this.awaitingTeleportTime > 20) { // Paper - this will greatly screw with clients with > 1000ms RTT
                 this.awaitingTeleportTime = this.tickCount;
                 this.teleport(
                     this.awaitingPositionFromClient.x,
@@ -1587,6 +1630,33 @@ public class ServerGamePacketListenerImpl
         }
     }
 
+    // Paper start - optimise out extra getCubes
+    private boolean hasNewCollision(final ServerLevel level, final Entity entity, final AABB oldBox, final AABB newBox) {
+        final List<AABB> collisionsBB = new java.util.ArrayList<>();
+        final List<VoxelShape> collisionsVoxel = new java.util.ArrayList<>();
+        ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.getCollisions(
+            level, entity, newBox, collisionsVoxel, collisionsBB,
+            ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_FLAG_COLLIDE_WITH_UNLOADED_CHUNKS | ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_FLAG_CHECK_BORDER,
+            null, null
+        );
+
+        for (int i = 0, len = collisionsBB.size(); i < len; ++i) {
+            final AABB box = collisionsBB.get(i);
+            if (!ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.voxelShapeIntersect(box, oldBox)) {
+                return true;
+            }
+        }
+
+        for (int i = 0, len = collisionsVoxel.size(); i < len; ++i) {
+            final VoxelShape voxel = collisionsVoxel.get(i);
+            if (!ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.voxelShapeIntersectNoEmpty(voxel, oldBox)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+    // Paper end - optimise out extra getCubes
     private boolean isPlayerCollidingWithAnythingNew(LevelReader level, AABB box, double x, double y, double z) {
         AABB aabb = this.player.getBoundingBox().move(x - this.player.getX(), y - this.player.getY(), z - this.player.getZ());
         Iterable<VoxelShape> collisions = level.getCollisions(this.player, aabb.deflate(1.0E-5F));