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