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.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;
11 import java.util.HashSet;
15 public class CacheProvider implements IBoundingBoxProvider<AbstractBoundingBox> {
16 private static final int CHUNK_SIZE = 16;
18 private final GetCache getCache;
20 public CacheProvider(GetCache getCache) {
21 this.getCache = getCache;
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);
31 return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
35 public Iterable<AbstractBoundingBox> get(int dimensionId) {
36 Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
38 Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
39 BoundingBoxCache cache = getCache.apply(dimensionId);
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);
51 boundingBoxes.add(key);