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