Optimize unnecessary chunk coordinate conversion

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-11 01:04:11 +02:00
parent 73237bb11b
commit 0c5b37ed18
2 changed files with 16 additions and 12 deletions

View File

@ -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 -> {

View File

@ -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.
*