]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/FlowerForestProvider.java
dcb4d2b367465c853579f71d90d9477930455c3a
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / providers / FlowerForestProvider.java
1 /*
2 package com.irtimaled.bbor.client.providers;
3
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;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
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<>();
26
27     @Override
28     public boolean canProvide(DimensionId dimensionId) {
29         return BoundingBoxTypeHelper.shouldRender(BoundingBoxType.FlowerForest);
30     }
31
32     @Override
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();
40         }
41         return lastBoundingBoxes.values();
42     }
43
44     public void clearCache() {
45         lastBoundingBoxes = new HashMap<>();
46         lastPlayerCoords = null;
47     }
48
49     private Map<Coords, BoundingBoxFlowerForest> getBoundingBoxes() {
50         int renderDistance = lastRenderDistance;
51         Coords playerCoords = lastPlayerCoords;
52
53         int width = MathHelper.floor(Math.pow(2, 2 + renderDistance));
54
55         int blockX = playerCoords.getX();
56         int minX = blockX - width;
57         int maxX = blockX + width;
58
59         int blockZ = playerCoords.getZ();
60         int minZ = blockZ - width;
61         int maxZ = blockZ + width;
62
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);
69                     if (y == 0) {
70                         continue;
71                     }
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);
77                 }
78             }
79         }
80         return boundingBoxes;
81     }
82
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
86         while (topY > 0) {
87             if (FlowerForestHelper.canGrowFlower(x, topY, z)) return topY;
88             topY--;
89         }
90         return 0;
91     }
92 }
93
94  */