Minestom now properly adapts when a player changes their render distance, also fixed a bug that pointers to chunks that were unloaded were kept in the viewableChunks list until the player's instance was changed.

This commit is contained in:
Eoghanmc22 2020-08-13 17:54:55 -04:00
parent d2f6c08c4e
commit cc01a48cf1
2 changed files with 9 additions and 4 deletions

View File

@ -984,7 +984,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
final boolean isPlayer = this instanceof Player;
if (isPlayer)
((Player) this).onChunkChange(lastChunk, newChunk); // Refresh loaded chunk
((Player) this).onChunkChange(newChunk); // Refresh loaded chunk
// Refresh entity viewable list
final long[] lastVisibleChunksEntity = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), MinecraftServer.ENTITY_VIEW_DISTANCE);

View File

@ -1169,11 +1169,15 @@ public class Player extends LivingEntity implements CommandSender {
* It does remove and add the player from the chunks viewers list when removed or added
* It also calls the events {@link PlayerChunkUnloadEvent} and {@link PlayerChunkLoadEvent}
*
* @param lastChunk the last player chunk
* @param newChunk the current/new player chunk
*/
protected void onChunkChange(Chunk lastChunk, Chunk newChunk) {
final long[] lastVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * lastChunk.getChunkX(), 0, 16 * lastChunk.getChunkZ()), getChunkRange());
protected void onChunkChange(Chunk newChunk) {
final long[] lastVisibleChunks = new long[viewableChunks.size()];
Chunk[] lastViewableChunks = viewableChunks.toArray(new Chunk[0]);
for (int i = 0; i < lastViewableChunks.length; i++) {
Chunk lastViewableChunk = lastViewableChunks[i];
lastVisibleChunks[i] = ChunkUtils.getChunkIndex(lastViewableChunk.getChunkX(), lastViewableChunk.getChunkZ());
}
final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(new Position(16 * newChunk.getChunkX(), 0, 16 * newChunk.getChunkZ()), getChunkRange());
final int[] oldChunks = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks);
final int[] newChunks = ArrayUtils.getDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks);
@ -1187,6 +1191,7 @@ public class Player extends LivingEntity implements CommandSender {
playerConnection.sendPacket(unloadChunkPacket);
Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]);
viewableChunks.remove(chunk);
if (chunk != null)
chunk.removeViewer(this);
}