// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.universe.world;

import com.hypixel.hytale.server.core.universe.world.accessor.BlockAccessor;
import com.hypixel.hytale.assetstore.AssetRegistry;
import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.accessor.IChunkAccessorSync;

@Deprecated
public interface IWorldChunks extends IChunkAccessorSync<WorldChunk>, IWorldChunksAsync
{
    @Deprecated
    void consumeTaskQueue();
    
    boolean isInThread();
    
    default WorldChunk getChunk(final long index) {
        final WorldChunk worldChunk = this.loadChunkIfInMemory(index);
        if (worldChunk != null) {
            return worldChunk;
        }
        final CompletableFuture<WorldChunk> future = this.getChunkAsync(index);
        return this.waitForFutureWithoutLock(future);
    }
    
    default WorldChunk getNonTickingChunk(final long index) {
        final WorldChunk worldChunk = this.getChunkIfInMemory(index);
        if (worldChunk != null) {
            return worldChunk;
        }
        final CompletableFuture<WorldChunk> future = this.getNonTickingChunkAsync(index);
        return this.waitForFutureWithoutLock(future);
    }
    
    default <T> T waitForFutureWithoutLock(@Nonnull final CompletableFuture<T> future) {
        if (!this.isInThread()) {
            return future.join();
        }
        AssetRegistry.ASSET_LOCK.readLock().unlock();
        while (!future.isDone()) {
            AssetRegistry.ASSET_LOCK.readLock().lock();
            try {
                this.consumeTaskQueue();
            }
            finally {
                AssetRegistry.ASSET_LOCK.readLock().unlock();
            }
            Thread.yield();
        }
        AssetRegistry.ASSET_LOCK.readLock().lock();
        return future.join();
    }
}
