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