1 package com.irtimaled.bbor.client.providers;
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;
14 import java.util.HashSet;
17 public class SpawningSphereProvider implements IBoundingBoxProvider<BoundingBoxSpawningSphere> {
18 public static final Minecraft minecraft = Minecraft.getInstance();
19 private static Long lastGameTime = null;
21 private static Set<BoundingBoxSpawningSphere> lastBoundingBox = null;
22 private static BoundingBoxSpawningSphere spawningSphere;
23 private static Integer dimensionId;
25 public static void setSphere(double x, double y, double z) {
26 Point point = new Point(snapToNearestHalf(x), y, snapToNearestHalf(z));
28 if (spawningSphere != null && spawningSphere.getPoint().equals(point)) {
33 dimensionId = Player.getDimensionId();
34 spawningSphere = new BoundingBoxSpawningSphere(point);
35 lastBoundingBox = null;
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);
45 public static boolean clear() {
46 if(spawningSphere != null) {
47 lastBoundingBox = null;
48 spawningSphere = null;
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;
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);
70 static boolean playerInsideSphere() {
71 return spawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
74 private static boolean spawningSphereInDimension(int dimensionId) {
75 return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId;
79 public boolean canProvide(int dimensionId) {
80 return spawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
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();
90 return lastBoundingBox;
93 private Set<BoundingBoxSpawningSphere> getSpawningSphere() {
94 Set<Coords> blocks = spawningSphere.getBlocks();
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()));
100 SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height,
101 (x, y, z) -> blocks.add(new Coords(x, y, z)));
103 Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
104 boundingBoxes.add(spawningSphere);
105 return boundingBoxes;