aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/mod/utils/BlockCache.java
blob: b83137789ba557972463366738753c9408e90180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package kr.syeyoung.dungeonsguide.mod.utils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import kr.syeyoung.dungeonsguide.mod.features.FeatureRegistry;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.util.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;

public class BlockCache {
    public BlockCache() {
        MinecraftForge.EVENT_BUS.register(this);
    }

    @SuppressWarnings("UnstableApiUsage")
    private final LoadingCache<BlockPos, IBlockState> cache = CacheBuilder.newBuilder()
            .maximumSize(10000)
            .expireAfterWrite(3, TimeUnit.SECONDS)
            .build(
                    new CacheLoader<BlockPos, IBlockState>() {
                        public IBlockState load(@NotNull BlockPos pos) {
                            return Minecraft.getMinecraft().theWorld.getBlockState(pos);
                        }
                    });


    @SuppressWarnings("UnstableApiUsage")
    public IBlockState getBlockState(@NotNull BlockPos pos){
        if(FeatureRegistry.DEBUG_BLOCK_CACHING.isEnabled()){
            return cache.getUnchecked(pos);
        } else {
            return Minecraft.getMinecraft().theWorld.getBlockState(pos);
        }
    }


    @SubscribeEvent @SuppressWarnings("UnstableApiUsage")
    public void onWorldLoad(WorldEvent.Load e){
        cache.invalidateAll();
    }


}