mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 14:13:24 +02:00
Cache entity current chunk
This commit is contained in:
parent
aaab620839
commit
c74946cc3c
@ -64,6 +64,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
|
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
|
||||||
|
|
||||||
protected Instance instance;
|
protected Instance instance;
|
||||||
|
protected Chunk currentChunk;
|
||||||
protected final Position position;
|
protected final Position position;
|
||||||
protected double lastX, lastY, lastZ;
|
protected double lastX, lastY, lastZ;
|
||||||
protected double cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
protected double cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
||||||
@ -566,11 +567,16 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
|
|
||||||
// World border collision
|
// World border collision
|
||||||
final Position finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
final Position finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
||||||
final Chunk finalChunk = instance.getChunkAt(finalVelocityPosition);
|
final Chunk finalChunk;
|
||||||
|
if (!ChunkUtils.same(position, finalVelocityPosition)) {
|
||||||
|
finalChunk = instance.getChunkAt(finalVelocityPosition);
|
||||||
|
|
||||||
// Entity shouldn't be updated when moving in an unloaded chunk
|
// Entity shouldn't be updated when moving in an unloaded chunk
|
||||||
if (!ChunkUtils.isLoaded(finalChunk)) {
|
if (!ChunkUtils.isLoaded(finalChunk)) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finalChunk = getChunk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the position if changed
|
// Apply the position if changed
|
||||||
@ -816,9 +822,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
*
|
*
|
||||||
* @return the entity chunk, can be null even if unlikely
|
* @return the entity chunk, can be null even if unlikely
|
||||||
*/
|
*/
|
||||||
@Nullable
|
public @Nullable Chunk getChunk() {
|
||||||
public Chunk getChunk() {
|
return currentChunk;
|
||||||
return instance.getChunkAt(position.getX(), position.getZ());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -826,8 +831,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
*
|
*
|
||||||
* @return the entity instance, can be null if the entity doesn't have an instance yet
|
* @return the entity instance, can be null if the entity doesn't have an instance yet
|
||||||
*/
|
*/
|
||||||
@Nullable
|
public @Nullable Instance getInstance() {
|
||||||
public Instance getInstance() {
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -855,6 +859,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
|
|
||||||
this.isActive = true;
|
this.isActive = true;
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
|
this.currentChunk = instance.getChunkAt(position.getX(), position.getZ());
|
||||||
instance.UNSAFE_addEntity(this);
|
instance.UNSAFE_addEntity(this);
|
||||||
spawn();
|
spawn();
|
||||||
EntitySpawnEvent entitySpawnEvent = new EntitySpawnEvent(this, instance);
|
EntitySpawnEvent entitySpawnEvent = new EntitySpawnEvent(this, instance);
|
||||||
@ -1309,8 +1314,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
|
|
||||||
final Instance instance = getInstance();
|
final Instance instance = getInstance();
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
final int lastChunkX = ChunkUtils.getChunkCoordinate(lastX);
|
final int lastChunkX = currentChunk.getChunkX();
|
||||||
final int lastChunkZ = ChunkUtils.getChunkCoordinate(lastZ);
|
final int lastChunkZ = currentChunk.getChunkZ();
|
||||||
|
|
||||||
final int newChunkX = ChunkUtils.getChunkCoordinate(x);
|
final int newChunkX = ChunkUtils.getChunkCoordinate(x);
|
||||||
final int newChunkZ = ChunkUtils.getChunkCoordinate(z);
|
final int newChunkZ = ChunkUtils.getChunkCoordinate(z);
|
||||||
@ -1330,6 +1335,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
|
|||||||
player.refreshVisibleChunks(newChunk);
|
player.refreshVisibleChunks(newChunk);
|
||||||
player.refreshVisibleEntities(newChunk);
|
player.refreshVisibleEntities(newChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.currentChunk = newChunk;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +84,14 @@ public final class ChunkUtils {
|
|||||||
return isLoaded(chunk);
|
return isLoaded(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean same(@NotNull Position pos1, @NotNull Position pos2) {
|
||||||
|
final int x1 = ChunkUtils.getChunkCoordinate(pos1.getX());
|
||||||
|
final int z1 = ChunkUtils.getChunkCoordinate(pos1.getZ());
|
||||||
|
final int x2 = ChunkUtils.getChunkCoordinate(pos2.getX());
|
||||||
|
final int z2 = ChunkUtils.getChunkCoordinate(pos2.getZ());
|
||||||
|
return x1 != x2 || z1 != z2;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param xz the instance coordinate to convert
|
* @param xz the instance coordinate to convert
|
||||||
* @return the chunk X or Z based on the argument
|
* @return the chunk X or Z based on the argument
|
||||||
|
Loading…
Reference in New Issue
Block a user