]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/FlowerForestProvider.java
Setup for 1.16.3 Fabric
[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.util.registry.BuiltinRegistries;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 public class FlowerForestProvider implements IBoundingBoxProvider<BoundingBoxFlowerForest>, ICachingProvider {
19     public static final int FLOWER_FOREST_BIOME_ID = BuiltinRegistries.BIOME.getRawId(FlowerForestHelper.BIOME);
20     private static Coords lastPlayerCoords = null;
21     private static Integer lastRenderDistance = null;
22     private static Map<Coords, BoundingBoxFlowerForest> lastBoundingBoxes = new HashMap<>();
23
24     @Override
25     public boolean canProvide(DimensionId dimensionId) {
26         return BoundingBoxTypeHelper.shouldRender(BoundingBoxType.FlowerForest);
27     }
28
29     @Override
30     public Iterable<BoundingBoxFlowerForest> get(DimensionId dimensionId) {
31         Coords playerCoords = Player.getCoords();
32         Integer renderDistance = ConfigManager.flowerForestsRenderDistance.get();
33         if (!playerCoords.equals(lastPlayerCoords) || !renderDistance.equals(lastRenderDistance)) {
34             lastPlayerCoords = playerCoords;
35             lastRenderDistance = renderDistance;
36             lastBoundingBoxes = getBoundingBoxes();
37         }
38         return lastBoundingBoxes.values();
39     }
40
41     public void clearCache() {
42         lastBoundingBoxes = new HashMap<>();
43         lastPlayerCoords = null;
44     }
45
46     private Map<Coords, BoundingBoxFlowerForest> getBoundingBoxes() {
47         int renderDistance = lastRenderDistance;
48         Coords playerCoords = lastPlayerCoords;
49
50         int width = MathHelper.floor(Math.pow(2, 2 + renderDistance));
51
52         int blockX = playerCoords.getX();
53         int minX = blockX - width;
54         int maxX = blockX + width;
55
56         int blockZ = playerCoords.getZ();
57         int minZ = blockZ - width;
58         int maxZ = blockZ + width;
59
60         Map<Coords, BoundingBoxFlowerForest> boundingBoxes = new HashMap<>();
61         for (int x = minX; x <= maxX; x++) {
62             for (int z = minZ; z <= maxZ; z++) {
63                 int biomeId = BiomeBorderHelper.getBiomeId(x, 255, z);
64                 if (biomeId == FLOWER_FOREST_BIOME_ID) {
65                     int y = getMaxYForPos(x, playerCoords.getY() + 1, z);
66                     if (y == 0) {
67                         continue;
68                     }
69                     Coords coords = new Coords(x, y + 1, z);
70                     BoundingBoxFlowerForest boundingBox = lastBoundingBoxes.containsKey(coords)
71                             ? lastBoundingBoxes.get(coords)
72                             : new BoundingBoxFlowerForest(coords, FlowerForestHelper.getFlowerColorAtPos(coords));
73                     boundingBoxes.put(coords, boundingBox);
74                 }
75             }
76         }
77         return boundingBoxes;
78     }
79
80     private static int getMaxYForPos(int x, int y, int z) {
81         while (y > 0) {
82             if (FlowerForestHelper.canGrowFlower(x, y, z)) return y;
83             y--;
84         }
85         return 0;
86     }
87 }