Fixed instance switch while unloading chunk

This commit is contained in:
Felix Cravic 2020-06-01 23:55:11 +02:00
parent b7b334645f
commit 895a4fd4bd
2 changed files with 16 additions and 8 deletions

View File

@ -295,9 +295,13 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
/** /**
* @param chunk the chunk to get the entities from * @param chunk the chunk to get the entities from
* @return an unmodifiable set containing all the entities in a chunk * @return an unmodifiable set containing all the entities in a chunk,
* if {@code chunk} is null, return an empty {@link HashSet}
*/ */
public Set<Entity> getChunkEntities(Chunk chunk) { public Set<Entity> getChunkEntities(Chunk chunk) {
if (chunk == null)
return new HashSet<>();
long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ()); long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
return Collections.unmodifiableSet(getEntitiesInChunk(index)); return Collections.unmodifiableSet(getEntitiesInChunk(index));
} }
@ -545,14 +549,16 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param chunk the chunk where the entity will be removed * @param chunk the chunk where the entity will be removed
*/ */
public void removeEntityFromChunk(Entity entity, Chunk chunk) { public void removeEntityFromChunk(Entity entity, Chunk chunk) {
long chunkIndex = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
synchronized (chunkEntities) { synchronized (chunkEntities) {
Set<Entity> entities = getEntitiesInChunk(chunkIndex); if (chunk != null) {
entities.remove(entity); long chunkIndex = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
if (entities.isEmpty()) { Set<Entity> entities = getEntitiesInChunk(chunkIndex);
this.chunkEntities.remove(chunkIndex); entities.remove(entity);
} else { if (entities.isEmpty()) {
this.chunkEntities.put(chunkIndex, entities); this.chunkEntities.remove(chunkIndex);
} else {
this.chunkEntities.put(chunkIndex, entities);
}
} }
if (entity instanceof Player) { if (entity instanceof Player) {

View File

@ -314,6 +314,8 @@ public class InstanceContainer extends Instance {
} }
this.chunks.remove(index); this.chunks.remove(index);
this.chunkEntities.remove(index);
chunk.unload(); chunk.unload();
} }