Improve movement cancelling

This commit is contained in:
TheMode 2021-08-28 11:28:14 +02:00
parent 4680dbd64e
commit 34ba838ab5
2 changed files with 26 additions and 6 deletions

View File

@ -1553,6 +1553,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
playerConnection.sendPacket(new UpdateViewPositionPacket(chunkX, chunkZ)); playerConnection.sendPacket(new UpdateViewPositionPacket(chunkX, chunkZ));
} }
public int getNextTeleportId() {
return teleportId.incrementAndGet();
}
public int getLastSentTeleportId() { public int getLastSentTeleportId() {
return teleportId.get(); return teleportId.get();
} }
@ -1572,7 +1576,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
@ApiStatus.Internal @ApiStatus.Internal
protected void synchronizePosition(boolean includeSelf) { protected void synchronizePosition(boolean includeSelf) {
if (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); super.synchronizePosition(includeSelf);
} }

View File

@ -6,6 +6,8 @@ import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.player.PlayerMoveEvent; import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.client.play.*; 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 net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -54,14 +56,28 @@ public class PlayerPositionListener {
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition); PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition);
GlobalHandles.PLAYER_MOVE.call(playerMoveEvent); GlobalHandles.PLAYER_MOVE.call(playerMoveEvent);
// True if the event call changed the player position (possibly a teleport) if (!currentPosition.equals(player.getPosition())) {
if (!playerMoveEvent.isCancelled() && currentPosition.equals(player.getPosition())) { // Player has been teleported in the event
// Move the player 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.refreshPosition(playerMoveEvent.getNewPosition());
player.refreshOnGround(onGround); player.refreshOnGround(onGround);
} else { } else {
// Cancelled, teleport to previous position // Position modified by the event
player.teleport(player.getPosition()); if (newPosition.samePoint(eventPosition)) {
player.setView(eventPosition.yaw(), eventPosition.pitch());
} else {
player.teleport(eventPosition);
}
} }
} }
} }