    001package net.minecraft.world.biome;
    002
    003import java.util.ArrayList;
    004import java.util.List;
    005import net.minecraft.util.LongHashMap;
    006
    007public class BiomeCache
    008{
    009    /** Reference to the WorldChunkManager */
    010    private final WorldChunkManager chunkManager;
    011
    012    /** The last time this BiomeCache was cleaned, in milliseconds. */
    013    private long lastCleanupTime = 0L;
    014
    015    /**
    016     * The map of keys to BiomeCacheBlocks. Keys are based on the chunk x, z coordinates as (x | z << 32).
    017     */
    018    private LongHashMap cacheMap = new LongHashMap();
    019
    020    /** The list of cached BiomeCacheBlocks */
    021    private List cache = new ArrayList();
    022
    023    public BiomeCache(WorldChunkManager par1WorldChunkManager)
    024    {
    025        this.chunkManager = par1WorldChunkManager;
    026    }
    027
    028    /**
    029     * Returns a biome cache block at location specified.
    030     */
    031    public BiomeCacheBlock getBiomeCacheBlock(int par1, int par2)
    032    {
    033        par1 >>= 4;
    034        par2 >>= 4;
    035        long k = (long)par1 & 4294967295L | ((long)par2 & 4294967295L) << 32;
    036        BiomeCacheBlock biomecacheblock = (BiomeCacheBlock)this.cacheMap.getValueByKey(k);
    037
    038        if (biomecacheblock == null)
    039        {
    040            biomecacheblock = new BiomeCacheBlock(this, par1, par2);
    041            this.cacheMap.add(k, biomecacheblock);
    042            this.cache.add(biomecacheblock);
    043        }
    044
    045        biomecacheblock.lastAccessTime = System.currentTimeMillis();
    046        return biomecacheblock;
    047    }
    048
    049    /**
    050     * Returns the BiomeGenBase related to the x, z position from the cache.
    051     */
    052    public BiomeGenBase getBiomeGenAt(int par1, int par2)
    053    {
    054        return this.getBiomeCacheBlock(par1, par2).getBiomeGenAt(par1, par2);
    055    }
    056
    057    /**
    058     * Removes BiomeCacheBlocks from this cache that haven't been accessed in at least 30 seconds.
    059     */
    060    public void cleanupCache()
    061    {
    062        long i = System.currentTimeMillis();
    063        long j = i - this.lastCleanupTime;
    064
    065        if (j > 7500L || j < 0L)
    066        {
    067            this.lastCleanupTime = i;
    068
    069            for (int k = 0; k < this.cache.size(); ++k)
    070            {
    071                BiomeCacheBlock biomecacheblock = (BiomeCacheBlock)this.cache.get(k);
    072                long l = i - biomecacheblock.lastAccessTime;
    073
    074                if (l > 30000L || l < 0L)
    075                {
    076                    this.cache.remove(k--);
    077                    long i1 = (long)biomecacheblock.xPosition & 4294967295L | ((long)biomecacheblock.zPosition & 4294967295L) << 32;
    078                    this.cacheMap.remove(i1);
    079                }
    080            }
    081        }
    082    }
    083
    084    /**
    085     * Returns the array of cached biome types in the BiomeCacheBlock at the given location.
    086     */
    087    public BiomeGenBase[] getCachedBiomes(int par1, int par2)
    088    {
    089        return this.getBiomeCacheBlock(par1, par2).biomes;
    090    }
    091
    092    /**
    093     * Get the world chunk manager object for a biome list.
    094     */
    095    static WorldChunkManager getChunkManager(BiomeCache par0BiomeCache)
    096    {
    097        return par0BiomeCache.chunkManager;
    098    }
    099}