From 34ba838ab538907e23fd6440263065bff2134d96 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 28 Aug 2021 11:28:14 +0200 Subject: [PATCH] Improve movement cancelling --- .../net/minestom/server/entity/Player.java | 6 ++++- .../listener/PlayerPositionListener.java | 26 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 246c0618d..dd32d7506 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1553,6 +1553,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable, playerConnection.sendPacket(new UpdateViewPositionPacket(chunkX, chunkZ)); } + public int getNextTeleportId() { + return teleportId.incrementAndGet(); + } + public int getLastSentTeleportId() { return teleportId.get(); } @@ -1572,7 +1576,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, @ApiStatus.Internal protected void synchronizePosition(boolean includeSelf) { if (includeSelf) { - playerConnection.sendPacket(new PlayerPositionAndLookPacket(position, (byte) 0x00, teleportId.incrementAndGet(), false)); + playerConnection.sendPacket(new PlayerPositionAndLookPacket(position, (byte) 0x00, getNextTeleportId(), false)); } super.synchronizePosition(includeSelf); } diff --git a/src/main/java/net/minestom/server/listener/PlayerPositionListener.java b/src/main/java/net/minestom/server/listener/PlayerPositionListener.java index 52132c004..d8fba54ff 100644 --- a/src/main/java/net/minestom/server/listener/PlayerPositionListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerPositionListener.java @@ -6,6 +6,8 @@ import net.minestom.server.event.GlobalHandles; import net.minestom.server.event.player.PlayerMoveEvent; import net.minestom.server.instance.Instance; import net.minestom.server.network.packet.client.play.*; +import net.minestom.server.network.packet.server.play.PlayerPositionAndLookPacket; +import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.chunk.ChunkUtils; import org.jetbrains.annotations.NotNull; @@ -54,14 +56,28 @@ public class PlayerPositionListener { PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition); GlobalHandles.PLAYER_MOVE.call(playerMoveEvent); - // True if the event call changed the player position (possibly a teleport) - if (!playerMoveEvent.isCancelled() && currentPosition.equals(player.getPosition())) { - // Move the player + if (!currentPosition.equals(player.getPosition())) { + // Player has been teleported in the event + return; + } + if (playerMoveEvent.isCancelled()) { + // Teleport to previous position + PlayerConnection connection = player.getPlayerConnection(); + connection.sendPacket(new PlayerPositionAndLookPacket(currentPosition, (byte) 0x00, player.getNextTeleportId(), false)); + return; + } + final Pos eventPosition = playerMoveEvent.getNewPosition(); + if (newPosition.equals(eventPosition)) { + // Event didn't change the position player.refreshPosition(playerMoveEvent.getNewPosition()); player.refreshOnGround(onGround); } else { - // Cancelled, teleport to previous position - player.teleport(player.getPosition()); + // Position modified by the event + if (newPosition.samePoint(eventPosition)) { + player.setView(eventPosition.yaw(), eventPosition.pitch()); + } else { + player.teleport(eventPosition); + } } } }