Search in sources :

Example 1 with IOTask

use of io.xol.chunkstories.world.io.IOTasks.IOTask in project chunkstories by Hugobros3.

the class ChunkHolderImplementation method unloadChunk.

private void unloadChunk() {
    chunkLock.writeLock().lock();
    CubicChunk chunk = this.chunk;
    if (chunk == null) {
        chunkLock.writeLock().unlock();
        return;
    }
    // Unlist it immediately
    regionLoadedChunks.remove(chunk);
    this.chunk = null;
    // Remove the entities from this chunk from the world
    region.world.entitiesLock.writeLock().lock();
    Iterator<Entity> i = chunk.localEntities.iterator();
    while (i.hasNext()) {
        Entity entity = i.next();
        if (entity instanceof EntityControllable && ((EntityControllable) entity).getController() != null) {
            // give grace to controlled entities
            continue;
        } else {
            region.world.removeEntityFromList(entity);
        }
    }
    region.world.entitiesLock.writeLock().unlock();
    // Lock it down
    chunk.entitiesLock.lock();
    // Kill any load chunk operation that is still scheduled
    if (loadChunkTask != null) {
        IOTask task = loadChunkTask;
        if (task != null)
            task.cancel();
        loadChunkTask = null;
    }
    // Compress chunk one last time before it has to go
    setCompressedData(compressChunkData(chunk));
    // destroy it (returns any internal data using up ressources)
    chunk.destroy();
    // unlock it (whoever messes with it now, his problem)
    chunk.entitiesLock.unlock();
    chunkLock.writeLock().unlock();
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) IOTask(io.xol.chunkstories.world.io.IOTasks.IOTask) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Aggregations

Entity (io.xol.chunkstories.api.entity.Entity)1 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)1 IOTask (io.xol.chunkstories.world.io.IOTasks.IOTask)1