]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/interop/BiomeBorderHelper.java
Performance improvements
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / BiomeBorderHelper.java
index 7cde22bbdc79702abb253550d9d21fa003cfd79f..5dd29e0a88d4dbecab29183de828a62c595fda94 100644 (file)
@@ -1,47 +1,47 @@
 package com.irtimaled.bbor.client.interop;
 
 import com.irtimaled.bbor.common.models.Coords;
+import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.world.ClientWorld;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.ChunkPos;
 import net.minecraft.util.registry.Registry;
-import net.minecraft.world.biome.Biome;
-import net.minecraft.world.biome.source.BiomeArray;
-import net.minecraft.world.biome.source.BiomeCoords;
-import net.minecraft.world.chunk.WorldChunk;
 
 public class BiomeBorderHelper {
 
-    private static final Long2ObjectOpenHashMap<BiomeArray> biomeCache = new Long2ObjectOpenHashMap<>();
+    private static final Long2ObjectOpenHashMap<Long2IntOpenHashMap> biomeCache = new Long2ObjectOpenHashMap<>();
 
     public static void onChunkLoaded(int chunkX, int chunkZ) {
-        assert MinecraftClient.getInstance().world != null;
-        final WorldChunk chunk = MinecraftClient.getInstance().world.getChunk(chunkX, chunkZ);
-        if (chunk == null) return;
-        biomeCache.put(ChunkPos.toLong(chunkX, chunkZ), chunk.getBiomeArray());
+
     }
 
     public static void onChunkUnload(int chunkX, int chunkZ) {
         biomeCache.remove(ChunkPos.toLong(chunkX, chunkZ));
     }
 
+    public static void onDisconnect() {
+        biomeCache.clear();
+    }
+
     public static int getBiomeId(Coords coords) {
         return getBiomeId(coords.getX(), coords.getY(), coords.getZ());
     }
 
     public static int getBiomeId(int x, int y, int z) {
         BlockPos pos = new BlockPos(x, y, z);
-        final BiomeArray biomeArray = biomeCache.get(ChunkPos.toLong(pos));
+        final Long2IntOpenHashMap biomeArray = biomeCache.computeIfAbsent(ChunkPos.toLong(pos), key -> createNewMap());
         final ClientWorld world = MinecraftClient.getInstance().world;
-        final Biome biome;
-        if (biomeArray != null) {
-            biome = biomeArray.getBiomeForNoiseGen(BiomeCoords.fromBlock(x & 15), y, BiomeCoords.fromBlock(z & 15));
-        } else {
+        return biomeArray.computeIfAbsent(pos.asLong(), key -> {
             assert world != null;
-            biome = world.getBiome(pos);
-        }
-        return world.getRegistryManager().get(Registry.BIOME_KEY).getRawId(biome);
+            return world.getRegistryManager().get(Registry.BIOME_KEY).getRawId(world.getBiome(pos));
+        });
+    }
+
+    private static Long2IntOpenHashMap createNewMap() {
+        final Long2IntOpenHashMap long2IntOpenHashMap = new Long2IntOpenHashMap();
+        long2IntOpenHashMap.defaultReturnValue(-1);
+        return long2IntOpenHashMap;
     }
 }