diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b01fdec8e..21b8384e0 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1445,16 +1445,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable, // New chunks indexes final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(newChunk.toPosition(), getChunkRange()); - // Find the difference between the two arrays - final int[] oldChunks = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks); - final int[] newChunks = ArrayUtils.getDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks); - // Update client render distance updateViewPosition(newChunk.getChunkX(), newChunk.getChunkZ()); // Unload old chunks - for (int index : oldChunks) { - final long chunkIndex = lastVisibleChunks[index]; + ArrayUtils.forDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks, chunkIndex -> { final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex); final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex); @@ -1467,14 +1462,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable, if (chunk != null) { chunk.removeViewer(this); } - } - + }); // Load new chunks - for (int index : newChunks) { - final long chunkIndex = updatedVisibleChunks[index]; + ArrayUtils.forDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks, chunkIndex -> { final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex); final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex); - this.instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(chunk -> { if (chunk == null) { // Cannot load chunk (auto load is not enabled) @@ -1482,7 +1474,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } chunk.addViewer(this); }); - } + }); } public void refreshVisibleChunks() { diff --git a/src/main/java/net/minestom/server/utils/ArrayUtils.java b/src/main/java/net/minestom/server/utils/ArrayUtils.java index 7efc02f6d..e7c58e1c2 100644 --- a/src/main/java/net/minestom/server/utils/ArrayUtils.java +++ b/src/main/java/net/minestom/server/utils/ArrayUtils.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.util.Objects; +import java.util.function.LongConsumer; @ApiStatus.Internal public final class ArrayUtils { @@ -30,19 +31,9 @@ public final class ArrayUtils { System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index); } - /** - * Gets the differences between 2 arrays. - * - * @param a the first array - * @param b the second array - * @return an array containing a's indexes that aren't in b array - */ - public static int @NotNull [] getDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b) { - int counter = 0; - int[] indexes = new int[Math.max(a.length, b.length)]; - - for (int i = 0; i < a.length; i++) { - final long aValue = a[i]; + public static void forDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b, + @NotNull LongConsumer consumer) { + for (final long aValue : a) { boolean contains = false; for (final long bValue : b) { if (bValue == aValue) { @@ -51,14 +42,9 @@ public final class ArrayUtils { } } if (!contains) { - indexes[counter++] = i; + consumer.accept(aValue); } } - - // Resize array - int[] result = new int[counter]; - System.arraycopy(indexes, 0, result, 0, counter); - return result; } public static int @NotNull [] toArray(@NotNull IntList list) {