Limit chunk map lookup while moving

This commit is contained in:
TheMode 2021-07-20 06:17:13 +02:00
parent 12e430db69
commit bca3324b56
2 changed files with 7 additions and 9 deletions

View File

@ -61,11 +61,6 @@ public class PlayerPositionListener {
if (player.getLastSentTeleportId() != player.getLastReceivedTeleportId()) {
return;
}
// Try to move in an unloaded chunk, prevent it
if (!ChunkUtils.isLoaded(instance, x, z)) {
player.teleport(player.getPosition());
return;
}
final var currentPosition = player.getPosition();
final var newPosition = new Pos(x, y, z, yaw, pitch);
@ -74,6 +69,12 @@ public class PlayerPositionListener {
return;
}
// Try to move in an unloaded chunk, prevent it
if (!currentPosition.sameChunk(newPosition) && !ChunkUtils.isLoaded(instance, x, z)) {
player.teleport(player.getPosition());
return;
}
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition);
EventDispatcher.call(playerMoveEvent);

View File

@ -77,10 +77,7 @@ public final class ChunkUtils {
* @return true if the chunk is loaded, false otherwise
*/
public static boolean isLoaded(@NotNull Instance instance, double x, double z) {
final int chunkX = getChunkCoordinate(x);
final int chunkZ = getChunkCoordinate(z);
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
final Chunk chunk = instance.getChunk(getChunkCoordinate(x), getChunkCoordinate(z));
return isLoaded(chunk);
}