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