]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/CacheProvider.java
be43ff0792fd1b39d9b6df5e4c27e65aafc9a373
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / providers / CacheProvider.java
1 package com.irtimaled.bbor.client.providers;
2
3 import com.irtimaled.bbor.client.GetCache;
4 import com.irtimaled.bbor.client.Player;
5 import com.irtimaled.bbor.client.interop.ClientInterop;
6 import com.irtimaled.bbor.common.BoundingBoxCache;
7 import com.irtimaled.bbor.common.MathHelper;
8 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
9 import com.irtimaled.bbor.config.ConfigManager;
10
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14
15 public class CacheProvider implements IBoundingBoxProvider<AbstractBoundingBox> {
16     private static final int CHUNK_SIZE = 16;
17
18     private final GetCache getCache;
19
20     public CacheProvider(GetCache getCache) {
21         this.getCache = getCache;
22     }
23
24     private static boolean isWithinRenderDistance(AbstractBoundingBox boundingBox) {
25         int renderDistanceBlocks = ClientInterop.getRenderDistanceChunks() * CHUNK_SIZE;
26         int minX = MathHelper.floor(Player.getX() - renderDistanceBlocks);
27         int maxX = MathHelper.floor(Player.getX() + renderDistanceBlocks);
28         int minZ = MathHelper.floor(Player.getZ() - renderDistanceBlocks);
29         int maxZ = MathHelper.floor(Player.getZ() + renderDistanceBlocks);
30
31         return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
32     }
33
34     @Override
35     public Iterable<AbstractBoundingBox> get(int dimensionId) {
36         Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
37
38         Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
39         BoundingBoxCache cache = getCache.apply(dimensionId);
40         if (cache != null) {
41             for (Map.Entry<AbstractBoundingBox, Set<AbstractBoundingBox>> entry : cache.getBoundingBoxes().entrySet()) {
42                 AbstractBoundingBox key = entry.getKey();
43                 if (key.shouldRender() && isWithinRenderDistance(key)) {
44                     if (!outerBoxesOnly) {
45                         Set<AbstractBoundingBox> children = entry.getValue();
46                         if (children != null && children.size() > 0) {
47                             boundingBoxes.addAll(children);
48                             continue;
49                         }
50                     }
51                     boundingBoxes.add(key);
52                 }
53             }
54         }
55         return boundingBoxes;
56     }
57 }