]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/SpawningSphereProvider.java
6b17475ea8762593a827bd8b48cac2d3cf09145f
[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.SpawningSphereHelper;
7 import com.irtimaled.bbor.client.models.BoundingBoxSpawningSphere;
8 import com.irtimaled.bbor.common.BoundingBoxType;
9 import com.irtimaled.bbor.common.MathHelper;
10 import com.irtimaled.bbor.common.models.Coords;
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(double x, double y, double z) {
26         Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
27
28         if (spawningSphere != null && spawningSphere.getPoint().equals(point)) {
29             return;
30         }
31         clear();
32
33         dimensionId = Player.getDimensionId();
34         spawningSphere = new BoundingBoxSpawningSphere(point);
35         lastBoundingBox = null;
36     }
37
38     private static double snapToNearestHalf(double value) {
39         int floor = MathHelper.floor(value);
40         int fraction = MathHelper.floor((value - floor) * 4.0);
41         if (fraction % 2 == 1) fraction++;
42         return floor + (fraction / 4.0);
43     }
44
45     public static boolean clear() {
46         if(spawningSphere != null) {
47             lastBoundingBox = null;
48             spawningSphere = null;
49             dimensionId = null;
50             return true;
51         }
52         return false;
53     }
54
55     public static int recalculateSpawnableSpacesCount() {
56         if (spawningSphere != null) {
57             Point sphereCenter = spawningSphere.getPoint();
58             int spawnableSpacesCount = getSpawnableSpacesCount(sphereCenter);
59             spawningSphere.setSpawnableCount(spawnableSpacesCount);
60             return spawnableSpacesCount;
61         }
62         return -1;
63     }
64
65     private static int getSpawnableSpacesCount(Point center) {
66         int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
67         return SpawningSphereHelper.findSpawnableSpaces(center, center.getCoords(), size, size, (x, y, z) -> true);
68     }
69
70     static boolean playerInsideSphere() {
71         return spawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
72     }
73
74     private static boolean spawningSphereInDimension(int dimensionId) {
75         return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId;
76     }
77
78     @Override
79     public boolean canProvide(int dimensionId) {
80         return spawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
81     }
82
83     @Override
84     public Iterable<BoundingBoxSpawningSphere> get(int dimensionId) {
85         long gameTime = minecraft.world.getGameTime();
86         if (lastBoundingBox == null || (!((Long) gameTime).equals(lastGameTime) && gameTime % 2L == 0L)) {
87             lastGameTime = gameTime;
88             lastBoundingBox = getSpawningSphere();
89         }
90         return lastBoundingBox;
91     }
92
93     private Set<BoundingBoxSpawningSphere> getSpawningSphere() {
94         Set<Coords> blocks = spawningSphere.getBlocks();
95         blocks.clear();
96         if (ConfigManager.renderAFKSpawnableBlocks.get()) {
97             int width = MathHelper.floor(Math.pow(2, 1 + ConfigManager.spawnableBlocksRenderWidth.get()));
98             int height = MathHelper.floor(Math.pow(2, ConfigManager.spawnableBlocksRenderHeight.get()));
99
100             SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height,
101                     (x, y, z) -> blocks.add(new Coords(x, y, z)));
102         }
103         Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
104         boundingBoxes.add(spawningSphere);
105         return boundingBoxes;
106     }
107 }