]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
Move config to client module
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / ClientRenderer.java
index 0a197daf1805ee5b37bbe682ed5d4227cc4e7b0d..6672c6955d15f1e29752441734df31c53e49bdd4 100644 (file)
 package com.irtimaled.bbor.client;
 
+import com.irtimaled.bbor.client.config.ConfigManager;
+import com.irtimaled.bbor.client.interop.ClientInterop;
+import com.irtimaled.bbor.client.models.*;
+import com.irtimaled.bbor.client.providers.*;
 import com.irtimaled.bbor.client.renderers.*;
-import com.irtimaled.bbor.common.DimensionCache;
-import com.irtimaled.bbor.common.models.*;
-import com.irtimaled.bbor.config.ConfigManager;
-import net.minecraft.world.dimension.DimensionType;
+import com.irtimaled.bbor.common.MathHelper;
+import com.irtimaled.bbor.common.models.AbstractBoundingBox;
+import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
+import com.irtimaled.bbor.common.models.BoundingBoxVillage;
 import org.lwjgl.opengl.GL11;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 public class ClientRenderer {
-    private final ClientBoundingBoxProvider clientBoundingBoxProvider;
-    private static final Map<Class<? extends BoundingBox>, Renderer> boundingBoxRendererMap = new HashMap<>();
+    private static final int CHUNK_SIZE = 16;
+    private static final Map<Class<? extends AbstractBoundingBox>, AbstractRenderer> boundingBoxRendererMap = new HashMap<>();
 
-    ClientRenderer(DimensionCache dimensionCache) {
-        this.clientBoundingBoxProvider = new ClientBoundingBoxProvider(dimensionCache);
-        boundingBoxRendererMap.put(BoundingBoxVillage.class, new VillageRenderer());
-        boundingBoxRendererMap.put(BoundingBoxSlimeChunk.class, new SlimeChunkRenderer());
-        boundingBoxRendererMap.put(BoundingBoxWorldSpawn.class, new WorldSpawnRenderer());
-        boundingBoxRendererMap.put(BoundingBoxStructure.class, new StructureRenderer());
-        boundingBoxRendererMap.put(BoundingBoxMobSpawner.class, new MobSpawnerRenderer());
+    private static boolean active;
+    private static final Set<IBoundingBoxProvider> providers = new HashSet<>();
+
+    public static boolean getActive() {
+        return active;
+    }
+
+    public static void toggleActive() {
+        active = !active;
+        if (!active) return;
+
+        Player.setActiveY();
+    }
+
+    static void deactivate() {
+        active = false;
+    }
+
+    static {
+        registerRenderer(BoundingBoxVillage.class, new VillageRenderer());
+        registerRenderer(BoundingBoxSlimeChunk.class, new SlimeChunkRenderer());
+        registerRenderer(BoundingBoxWorldSpawn.class, new WorldSpawnRenderer());
+        registerRenderer(BoundingBoxCuboid.class, new CuboidRenderer());
+        registerRenderer(BoundingBoxMobSpawner.class, new MobSpawnerRenderer());
+        registerRenderer(BoundingBoxSpawningSphere.class, new SpawningSphereRenderer());
+        registerRenderer(BoundingBoxBeacon.class, new BeaconRenderer());
+        registerRenderer(BoundingBoxBiomeBorder.class, new BiomeBorderRenderer());
+        registerRenderer(BoundingBoxConduit.class, new ConduitRenderer());
+
+        registerProvider(new SlimeChunkProvider());
+        registerProvider(new WorldSpawnProvider());
+        registerProvider(new SpawningSphereProvider());
+        registerProvider(new BeaconProvider());
+        registerProvider(new CustomBoxProvider());
+        registerProvider(new CustomBeaconProvider());
+        registerProvider(new BiomeBorderProvider());
+        registerProvider(new MobSpawnerProvider());
+        registerProvider(new ConduitProvider());
+    }
+
+    public static <T extends AbstractBoundingBox> void registerProvider(IBoundingBoxProvider<T> provider) {
+        providers.add(provider);
+    }
+
+    public static <T extends AbstractBoundingBox> void registerRenderer(Class<? extends T> type, AbstractRenderer<T> renderer) {
+        boundingBoxRendererMap.put(type, renderer);
+    }
+
+    private static boolean isWithinRenderDistance(AbstractBoundingBox boundingBox) {
+        int renderDistanceBlocks = ClientInterop.getRenderDistanceChunks() * CHUNK_SIZE;
+        int minX = MathHelper.floor(Player.getX() - renderDistanceBlocks);
+        int maxX = MathHelper.floor(Player.getX() + renderDistanceBlocks);
+        int minZ = MathHelper.floor(Player.getZ() - renderDistanceBlocks);
+        int maxZ = MathHelper.floor(Player.getZ() + renderDistanceBlocks);
+
+        return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
     }
 
-    public void render(DimensionType dimensionType, Boolean outerBoxesOnly) {
-        Set<BoundingBox> boundingBoxes = clientBoundingBoxProvider.getBoundingBoxes(dimensionType, outerBoxesOnly);
-        if (boundingBoxes == null || boundingBoxes.size() == 0)
-            return;
+    public static void render(int dimensionId) {
+        if (!active) return;
+
+        Set<AbstractBoundingBox> boundingBoxes = getBoundingBoxes(dimensionId);
 
         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
         GL11.glLineWidth(2.0f);
         GL11.glDisable(GL11.GL_TEXTURE_2D);
         GL11.glDisable(GL11.GL_CULL_FACE);
 
-        if (ConfigManager.alwaysVisible.getBoolean()) {
+        if (ConfigManager.alwaysVisible.get()) {
             GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
         }
-        for (BoundingBox bb : boundingBoxes) {
-            Renderer renderer = boundingBoxRendererMap.get(bb.getClass());
-            if (renderer != null) {
-                renderer.render(bb);
-            }
+
+        for (AbstractBoundingBox key : boundingBoxes) {
+            AbstractRenderer renderer = boundingBoxRendererMap.get(key.getClass());
+            if (renderer == null) continue;
+
+            renderer.render(key);
         }
 
         GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
         GL11.glEnable(GL11.GL_CULL_FACE);
         GL11.glEnable(GL11.GL_TEXTURE_2D);
     }
+
+    private static Set<AbstractBoundingBox> getBoundingBoxes(int dimensionId) {
+        Set<AbstractBoundingBox> boundingBoxes = new HashSet<>();
+        for (IBoundingBoxProvider<?> provider : providers) {
+            if (provider.canProvide(dimensionId)) {
+                for (AbstractBoundingBox boundingBox : provider.get(dimensionId)) {
+                    if (isWithinRenderDistance(boundingBox)) {
+                        boundingBoxes.add(boundingBox);
+                    }
+                }
+            }
+        }
+        return boundingBoxes;
+    }
 }