]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/SpawningSphereProvider.java
Add support for blocks only spawnable at night
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / providers / SpawningSphereProvider.java
1 package com.irtimaled.bbor.client.providers;
2
3 import com.irtimaled.bbor.client.Player;
4 import com.irtimaled.bbor.client.config.BoundingBoxTypeHelper;
5 import com.irtimaled.bbor.client.config.ConfigManager;
6 import com.irtimaled.bbor.client.interop.BlockProcessor;
7 import com.irtimaled.bbor.client.interop.SpawningSphereHelper;
8 import com.irtimaled.bbor.client.models.BoundingBoxSpawningSphere;
9 import com.irtimaled.bbor.common.BoundingBoxType;
10 import com.irtimaled.bbor.common.MathHelper;
11 import com.irtimaled.bbor.common.models.Point;
12 import net.minecraft.client.Minecraft;
13
14 import java.util.HashSet;
15 import java.util.Set;
16
17 public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxSpawningSphere> {
18     public static final Minecraft minecraft = Minecraft.getInstance();
19     private static Long lastGameTime = null;
20
21     private static Set<BoundingBoxSpawningSphere> lastBoundingBox = null;
22     private static BoundingBoxSpawningSphere spawningSphere;
23     private static Integer dimensionId;
24
25     public static void setSphere(double x, double y, double z) {
26         Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
27
28         if (spawningSphere != null && spawningSphere.getPoint().equals(point)) return;
29         clear();
30
31         dimensionId = Player.getDimensionId();
32         spawningSphere = new BoundingBoxSpawningSphere(point);
33         lastBoundingBox = null;
34     }
35
36     private static double snapToNearestHalf(double value) {
37         int floor = MathHelper.floor(value);
38         int fraction = MathHelper.floor((value - floor) * 4.0);
39         if (fraction % 2 == 1) fraction++;
40         return floor + (fraction / 4.0);
41     }
42
43     public static boolean clear() {
44         if (spawningSphere != null) {
45             lastBoundingBox = null;
46             spawningSphere = null;
47             dimensionId = null;
48             return true;
49         }
50         return false;
51     }
52
53     public static void calculateSpawnableSpacesCount(BlockProcessor blockProcessor) {
54         if (spawningSphere != null) {
55             Point sphereCenter = spawningSphere.getPoint();
56             int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
57             SpawningSphereHelper.findSpawnableSpaces(sphereCenter, sphereCenter.getCoords(), size, size, blockProcessor);
58         }
59     }
60
61     static boolean playerInsideSphere() {
62         return hasSpawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
63     }
64
65     public static boolean hasSpawningSphereInDimension(int dimensionId) {
66         return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId;
67     }
68
69     public static void setSpawnableSpacesCount(int count) {
70         if (spawningSphere != null) {
71             spawningSphere.setSpawnableCount(count);
72         }
73     }
74
75     @Override
76     public boolean canProvide(int dimensionId) {
77         return hasSpawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
78     }
79
80     @Override
81     public Iterable<BoundingBoxSpawningSphere> get(int dimensionId) {
82         long gameTime = minecraft.world.getGameTime();
83         if (lastBoundingBox == null || (!((Long) gameTime).equals(lastGameTime) && gameTime % 2L == 0L)) {
84             lastGameTime = gameTime;
85             lastBoundingBox = getSpawningSphere();
86         }
87         return lastBoundingBox;
88     }
89
90     private Set<BoundingBoxSpawningSphere> getSpawningSphere() {
91         spawningSphere.getBlocks().clear();
92         if (ConfigManager.renderAFKSpawnableBlocks.get()) {
93             int width = MathHelper.floor(Math.pow(2, 1 + ConfigManager.spawnableBlocksRenderWidth.get()));
94             int height = MathHelper.floor(Math.pow(2, ConfigManager.spawnableBlocksRenderHeight.get()));
95
96             SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height, spawningSphere.getBlocks()::add);
97         }
98         Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
99         boundingBoxes.add(spawningSphere);
100         return boundingBoxes;
101     }
102 }