mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 21:17:53 +01:00
Optimize unnecessary chunk coordinate conversion
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
73237bb11b
commit
0c5b37ed18
@ -556,9 +556,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
}
|
||||
|
||||
if (updateChunks) {
|
||||
// Warning: loop to remove once `refreshVisibleChunks` manage it
|
||||
previousChunks.forEach(chunk ->
|
||||
playerConnection.sendPacket(new UnloadChunkPacket(chunk.getChunkX(), chunk.getChunkZ())));
|
||||
refreshVisibleChunks();
|
||||
}
|
||||
|
||||
@ -1169,13 +1166,16 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
* @param newChunk the current/new player chunk (can be the current one)
|
||||
*/
|
||||
public void refreshVisibleChunks(@NotNull Chunk newChunk) {
|
||||
final int newChunkX = newChunk.getChunkX();
|
||||
final int newChunkZ = newChunk.getChunkZ();
|
||||
final int range = getChunkRange();
|
||||
// Previous chunks indexes
|
||||
final long[] lastVisibleChunks = viewableChunks.stream().mapToLong(ChunkUtils::getChunkIndex).toArray();
|
||||
// New chunks indexes
|
||||
final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(newChunk.toPosition(), getChunkRange());
|
||||
final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(newChunkX, newChunkZ, range);
|
||||
|
||||
// Update client render distance
|
||||
updateViewPosition(newChunk.getChunkX(), newChunk.getChunkZ());
|
||||
updateViewPosition(newChunkX, newChunkZ);
|
||||
|
||||
// Unload old chunks
|
||||
ArrayUtils.forDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks, chunkIndex -> {
|
||||
|
@ -156,23 +156,27 @@ public final class ChunkUtils {
|
||||
/**
|
||||
* Gets the chunks in range of a position.
|
||||
*
|
||||
* @param point the initial point
|
||||
* @param range how far should it retrieves chunk
|
||||
* @param chunkX the initial chunk X
|
||||
* @param chunkZ the initial chunk Z
|
||||
* @param range how far should it retrieves chunk
|
||||
* @return an array containing chunks index
|
||||
*/
|
||||
public static long @NotNull [] getChunksInRange(@NotNull Point point, int range) {
|
||||
final int chunkX = point.chunkX();
|
||||
final int chunkZ = point.chunkZ();
|
||||
public static long @NotNull [] getChunksInRange(int chunkX, int chunkZ, int range) {
|
||||
// FIXME: currently broken using GraalVM
|
||||
long[] array = new long[MathUtils.square(range * 2 + 1)];
|
||||
int i = 0;
|
||||
for (int z = -range; z <= range; ++z) {
|
||||
for (int x = -range; x <= range; ++x) {
|
||||
for (int x = -range; x <= range; ++x) {
|
||||
for (int z = -range; z <= range; ++z) {
|
||||
array[i++] = getChunkIndex(chunkX + x, chunkZ + z);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static long @NotNull [] getChunksInRange(@NotNull Point point, int range) {
|
||||
return getChunksInRange(point.chunkX(), point.chunkZ(), range);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the block index of a position.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user