mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 21:17:53 +01:00
Improve movement cancelling
This commit is contained in:
parent
4680dbd64e
commit
34ba838ab5
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user