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.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;
13 import java.util.HashSet;
17 public class CacheProvider implements IBoundingBoxProvider<AbstractBoundingBox> {
18 private static final int CHUNK_SIZE = 16;
20 private final GetCache getCache;
22 public CacheProvider(GetCache getCache) {
23 this.getCache = getCache;
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);
33 return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
37 public Iterable<AbstractBoundingBox> get(DimensionId dimensionId) {
38 Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
40 Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
41 BoundingBoxCache cache = getCache.apply(dimensionId);
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);
53 boundingBoxes.add(key);