]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/SpawningSphereProvider.java
1efd41ebf023b0a09077f2aa5729c507b93d6a0c
[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.interop.SpawningSphereHelper;
6 import com.irtimaled.bbor.client.models.BoundingBoxSpawningSphere;
7 import com.irtimaled.bbor.common.BoundingBoxType;
8 import com.irtimaled.bbor.common.MathHelper;
9 import com.irtimaled.bbor.common.models.Point;
10
11 import java.util.HashSet;
12 import java.util.Set;
13
14 public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxSpawningSphere> {
15     private static BoundingBoxSpawningSphere spawningSphere;
16     private static Integer dimensionId;
17
18     public static void setSphere(double x, double y, double z) {
19         Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
20
21         if (spawningSphere != null && spawningSphere.getPoint().equals(point)) {
22             return;
23         }
24         clear();
25
26         dimensionId = Player.getDimensionId();
27         spawningSphere = new BoundingBoxSpawningSphere(point);
28     }
29
30     private static double snapToNearestHalf(double value) {
31         int floor = MathHelper.floor(value);
32         int fraction = MathHelper.floor((value - floor) * 4.0);
33         if (fraction % 2 == 1) fraction++;
34         return floor + (fraction / 4.0);
35     }
36
37     public static boolean clear() {
38         if(spawningSphere != null) {
39             spawningSphere = null;
40             dimensionId = null;
41             return true;
42         }
43         return false;
44     }
45
46     public static int recalculateSpawnableSpacesCount() {
47         if (spawningSphere != null) {
48             Point sphereCenter = spawningSphere.getPoint();
49             int spawnableSpacesCount = getSpawnableSpacesCount(sphereCenter);
50             spawningSphere.setSpawnableCount(spawnableSpacesCount);
51             return spawnableSpacesCount;
52         }
53         return -1;
54     }
55
56     private static int getSpawnableSpacesCount(Point center) {
57         int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
58         return SpawningSphereHelper.findSpawnableSpaces(center, center.getCoords(), size, size, (x, y, z) -> true);
59     }
60
61     @Override
62     public boolean canProvide(int dimensionId) {
63         return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
64     }
65
66     @Override
67     public Iterable<BoundingBoxSpawningSphere> get(int dimensionId) {
68         Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
69         boundingBoxes.add(spawningSphere);
70         return boundingBoxes;
71     }
72 }