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.BiomeBorderHelper;
7 import com.irtimaled.bbor.client.interop.FlowerForestHelper;
8 import com.irtimaled.bbor.client.models.BoundingBoxFlowerForest;
9 import com.irtimaled.bbor.common.BoundingBoxType;
10 import com.irtimaled.bbor.common.MathHelper;
11 import com.irtimaled.bbor.common.models.Coords;
12 import com.irtimaled.bbor.common.models.DimensionId;
13 import net.minecraft.client.MinecraftClient;
14 import net.minecraft.util.registry.BuiltinRegistries;
15 import net.minecraft.world.Heightmap;
17 import java.util.HashMap;
20 public class FlowerForestProvider implements IBoundingBoxProvider<BoundingBoxFlowerForest>, ICachingProvider {
21 public static final int FLOWER_FOREST_BIOME_ID = BuiltinRegistries.BIOME.getRawId(FlowerForestHelper.BIOME);
22 private static Coords lastPlayerCoords = null;
23 private static Integer lastRenderDistance = null;
24 private static Map<Coords, BoundingBoxFlowerForest> lastBoundingBoxes = new HashMap<>();
27 public boolean canProvide(DimensionId dimensionId) {
28 return BoundingBoxTypeHelper.shouldRender(BoundingBoxType.FlowerForest);
32 public Iterable<BoundingBoxFlowerForest> get(DimensionId dimensionId) {
33 Coords playerCoords = Player.getCoords();
34 Integer renderDistance = ConfigManager.flowerForestsRenderDistance.get();
35 if (!playerCoords.equals(lastPlayerCoords) || !renderDistance.equals(lastRenderDistance)) {
36 lastPlayerCoords = playerCoords;
37 lastRenderDistance = renderDistance;
38 lastBoundingBoxes = getBoundingBoxes();
40 return lastBoundingBoxes.values();
43 public void clearCache() {
44 lastBoundingBoxes = new HashMap<>();
45 lastPlayerCoords = null;
48 private Map<Coords, BoundingBoxFlowerForest> getBoundingBoxes() {
49 int renderDistance = lastRenderDistance;
50 Coords playerCoords = lastPlayerCoords;
52 int width = MathHelper.floor(Math.pow(2, 2 + renderDistance));
54 int blockX = playerCoords.getX();
55 int minX = blockX - width;
56 int maxX = blockX + width;
58 int blockZ = playerCoords.getZ();
59 int minZ = blockZ - width;
60 int maxZ = blockZ + width;
62 Map<Coords, BoundingBoxFlowerForest> boundingBoxes = new HashMap<>((maxX - minX) * (maxZ - minZ));
63 for (int x = minX; x <= maxX; x++) {
64 for (int z = minZ; z <= maxZ; z++) {
65 int biomeId = BiomeBorderHelper.getBiomeId(x, 255, z);
66 if (biomeId == FLOWER_FOREST_BIOME_ID) {
67 int y = getMaxYForPos(x, playerCoords.getY() + 1, z);
71 Coords coords = new Coords(x, y + 1, z);
72 BoundingBoxFlowerForest boundingBox = lastBoundingBoxes.containsKey(coords)
73 ? lastBoundingBoxes.get(coords)
74 : new BoundingBoxFlowerForest(coords, FlowerForestHelper.getFlowerColorAtPos(coords));
75 boundingBoxes.put(coords, boundingBox);
82 private static int getMaxYForPos(int x, int y, int z) {
83 int topY = MinecraftClient.getInstance().world.getTopY(Heightmap.Type.MOTION_BLOCKING, x, z);
84 if (topY == 0) topY = y; // heightmap appears to be broken
86 if (FlowerForestHelper.canGrowFlower(x, topY, z)) return topY;