]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Add support for blocks only spawnable at night
authorIrtimaled <irtimaled@gmail.com>
Sun, 10 May 2020 22:27:16 +0000 (15:27 -0700)
committerIrtimaled <irtimaled@gmail.com>
Mon, 18 May 2020 00:31:27 +0000 (17:31 -0700)
src/main/java/com/irtimaled/bbor/client/commands/SpawningSphereCommand.java
src/main/java/com/irtimaled/bbor/client/interop/BlockProcessor.java
src/main/java/com/irtimaled/bbor/client/interop/SpawnableBlocksHelper.java
src/main/java/com/irtimaled/bbor/client/interop/SpawningSphereHelper.java
src/main/java/com/irtimaled/bbor/client/models/BoundingBoxSpawnableBlocks.java
src/main/java/com/irtimaled/bbor/client/models/BoundingBoxSpawningSphere.java
src/main/java/com/irtimaled/bbor/client/providers/SpawnableBlocksProvider.java
src/main/java/com/irtimaled/bbor/client/providers/SpawningSphereProvider.java
src/main/resources/assets/bbor/lang/en_us.json

index a97c360fb27ea79f0a3a283437a8fe41ee281b2a..c109863bb6d5f05b35292f0bf0bdbd919c2c705c 100644 (file)
@@ -1,12 +1,16 @@
 package com.irtimaled.bbor.client.commands;
 
+import com.irtimaled.bbor.client.Player;
 import com.irtimaled.bbor.client.providers.SpawningSphereProvider;
 import com.mojang.brigadier.CommandDispatcher;
 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
+import net.minecraft.client.Minecraft;
 import net.minecraft.command.Commands;
 import net.minecraft.command.ISuggestionProvider;
 import net.minecraft.command.arguments.Vec3Argument;
 import net.minecraft.util.math.Vec3d;
+import net.minecraft.world.EnumLightType;
+import net.minecraft.world.World;
 
 public class SpawningSphereCommand {
     private static final String COMMAND = "bbor:spawningSphere";
@@ -43,12 +47,31 @@ public class SpawningSphereCommand {
                         }))
                 .then(Commands.literal(CALCULATE_SPAWNABLE)
                         .executes(context -> {
-                            int count = SpawningSphereProvider.recalculateSpawnableSpacesCount();
+                            if(!SpawningSphereProvider.hasSpawningSphereInDimension(Player.getDimensionId())) {
+                                CommandHelper.feedback(context, "bbor.commands.spawningSphere.notSet");
+                                return 0;
+                            }
 
-                            String format = count == -1 ? "bbor.commands.spawningSphere.notSet" : "bbor.commands.spawningSphere.calculated";
-                            CommandHelper.feedback(context, format, String.format("%,d", count));
+                            Counts counts = new Counts();
+                            World world = Minecraft.getInstance().world;
+                            SpawningSphereProvider.calculateSpawnableSpacesCount(pos -> {
+                                counts.spawnable++;
+                                if(world.getLightFor(EnumLightType.SKY, pos) > 7)
+                                    counts.nightSpawnable++;
+                            });
+                            SpawningSphereProvider.setSpawnableSpacesCount(counts.spawnable);
+
+                            CommandHelper.feedback(context, "bbor.commands.spawningSphere.calculated",
+                                    String.format("%,d", counts.spawnable),
+                                    String.format("%,d", counts.nightSpawnable));
                             return 0;
                         }));
         commandDispatcher.register(command);
     }
+
+    private static class Counts {
+        private int spawnable = 0;
+        private int nightSpawnable = 0;
+
+    }
 }
index fbcdfdc66aa39f6d0d19128eb3e64a01af9857ae..1b66b039790c071949a3fb8c79af606f9362dc89 100644 (file)
@@ -1,6 +1,8 @@
 package com.irtimaled.bbor.client.interop;
 
+import net.minecraft.util.math.BlockPos;
+
 @FunctionalInterface
 public interface BlockProcessor {
-    boolean process(int x, int y, int z);
+    void process(BlockPos blockPos);
 }
index f781a6c7337fdd65f934ab82fec0457acc1d97df..e4d76379922565d45b8904ec1c3273e171835211 100644 (file)
@@ -38,7 +38,7 @@ public class SpawnableBlocksHelper {
                     BlockPos pos = new BlockPos(x, y, z);
                     upperBlockState = world.getBlockState(pos);
                     if (isSpawnable(world, pos, spawnBlockState, upperBlockState)) {
-                        blockProcessor.process(x, y, z);
+                        blockProcessor.process(pos);
                     }
                 }
             }
index e9a5e6e82fbdbdaceb5e6e868cd3c46f57be92a6..f20ef77c02b2e06e2b42474c674025d99c27f534 100644 (file)
@@ -9,7 +9,7 @@ import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.World;
 
 public class SpawningSphereHelper {
-    public static int findSpawnableSpaces(Point center, Coords coords, int width, int height, BlockProcessor blockProcessor) {
+    public static void findSpawnableSpaces(Point center, Coords coords, int width, int height, BlockProcessor blockProcessor) {
         int blockX = coords.getX();
         int minX = blockX - width;
         int maxX = blockX + width + 1;
@@ -23,7 +23,6 @@ public class SpawningSphereHelper {
         int maxY = Math.min(255, blockY + height);
 
         World world = Minecraft.getInstance().world;
-        int processed = 0;
         for (int x = minX; x < maxX; x++) {
             for (int z = minZ; z < maxZ; z++) {
                 double closestX = x + 0.5D;
@@ -40,14 +39,12 @@ public class SpawningSphereHelper {
                     upperBlockState = world.getBlockState(pos);
                     distance = center.getDistance(new Point(closestX, y, closestZ));
                     if (isWithinSpawnableZone(distance) &&
-                            SpawnableBlocksHelper.isSpawnable(world, pos, spawnBlockState, upperBlockState) &&
-                            blockProcessor.process(x, y, z)) {
-                        processed++;
+                            SpawnableBlocksHelper.isSpawnable(world, pos, spawnBlockState, upperBlockState)) {
+                        blockProcessor.process(pos);
                     }
                 }
             }
         }
-        return processed;
     }
 
     private static boolean isWithinSpawnableZone(double distance) {
index 8f3a4d518850625822524da53574257f1e5088a9..889f2afd7360db99983e89c5d09defc2bcdc2e97 100644 (file)
@@ -2,19 +2,19 @@ package com.irtimaled.bbor.client.models;
 
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
-import com.irtimaled.bbor.common.models.Coords;
+import net.minecraft.util.math.BlockPos;
 
 import java.util.HashSet;
 import java.util.Set;
 
 public class BoundingBoxSpawnableBlocks extends AbstractBoundingBox {
-    private final Set<Coords> blocks = new HashSet<>();
+    private final Set<BlockPos> blocks = new HashSet<>();
 
     public BoundingBoxSpawnableBlocks() {
         super(BoundingBoxType.SpawnableBlocks);
     }
 
-    public Set<Coords> getBlocks() {
+    public Set<BlockPos> getBlocks() {
         return blocks;
     }
 
index b5bbd593fb120ef5150d282191ede2d1167b9b73..fbf6b8b075ce34b13d1220a6ae202a072d2dff27 100644 (file)
@@ -2,8 +2,8 @@ package com.irtimaled.bbor.client.models;
 
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.models.BoundingBoxSphere;
-import com.irtimaled.bbor.common.models.Coords;
 import com.irtimaled.bbor.common.models.Point;
+import net.minecraft.util.math.BlockPos;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -12,14 +12,14 @@ public class BoundingBoxSpawningSphere extends BoundingBoxSphere {
     public static final int SAFE_RADIUS = 24;
     public static final int SPAWN_RADIUS = 128;
 
-    private final Set<Coords> blocks = new HashSet<>();
+    private final Set<BlockPos> blocks = new HashSet<>();
     private Integer spawnableCount;
 
     public BoundingBoxSpawningSphere(Point point) {
         super(point, SPAWN_RADIUS, BoundingBoxType.AFKSphere);
     }
 
-    public Set<Coords> getBlocks() {
+    public Set<BlockPos> getBlocks() {
         return blocks;
     }
 
index 4f3704e93046893c308da67c4ad44108c04eb9c3..243cf48d752f74cf88889d27e312adc4e2cd56d2 100644 (file)
@@ -7,7 +7,6 @@ import com.irtimaled.bbor.client.interop.SpawnableBlocksHelper;
 import com.irtimaled.bbor.client.models.BoundingBoxSpawnableBlocks;
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.MathHelper;
-import com.irtimaled.bbor.common.models.Coords;
 import net.minecraft.client.Minecraft;
 
 import java.util.HashSet;
@@ -47,13 +46,11 @@ public class SpawnableBlocksProvider implements IBoundingBoxProvider<BoundingBox
 
     private Set<BoundingBoxSpawnableBlocks> getSpawnableBlocks() {
         BoundingBoxSpawnableBlocks boundingBox = new BoundingBoxSpawnableBlocks();
-        Set<Coords> blocks = boundingBox.getBlocks();
 
         int width = MathHelper.floor(Math.pow(2, 1 + ConfigManager.spawnableBlocksRenderWidth.get()));
         int height = MathHelper.floor(Math.pow(2, ConfigManager.spawnableBlocksRenderHeight.get()));
 
-        SpawnableBlocksHelper.findSpawnableBlocks(Player.getCoords(), width, height,
-                (x, y, z) -> blocks.add(new Coords(x, y, z)));
+        SpawnableBlocksHelper.findSpawnableBlocks(Player.getCoords(), width, height, boundingBox.getBlocks()::add);
 
         Set<BoundingBoxSpawnableBlocks> boundingBoxes = new HashSet<>();
         boundingBoxes.add(boundingBox);
index 6b17475ea8762593a827bd8b48cac2d3cf09145f..a5a2a1ef9393cd9c4e15a2f18fe0c002456caa09 100644 (file)
@@ -3,11 +3,11 @@ package com.irtimaled.bbor.client.providers;
 import com.irtimaled.bbor.client.Player;
 import com.irtimaled.bbor.client.config.BoundingBoxTypeHelper;
 import com.irtimaled.bbor.client.config.ConfigManager;
+import com.irtimaled.bbor.client.interop.BlockProcessor;
 import com.irtimaled.bbor.client.interop.SpawningSphereHelper;
 import com.irtimaled.bbor.client.models.BoundingBoxSpawningSphere;
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.MathHelper;
-import com.irtimaled.bbor.common.models.Coords;
 import com.irtimaled.bbor.common.models.Point;
 import net.minecraft.client.Minecraft;
 
@@ -25,9 +25,7 @@ public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxS
     public static void setSphere(double x, double y, double z) {
         Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
 
-        if (spawningSphere != null && spawningSphere.getPoint().equals(point)) {
-            return;
-        }
+        if (spawningSphere != null && spawningSphere.getPoint().equals(point)) return;
         clear();
 
         dimensionId = Player.getDimensionId();
@@ -43,7 +41,7 @@ public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxS
     }
 
     public static boolean clear() {
-        if(spawningSphere != null) {
+        if (spawningSphere != null) {
             lastBoundingBox = null;
             spawningSphere = null;
             dimensionId = null;
@@ -52,32 +50,31 @@ public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxS
         return false;
     }
 
-    public static int recalculateSpawnableSpacesCount() {
+    public static void calculateSpawnableSpacesCount(BlockProcessor blockProcessor) {
         if (spawningSphere != null) {
             Point sphereCenter = spawningSphere.getPoint();
-            int spawnableSpacesCount = getSpawnableSpacesCount(sphereCenter);
-            spawningSphere.setSpawnableCount(spawnableSpacesCount);
-            return spawnableSpacesCount;
+            int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
+            SpawningSphereHelper.findSpawnableSpaces(sphereCenter, sphereCenter.getCoords(), size, size, blockProcessor);
         }
-        return -1;
-    }
-
-    private static int getSpawnableSpacesCount(Point center) {
-        int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
-        return SpawningSphereHelper.findSpawnableSpaces(center, center.getCoords(), size, size, (x, y, z) -> true);
     }
 
     static boolean playerInsideSphere() {
-        return spawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
+        return hasSpawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
     }
 
-    private static boolean spawningSphereInDimension(int dimensionId) {
+    public static boolean hasSpawningSphereInDimension(int dimensionId) {
         return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId;
     }
 
+    public static void setSpawnableSpacesCount(int count) {
+        if (spawningSphere != null) {
+            spawningSphere.setSpawnableCount(count);
+        }
+    }
+
     @Override
     public boolean canProvide(int dimensionId) {
-        return spawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
+        return hasSpawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
     }
 
     @Override
@@ -91,14 +88,12 @@ public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxS
     }
 
     private Set<BoundingBoxSpawningSphere> getSpawningSphere() {
-        Set<Coords> blocks = spawningSphere.getBlocks();
-        blocks.clear();
+        spawningSphere.getBlocks().clear();
         if (ConfigManager.renderAFKSpawnableBlocks.get()) {
             int width = MathHelper.floor(Math.pow(2, 1 + ConfigManager.spawnableBlocksRenderWidth.get()));
             int height = MathHelper.floor(Math.pow(2, ConfigManager.spawnableBlocksRenderHeight.get()));
 
-            SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height,
-                    (x, y, z) -> blocks.add(new Coords(x, y, z)));
+            SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height, spawningSphere.getBlocks()::add);
         }
         Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
         boundingBoxes.add(spawningSphere);
index d345041c580152cfe24861b2dc4487e765c98f60..c3ec6285aeb8fc3f09d194060983158d4f9aa458 100644 (file)
@@ -81,7 +81,7 @@
   "bbor.commands.spawningSphere.set": "Spawning sphere set",
   "bbor.commands.spawningSphere.notSet": "No spawning sphere set",
   "bbor.commands.spawningSphere.cleared": "Spawning sphere cleared",
-  "bbor.commands.spawningSphere.calculated": "Calculated %s spawnable spaces",
+  "bbor.commands.spawningSphere.calculated": "Calculated %s spawnable spaces (%s only at night)",
 
   "bbor.renderer.spawningSphere.spawnable": "Spawnable:",
   "bbor.renderer.spawningSphere.none": "None"