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