]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Add CacheProvider and simplify ClientRenderer
authorIrtimaled <irtimaled@gmail.com>
Fri, 4 Oct 2019 05:31:29 +0000 (22:31 -0700)
committerIrtimaled <irtimaled@gmail.com>
Tue, 19 Nov 2019 04:26:15 +0000 (20:26 -0800)
src/main/java/com/irtimaled/bbor/client/ClientProxy.java
src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
src/main/java/com/irtimaled/bbor/client/GetCache.java
src/main/java/com/irtimaled/bbor/client/providers/CacheProvider.java [new file with mode: 0644]

index fba55ed62c8ded7bd8b23ed7c02768e8b757f3bb..9fb1d6462f627c1b3d76e9c39533f22e2a867875 100644 (file)
@@ -38,7 +38,8 @@ public class ClientProxy extends CommonProxy {
                 .registerProvider(new WorldSpawnProvider())
                 .registerProvider(new SpawningSphereProvider())
                 .registerProvider(new BeaconProvider())
-                .registerProvider(new CustomBoxProvider());
+                .registerProvider(new CustomBoxProvider())
+                .registerProvider(new CacheProvider(this::getCache));
 
         KeyListener.init();
     }
index 3ddea1a4333f41ec01a27a900757b0f6f64f462f..6bd465276ed5238c2683edea78af2e03584767c2 100644 (file)
@@ -72,7 +72,7 @@ public class ClientRenderer {
     public void render(int dimensionId) {
         if(!active) return;
 
-        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxes = getBoundingBoxes(dimensionId);
+        Set<AbstractBoundingBox> boundingBoxes = getBoundingBoxes(dimensionId);
 
         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
         GL11.glLineWidth(2.0f);
@@ -83,21 +83,12 @@ public class ClientRenderer {
             GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
         }
 
-        Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
-        for (Map.Entry<AbstractBoundingBox, Set<AbstractBoundingBox>> entry : boundingBoxes.entrySet()) {
-            AbstractBoundingBox key = entry.getKey();
-            if (!key.shouldRender()) continue;
+        for (AbstractBoundingBox key : boundingBoxes) {
+            if (!key.shouldRender() || !isWithinRenderDistance(key)) continue;
 
             AbstractRenderer renderer = boundingBoxRendererMap.get(key.getClass());
             if (renderer == null) continue;
 
-            if (!outerBoxesOnly) {
-                Set<AbstractBoundingBox> children = entry.getValue();
-                if (children != null && children.size() > 0) {
-                    children.forEach(renderer::render);
-                    continue;
-                }
-            }
             renderer.render(key);
         }
 
@@ -106,21 +97,11 @@ public class ClientRenderer {
         GL11.glEnable(GL11.GL_TEXTURE_2D);
     }
 
-    private Map<AbstractBoundingBox, Set<AbstractBoundingBox>> getBoundingBoxes(int dimensionId) {
-        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxes = new HashMap<>();
+    private Set<AbstractBoundingBox> getBoundingBoxes(int dimensionId) {
+        Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
         for(IBoundingBoxProvider<?> provider: providers) {
             for (AbstractBoundingBox boundingBox : provider.get(dimensionId)) {
-                boundingBoxes.put(boundingBox, null);
-            }
-        }
-
-        BoundingBoxCache cache = getCache.apply(dimensionId);
-        if (cache != null) {
-            for (Map.Entry<AbstractBoundingBox, Set<AbstractBoundingBox>> entry : cache.getBoundingBoxes().entrySet()) {
-                AbstractBoundingBox key = entry.getKey();
-                if (key.shouldRender() && isWithinRenderDistance(key)) {
-                    boundingBoxes.put(key, entry.getValue());
-                }
+                boundingBoxes.add(boundingBox);
             }
         }
         return boundingBoxes;
index 1f4adba00522d495863c39a749a499ce1fa49329..332d86ae11326020ba4f6b06112b96709f8fc23b 100644 (file)
@@ -4,5 +4,5 @@ import com.irtimaled.bbor.common.BoundingBoxCache;
 
 import java.util.function.Function;
 
-interface GetCache extends Function<Integer, BoundingBoxCache> {
+public interface GetCache extends Function<Integer, BoundingBoxCache> {
 }
diff --git a/src/main/java/com/irtimaled/bbor/client/providers/CacheProvider.java b/src/main/java/com/irtimaled/bbor/client/providers/CacheProvider.java
new file mode 100644 (file)
index 0000000..3e43f69
--- /dev/null
@@ -0,0 +1,57 @@
+package com.irtimaled.bbor.client.providers;
+
+import com.irtimaled.bbor.client.GetCache;
+import com.irtimaled.bbor.client.PlayerCoords;
+import com.irtimaled.bbor.client.interop.ClientInterop;
+import com.irtimaled.bbor.common.BoundingBoxCache;
+import com.irtimaled.bbor.common.MathHelper;
+import com.irtimaled.bbor.common.models.AbstractBoundingBox;
+import com.irtimaled.bbor.config.ConfigManager;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class CacheProvider implements IBoundingBoxProvider<AbstractBoundingBox> {
+    private static final int CHUNK_SIZE = 16;
+
+    private final GetCache getCache;
+
+    public CacheProvider(GetCache getCache) {
+        this.getCache = getCache;
+    }
+
+    private static boolean isWithinRenderDistance(AbstractBoundingBox boundingBox) {
+        int renderDistanceBlocks = ClientInterop.getRenderDistanceChunks() * CHUNK_SIZE;
+        int minX = MathHelper.floor(PlayerCoords.getX() - renderDistanceBlocks);
+        int maxX = MathHelper.floor(PlayerCoords.getX() + renderDistanceBlocks);
+        int minZ = MathHelper.floor(PlayerCoords.getZ() - renderDistanceBlocks);
+        int maxZ = MathHelper.floor(PlayerCoords.getZ() + renderDistanceBlocks);
+
+        return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
+    }
+
+    @Override
+    public Iterable<AbstractBoundingBox> get(int dimensionId) {
+        Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
+
+        Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
+        BoundingBoxCache cache = getCache.apply(dimensionId);
+        if (cache != null) {
+            for (Map.Entry<AbstractBoundingBox, Set<AbstractBoundingBox>> entry : cache.getBoundingBoxes().entrySet()) {
+                AbstractBoundingBox key = entry.getKey();
+                if (key.shouldRender() && isWithinRenderDistance(key)) {
+                    if (!outerBoxesOnly) {
+                        Set<AbstractBoundingBox> children = entry.getValue();
+                        if (children != null && children.size() > 0) {
+                            boundingBoxes.addAll(children);
+                            continue;
+                        }
+                    }
+                    boundingBoxes.add(key);
+                }
+            }
+        }
+        return boundingBoxes;
+    }
+}