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