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.interop.BedrockCeilingHelper;
6 import com.irtimaled.bbor.client.interop.ClientInterop;
7 import com.irtimaled.bbor.client.models.BoundingBoxBedrockCeiling;
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.DimensionId;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
17 import java.util.Random;
20 public class BedrockCeilingProvider implements IBoundingBoxProvider<BoundingBoxBedrockCeiling>, ICachingProvider {
21 private static final double CHUNK_SIZE = 16d;
22 private static Long lastGameTime = null;
23 private static final Map<String, BedrockChunk> chunks = new HashMap<>();
25 private static class BedrockChunk {
26 private final Set<BoundingBoxBedrockCeiling> boxes = new HashSet<>();
28 public BedrockChunk(int chunkX, int chunkZ) {
29 int chunkStartX = chunkX << 4;
30 int chunkStartZ = chunkZ << 4;
32 if (BedrockCeilingHelper.chunkLoaded(chunkX, chunkZ)) findBoxesFromBlockState(chunkStartX, chunkStartZ);
33 else findBoxesFromRNG(chunkX, chunkZ, chunkStartX, chunkStartZ);
36 private void findBoxesFromBlockState(int chunkStartX, int chunkStartZ) {
37 for (int x = 0; x < 16; x++) {
38 for (int z = 0; z < 16; z++) {
39 Coords coords = getCoordsFromBlockState(chunkStartX + x, chunkStartZ + z);
41 boxes.add(new BoundingBoxBedrockCeiling(coords));
47 private Coords getCoordsFromBlockState(int x, int z) {
49 for (int y = 127; y >= 123; y--) {
50 if (BedrockCeilingHelper.isBedrock(x, y, z)) {
52 coords = new Coords(x, y, z);
61 private void findBoxesFromRNG(int chunkX, int chunkZ, int chunkStartX, int chunkStartZ) {
62 Random random = BedrockCeilingHelper.getRandomForChunk(chunkX, chunkZ);
64 // preseed 16x16x3 calls to nextDouble
65 for (int dummy = 0; dummy < 768; dummy++) {
68 for (int z = 0; z < 16; z++) {
69 for (int x = 0; x < 16; x++) {
70 Coords coords = getBlocksFromRNG(random, chunkStartX + x, chunkStartZ + z);
73 boxes.add(new BoundingBoxBedrockCeiling(coords));
79 private Coords getBlocksFromRNG(Random random, int x, int z) {
81 for (int y = 127; y >= 123; y--) {
82 if (y >= 127 - random.nextInt(5)) {
86 for (int y = 4; y >= 0; y--) {
89 return count == 1 ? new Coords(x, 127, z) : null;
92 public Collection<? extends BoundingBoxBedrockCeiling> getBlocks() {
101 public void clearCache() {
102 chunks.values().forEach(BedrockChunk::clear);
107 public Iterable<BoundingBoxBedrockCeiling> get(DimensionId dimensionId) {
108 boolean shouldRecalculate = shouldRecalculate();
110 int renderDistanceChunks = ClientInterop.getRenderDistanceChunks() / 2;
111 int playerChunkX = MathHelper.floor(Player.getX() / CHUNK_SIZE);
112 int playerChunkZ = MathHelper.floor(Player.getZ() / CHUNK_SIZE);
114 Set<BoundingBoxBedrockCeiling> boxes = new HashSet<>();
116 for (int chunkX = playerChunkX - renderDistanceChunks; chunkX <= playerChunkX + renderDistanceChunks; chunkX++) {
117 for (int chunkZ = playerChunkZ - renderDistanceChunks; chunkZ <= playerChunkZ + renderDistanceChunks; chunkZ++) {
118 String key = String.format("%d,%d", chunkX, chunkZ);
119 if (shouldRecalculate || !chunks.containsKey(key)) {
120 chunks.put(key, new BedrockChunk(chunkX, chunkZ));
122 BedrockChunk chunk = chunks.get(key);
123 boxes.addAll(chunk.getBlocks());
129 public boolean shouldRecalculate() {
130 long gameTime = ClientInterop.getGameTime();
131 if (!((Long) gameTime).equals(lastGameTime) && gameTime % 2L == 0L) {
132 lastGameTime = gameTime;
139 public boolean canProvide(DimensionId dimensionId) {
140 return dimensionId == DimensionId.NETHER && BoundingBoxTypeHelper.shouldRender(BoundingBoxType.BedrockCeiling) && Player.getY() > 110;