From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Wed, 8 Jan 2025 22:48:04 -0300 Subject: [PATCH] Add PlayerPreMoveEvent diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index ac0af5da6646df1cfe66aead3f945a9f77efa5de..3a74c35da37326ee52bc7ccee79791732cb2e092 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -1462,7 +1462,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl d6 = d0 - this.lastGoodX; // Paper - diff on change, used for checking large move vectors above d7 = d1 - this.lastGoodY; // Paper - diff on change, used for checking large move vectors above d8 = d2 - this.lastGoodZ; // Paper - diff on change, used for checking large move vectors above - boolean flag1 = d7 > 0.0D; + boolean flag1 = d7 > 0.0D; // SparklyPaper - diff on change, used to reset the fall distance, checks if d7 is > 0.0D (player is moving upwards) if (this.player.onGround() && !packet.isOnGround() && flag1) { // Paper start - Add PlayerJumpEvent @@ -1498,7 +1498,37 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl boolean flag2 = this.player.verticalCollisionBelow; this.player.move(MoverType.PLAYER, new Vec3(d6, d7, d8)); - this.player.onGround = packet.isOnGround(); // CraftBukkit - SPIGOT-5810, SPIGOT-5835, SPIGOT-6828: reset by this.player.move + // SparklyPaper start - Add PlayerPreMoveEvent event + boolean isOnGround = packet.isOnGround(); + boolean horizontalCollision = packet.horizontalCollision(); + if (net.sparklypower.sparklypaper.event.player.PlayerPreMoveEvent.getHandlerList().getRegisteredListeners().length != 0) { + Player player = this.getCraftPlayer(); + Location from = new Location(player.getWorld(), lastPosX, lastPosY, lastPosZ, lastYaw, lastPitch); // Get the Players previous Event location. + Location to = player.getLocation().clone(); // Start off the To location as the Players current location. + + // If the packet contains movement information then we update the To location with the correct XYZ. + if (packet.hasPos) { + to.setX(packet.x); + to.setY(packet.y); + to.setZ(packet.z); + } + + // If the packet contains look information then we update the To location with the correct Yaw & Pitch. + if (packet.hasRot) { + to.setYaw(packet.yRot); + to.setPitch(packet.xRot); + } + + net.sparklypower.sparklypaper.event.player.PlayerPreMoveEvent event = new net.sparklypower.sparklypaper.event.player.PlayerPreMoveEvent(player, from, to, packet.isOnGround(), packet.horizontalCollision(), flag1); + + if (event.callEvent()) { + isOnGround = event.isOnGround(); + horizontalCollision = event.isHorizontalCollision(); + flag1 = event.isResetFallDistance(); + } + } + // SparklyPaper end + this.player.onGround = isOnGround; // CraftBukkit - SPIGOT-5810, SPIGOT-5835, SPIGOT-6828: reset by this.player.move // SparklyPaper - Add PlayerPreMoveEvent 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) { @@ -1551,7 +1581,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl if (teleportBack) { // Paper end - Add fail move event this.internalTeleport(d3, d4, d5, f, f1); // CraftBukkit - SPIGOT-1807: Don't call teleport event, when the client thinks the player is falling, because the chunks are not loaded on the client yet. - this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, packet.isOnGround()); + this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, isOnGround); // SparklyPaper - Add PlayerPreMoveEvent } else { // CraftBukkit start - fire PlayerMoveEvent // Reset to old location first @@ -1626,15 +1656,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl this.player.serverLevel().getChunkSource().move(this.player); Vec3 vec3d = new Vec3(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5); - this.player.setOnGroundWithMovement(packet.isOnGround(), packet.horizontalCollision(), vec3d); - this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, packet.isOnGround()); + this.player.setOnGroundWithMovement(isOnGround, horizontalCollision, vec3d); // SparklyPaper - Add PlayerPreMoveEvent + this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, isOnGround); // SparklyPaper - Add PlayerPreMoveEvent this.player.recordMovementThroughBlocks(new Vec3(d3, d4, d5), this.player.position()); this.handlePlayerKnownMovement(vec3d); if (flag1) { this.player.resetFallDistance(); } - if (packet.isOnGround() || this.player.hasLandedInLiquid() || this.player.onClimbable() || this.player.isSpectator() || flag || flag4) { + if (isOnGround || this.player.hasLandedInLiquid() || this.player.onClimbable() || this.player.isSpectator() || flag || flag4) { // SparklyPaper - Add PlayerPreMoveEvent this.player.tryResetCurrentImpulseContext(); }