1 package com.irtimaled.bbor.client.providers;
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;
12 import java.util.HashSet;
16 public class CacheProvider implements IBoundingBoxProvider<AbstractBoundingBox> {
17 private static final int CHUNK_SIZE = 16;
19 private final GetCache getCache;
21 public CacheProvider(GetCache getCache) {
22 this.getCache = getCache;
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);
32 return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
36 public Iterable<AbstractBoundingBox> get(int dimensionId) {
37 Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
39 Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
40 BoundingBoxCache cache = getCache.apply(dimensionId);
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);
52 boundingBoxes.add(key);