aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0977-Implement-PlayerFailMoveEvent.patch
blob: 42ea3632009b54ad58dd7e2562be7fef8386db48 (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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Moulberry <james.jenour@protonmail.com>
Date: Wed, 26 Jul 2023 20:13:31 +0800
Subject: [PATCH] Implement PlayerFailMoveEvent


diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 2af12e7dddf872f3b80d6288c0b1bd47fcd2b2f7..6e23c295f77e95564bf132a03397c4640bf9f49e 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1291,8 +1291,8 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
                     double d0 = ServerGamePacketListenerImpl.clampHorizontal(packet.getX(this.player.getX())); final double toX = d0; // Paper - OBFHELPER
                     double d1 = ServerGamePacketListenerImpl.clampVertical(packet.getY(this.player.getY())); final double toY = d1;
                     double d2 = ServerGamePacketListenerImpl.clampHorizontal(packet.getZ(this.player.getZ())); final double toZ = d2; // Paper - OBFHELPER
-                    float f = Mth.wrapDegrees(packet.getYRot(this.player.getYRot()));
-                    float f1 = Mth.wrapDegrees(packet.getXRot(this.player.getXRot()));
+                    float f = Mth.wrapDegrees(packet.getYRot(this.player.getYRot())); final float toYaw = f; // Paper - OBFHELPER
+                    float f1 = Mth.wrapDegrees(packet.getXRot(this.player.getXRot())); final float toPitch = f1; // Paper - OBFHELPER
 
                     if (this.player.isPassenger()) {
                         this.player.absMoveTo(this.player.getX(), this.player.getY(), this.player.getZ(), f, f1);
@@ -1358,8 +1358,14 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
                             }
                             // Paper start - Prevent moving into unloaded chunks
                             if (this.player.level().paperConfig().chunks.preventMovingIntoUnloadedChunks && (this.player.getX() != toX || this.player.getZ() != toZ) && !worldserver.areChunksLoadedForMove(this.player.getBoundingBox().expandTowards(new Vec3(toX, toY, toZ).subtract(this.player.position())))) {
+                                // Paper start - Add fail move event
+                                io.papermc.paper.event.player.PlayerFailMoveEvent event = fireFailMove(io.papermc.paper.event.player.PlayerFailMoveEvent.FailReason.MOVED_INTO_UNLOADED_CHUNK,
+                                    toX, toY, toZ, toYaw, toPitch, false);
+                                if (!event.isAllowed()) {
                                 this.internalTeleport(this.player.getX(), this.player.getY(), this.player.getZ(), this.player.getYRot(), this.player.getXRot(), Collections.emptySet());
                                 return;
+                                }
+                                // Paper end
                             }
                             // Paper end
 
@@ -1368,9 +1374,16 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
 
                                 if (d10 - d9 > Math.max(f2, Math.pow((double) (org.spigotmc.SpigotConfig.movedTooQuicklyMultiplier * (float) i * speed), 2)) && !this.isSingleplayerOwner()) {
                                 // CraftBukkit end
+                                    // Paper start - Add fail move event
+                                    io.papermc.paper.event.player.PlayerFailMoveEvent event = fireFailMove(io.papermc.paper.event.player.PlayerFailMoveEvent.FailReason.MOVED_TOO_QUICKLY,
+                                        toX, toY, toZ, toYaw, toPitch, true);
+                                    if (!event.isAllowed()) {
+                                        if (event.getLogWarning())
                                     ServerGamePacketListenerImpl.LOGGER.warn("{} moved too quickly! {},{},{}", new Object[]{this.player.getName().getString(), d6, d7, d8});
                                     this.teleport(this.player.getX(), this.player.getY(), this.player.getZ(), this.player.getYRot(), this.player.getXRot());
                                     return;
+                                    }
+                                    // Paper end
                                 }
                             }
 
@@ -1435,8 +1448,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
                             boolean flag2 = false;
 
                             if (!this.player.isChangingDimension() && d10 > org.spigotmc.SpigotConfig.movedWronglyThreshold && !this.player.isSleeping() && !this.player.gameMode.isCreative() && this.player.gameMode.getGameModeForPlayer() != GameType.SPECTATOR) { // Spigot
+                                // Paper start - Add fail move event
+                                io.papermc.paper.event.player.PlayerFailMoveEvent event = fireFailMove(io.papermc.paper.event.player.PlayerFailMoveEvent.FailReason.MOVED_WRONGLY,
+                                    toX, toY, toZ, toYaw, toPitch, true);
+                                if (!event.isAllowed()) {
                                 flag2 = true; // Paper - diff on change, this should be moved wrongly
+                                    if (event.getLogWarning())
                                 ServerGamePacketListenerImpl.LOGGER.warn("{} moved wrongly!", this.player.getName().getString());
+                                }
+                                // Paper end
                             }
 
                             // Paper start - optimise out extra getCubes
@@ -1449,6 +1469,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
                                 if (didCollide || !axisalignedbb.equals(newBox)) {
                                     // note: only call after setLocation, or else getBoundingBox is wrong
                                     teleportBack = this.hasNewCollision(worldserver, this.player, axisalignedbb, newBox);
+                                    // Paper start - Add fail move event
+                                    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);
+                                        if (event.isAllowed()) {
+                                            teleportBack = false;
+                                        }
+                                    }
+                                    // Paper end
                                 } // else: no collision at all detected, why do we care?
                             }
                             if (!this.player.noPhysics && !this.player.isSleeping() && teleportBack) { // Paper end - optimise out extra getCubes
@@ -1537,6 +1566,18 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
         }
     }
 
+    // Paper start - Add fail move event
+    private io.papermc.paper.event.player.PlayerFailMoveEvent fireFailMove(io.papermc.paper.event.player.PlayerFailMoveEvent.FailReason failReason,
+               double toX, double toY, double toZ, float toYaw, float toPitch, boolean logWarning) {
+        Player player = this.getCraftPlayer();
+        Location from = new Location(player.getWorld(), this.lastPosX, this.lastPosY, this.lastPosZ, this.lastYaw, this.lastPitch);
+        Location to = new Location(player.getWorld(), toX, toY, toZ, toYaw, toPitch);
+        io.papermc.paper.event.player.PlayerFailMoveEvent event = new io.papermc.paper.event.player.PlayerFailMoveEvent(player, failReason,
+            false, logWarning, from, to);
+        event.callEvent();
+        return event;
+    }
+    // Paper end
     // Paper start - optimise out extra getCubes
     private boolean hasNewCollision(final ServerLevel world, final Entity entity, final AABB oldBox, final AABB newBox) {
         final List<AABB> collisionsBB = new java.util.ArrayList<>();