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