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