9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0263-optimise-getBiome.patch
2025-06-25 23:05:25 +09:00

148 lines
7.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: hayanesuru <hayanesuru@outlook.jp>
Date: Tue, 3 Jun 2025 15:20:40 +0900
Subject: [PATCH] optimise getBiome
diff --git a/net/minecraft/advancements/critereon/LocationPredicate.java b/net/minecraft/advancements/critereon/LocationPredicate.java
index a26a5311f87873e0d4d26fda9cb8956a32ee81e8..65e4315cce35814c60b21bbd5baea2ffac82162c 100644
--- a/net/minecraft/advancements/critereon/LocationPredicate.java
+++ b/net/minecraft/advancements/critereon/LocationPredicate.java
@@ -49,7 +49,7 @@ public record LocationPredicate(
} else {
BlockPos blockPos = BlockPos.containing(x, y, z);
boolean isLoaded = level.isLoaded(blockPos);
- return (!this.biomes.isPresent() || isLoaded && this.biomes.get().contains(level.getBiome(blockPos)))
+ return (!this.biomes.isPresent() || isLoaded && this.biomes.get().contains(org.dreeam.leaf.config.modules.opt.OptimizeBiome.advancement ? level.getBiomeCached(blockPos) : level.getBiome(blockPos))) // Leaf - cache getBiome
&& (!this.structures.isPresent() || isLoaded && level.structureManager().getStructureWithPieceAt(blockPos, this.structures.get()).isValid())
&& (!this.smokey.isPresent() || isLoaded && this.smokey.get() == CampfireBlock.isSmokeyPos(level, blockPos))
&& (!this.light.isPresent() || this.light.get().matches(level, blockPos))
diff --git a/net/minecraft/world/level/LevelReader.java b/net/minecraft/world/level/LevelReader.java
index 0842fd6488c8b27d98c4344e1244996b4c0e9912..c6ddb49648c55443ae880c1adba9887ab0681c82 100644
--- a/net/minecraft/world/level/LevelReader.java
+++ b/net/minecraft/world/level/LevelReader.java
@@ -57,6 +57,12 @@ public interface LevelReader extends ca.spottedleaf.moonrise.patches.chunk_syste
return this.getBiomeManager().getBiome(pos);
}
+ // Leaf start - cache getBiome
+ default Holder<Biome> getBiomeCached(BlockPos pos) {
+ return this.getBiomeManager().getBiomeCached(pos);
+ }
+ // Leaf end - cache getBiome
+
default Stream<BlockState> getBlockStatesIfLoaded(AABB aabb) {
int floor = Mth.floor(aabb.minX);
int floor1 = Mth.floor(aabb.maxX);
diff --git a/net/minecraft/world/level/NaturalSpawner.java b/net/minecraft/world/level/NaturalSpawner.java
index a2da4fce50f31d56036d04041c4f80ed90c18b27..f242941ce06d356a025e306efe78c688e9b755c4 100644
--- a/net/minecraft/world/level/NaturalSpawner.java
+++ b/net/minecraft/world/level/NaturalSpawner.java
@@ -443,7 +443,7 @@ public final class NaturalSpawner {
private static Optional<MobSpawnSettings.SpawnerData> getRandomSpawnMobAt(
ServerLevel level, StructureManager structureManager, ChunkGenerator generator, MobCategory category, RandomSource random, BlockPos pos
) {
- Holder<Biome> biome = level.getBiome(pos);
+ Holder<Biome> biome = org.dreeam.leaf.config.modules.opt.OptimizeBiome.mobSpawn ? level.getBiomeCached(pos) : level.getBiome(pos); // Leaf - cache getBiome
return category == MobCategory.WATER_AMBIENT && biome.is(BiomeTags.REDUCED_WATER_AMBIENT_SPAWNS) && random.nextFloat() < 0.98F
? Optional.empty()
: mobsAt(level, structureManager, generator, category, pos, biome).getRandom(random);
@@ -460,7 +460,7 @@ public final class NaturalSpawner {
) {
return isInNetherFortressBounds(pos, level, cetagory, structureManager)
? NetherFortressStructure.FORTRESS_ENEMIES
- : generator.getMobsAt(biome != null ? biome : level.getBiome(pos), structureManager, cetagory, pos);
+ : generator.getMobsAt(biome != null ? biome : (org.dreeam.leaf.config.modules.opt.OptimizeBiome.mobSpawn ? level.getBiomeCached(pos) : level.getBiome(pos)), structureManager, cetagory, pos); // Leaf - cache getBiome
}
public static boolean isInNetherFortressBounds(BlockPos pos, ServerLevel level, MobCategory category, StructureManager structureManager) {
diff --git a/net/minecraft/world/level/biome/BiomeManager.java b/net/minecraft/world/level/biome/BiomeManager.java
index a48175a7ebb1788ace46395621ed78d910178a53..00122472991ba0c1a0ea77053aad71cdfa92a7bd 100644
--- a/net/minecraft/world/level/biome/BiomeManager.java
+++ b/net/minecraft/world/level/biome/BiomeManager.java
@@ -15,10 +15,23 @@ public class BiomeManager {
private final BiomeManager.NoiseBiomeSource noiseBiomeSource;
private final long biomeZoomSeed;
private static final double maxOffset = 0.4500000001D; // Leaf - Carpet-Fixes - Optimized getBiome method
+ // Leaf start - cache getBiome
+ private final Holder<Biome>[] biomeCache;
+ private final long[] biomeCachePos;
+ // Leaf end - cache getBiome
public BiomeManager(BiomeManager.NoiseBiomeSource noiseBiomeSource, long biomeZoomSeed) {
this.noiseBiomeSource = noiseBiomeSource;
this.biomeZoomSeed = biomeZoomSeed;
+ // Leaf start - cache getBiome
+ if (org.dreeam.leaf.config.modules.opt.OptimizeBiome.enabled) {
+ biomeCache = new Holder[65536];
+ biomeCachePos = new long[65536];
+ } else {
+ biomeCache = null;
+ biomeCachePos = null;
+ }
+ // Leaf end - cache getBiome
}
public static long obfuscateSeed(long seed) {
@@ -29,6 +42,40 @@ public class BiomeManager {
return new BiomeManager(newSource, this.biomeZoomSeed);
}
+ // Leaf start - cache getBiome
+ public Holder<Biome> getBiomeCached(BlockPos pos) {
+ if (biomeCache == null) {
+ return getBiome(pos);
+ }
+ int xMinus2 = pos.getX() - 2;
+ int yMinus2 = pos.getY() - 2;
+ int zMinus2 = pos.getZ() - 2;
+ int x = xMinus2 >> 2;
+ int y = yMinus2 >> 2;
+ int z = zMinus2 >> 2;
+ long packedPos = BlockPos.asLong(x, y, z);
+ long hash = packedPos;
+ hash = (hash ^ (hash >>> 32)) * 0xff51afd7ed558ccdL;
+ hash = (hash ^ (hash >>> 32)) * 0xc4ceb9fe1a85ec53L;
+ hash = (hash ^ (hash >>> 32)) & 65535L;
+
+ long pos1 = biomeCachePos[(int) hash];
+ if (pos1 == packedPos) {
+ Holder<Biome> biome = biomeCache[(int) hash];
+ if (biome != null) {
+ return biome;
+ }
+ }
+
+ Holder<Biome> biome = getBiome(pos);
+
+ biomeCache[(int) hash] = biome;
+ biomeCachePos[(int) hash] = packedPos;
+
+ return biome;
+ }
+ // Leaf end - cache getBiome
+
public Holder<Biome> getBiome(BlockPos pos) {
// Leaf start - Carpet-Fixes - Optimized getBiome method
int xMinus2 = pos.getX() - 2;
@@ -126,9 +173,18 @@ public class BiomeManager {
return Mth.square(zNoise + fiddle2) + Mth.square(yNoise + fiddle1) + Mth.square(xNoise + fiddle);
}
+ // Leaf start - optimise getBiome
+ private static final double[] FIDDLE_TABLE = new double[1024];
+ static {
+ for (int i = 0; i < 1024; i++) {
+ FIDDLE_TABLE[i] = (i - 512) * (0.9 / 1024.0);
+ }
+ }
private static double getFiddle(long seed) {
- return (double)(((seed >> 24) & (1024 - 1)) - (1024/2)) * (0.9 / 1024.0); // Paper - avoid floorMod, fp division, and fp subtraction
+ return FIDDLE_TABLE[(int)(seed >>> 24) & 1023];
+ //return (double)(((seed >> 24) & (1024 - 1)) - (1024/2)) * (0.9 / 1024.0); // Paper - avoid floorMod, fp division, and fp subtraction
}
+ // Leaf end - optimise getBiome
public interface NoiseBiomeSource {
Holder<Biome> getNoiseBiome(int x, int y, int z);