Remove entities other than players when unloading a chunk

This commit is contained in:
Felix Cravic 2020-06-02 00:03:03 +02:00
parent 895a4fd4bd
commit 50373505b3
2 changed files with 11 additions and 0 deletions

View File

@ -105,6 +105,10 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
public abstract void loadOptionalChunk(int chunkX, int chunkZ, Consumer<Chunk> callback);
/**
* Unload a chunk
* <p>
* WARNING: all entities other than {@link Player} will be removed
*
* @param chunk the chunk to unload
*/
public abstract void unloadChunk(Chunk chunk);

View File

@ -313,6 +313,13 @@ public class InstanceContainer extends Instance {
chunk.removeViewer(viewer);
}
// Remove all entities in chunk
getChunkEntities(chunk).forEach(entity -> {
if (!(entity instanceof Player))
entity.remove();
});
// Clear cache
this.chunks.remove(index);
this.chunkEntities.remove(index);