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