    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 var3 = (long)par1 & 4294967295L | ((long)par2 & 4294967295L) << 32;
    036        BiomeCacheBlock var5 = (BiomeCacheBlock)this.cacheMap.getValueByKey(var3);
    037
    038        if (var5 == null)
    039        {
    040            var5 = new BiomeCacheBlock(this, par1, par2);
    041            this.cacheMap.add(var3, var5);
    042            this.cache.add(var5);
    043        }
    044
    045        var5.lastAccessTime = System.currentTimeMillis();
    046        return var5;
    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 var1 = System.currentTimeMillis();
    063        long var3 = var1 - this.lastCleanupTime;
    064
    065        if (var3 > 7500L || var3 < 0L)
    066        {
    067            this.lastCleanupTime = var1;
    068
    069            for (int var5 = 0; var5 < this.cache.size(); ++var5)
    070            {
    071                BiomeCacheBlock var6 = (BiomeCacheBlock)this.cache.get(var5);
    072                long var7 = var1 - var6.lastAccessTime;
    073
    074                if (var7 > 30000L || var7 < 0L)
    075                {
    076                    this.cache.remove(var5--);
    077                    long var9 = (long)var6.xPosition & 4294967295L | ((long)var6.zPosition & 4294967295L) << 32;
    078                    this.cacheMap.remove(var9);
    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}