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.BlockProcessor;
7 import com.irtimaled.bbor.client.interop.SpawningSphereHelper;
8 import com.irtimaled.bbor.client.models.BoundingBoxSpawningSphere;
9 import com.irtimaled.bbor.common.BoundingBoxType;
10 import com.irtimaled.bbor.common.MathHelper;
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)) return;
31 dimensionId = Player.getDimensionId();
32 spawningSphere = new BoundingBoxSpawningSphere(point);
33 lastBoundingBox = null;
36 private static double snapToNearestHalf(double value) {
37 int floor = MathHelper.floor(value);
38 int fraction = MathHelper.floor((value - floor) * 4.0);
39 if (fraction % 2 == 1) fraction++;
40 return floor + (fraction / 4.0);
43 public static boolean clear() {
44 if (spawningSphere != null) {
45 lastBoundingBox = null;
46 spawningSphere = null;
53 public static void calculateSpawnableSpacesCount(BlockProcessor blockProcessor) {
54 if (spawningSphere != null) {
55 Point sphereCenter = spawningSphere.getPoint();
56 int size = BoundingBoxSpawningSphere.SPAWN_RADIUS + 2;
57 SpawningSphereHelper.findSpawnableSpaces(sphereCenter, sphereCenter.getCoords(), size, size, blockProcessor);
61 static boolean playerInsideSphere() {
62 return hasSpawningSphereInDimension(Player.getDimensionId()) && spawningSphere.isWithinSphere(Player.getPoint());
65 public static boolean hasSpawningSphereInDimension(int dimensionId) {
66 return spawningSphere != null && SpawningSphereProvider.dimensionId == dimensionId;
69 public static void setSpawnableSpacesCount(int count) {
70 if (spawningSphere != null) {
71 spawningSphere.setSpawnableCount(count);
76 public boolean canProvide(int dimensionId) {
77 return hasSpawningSphereInDimension(dimensionId) && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.AFKSphere);
81 public Iterable<BoundingBoxSpawningSphere> get(int dimensionId) {
82 long gameTime = minecraft.world.getGameTime();
83 if (lastBoundingBox == null || (!((Long) gameTime).equals(lastGameTime) && gameTime % 2L == 0L)) {
84 lastGameTime = gameTime;
85 lastBoundingBox = getSpawningSphere();
87 return lastBoundingBox;
90 private Set<BoundingBoxSpawningSphere> getSpawningSphere() {
91 spawningSphere.getBlocks().clear();
92 if (ConfigManager.renderAFKSpawnableBlocks.get()) {
93 int width = MathHelper.floor(Math.pow(2, 1 + ConfigManager.spawnableBlocksRenderWidth.get()));
94 int height = MathHelper.floor(Math.pow(2, ConfigManager.spawnableBlocksRenderHeight.get()));
96 SpawningSphereHelper.findSpawnableSpaces(spawningSphere.getPoint(), Player.getCoords(), width, height, spawningSphere.getBlocks()::add);
98 Set<BoundingBoxSpawningSphere> boundingBoxes = new HashSet<>();
99 boundingBoxes.add(spawningSphere);
100 return boundingBoxes;