Skip to content

Instantly share code, notes, and snippets.

@CorgiTaco
Created February 18, 2021 07:48
Show Gist options
  • Save CorgiTaco/f91c1a7af5cf76fde19764a778acf482 to your computer and use it in GitHub Desktop.
Save CorgiTaco/f91c1a7af5cf76fde19764a778acf482 to your computer and use it in GitHub Desktop.
package net.minecraft.world.level.chunk;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.core.IdMap;
import net.minecraft.core.QuartPos;
import net.minecraft.util.Mth;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.LevelHeightAccessor;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeManager;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.dimension.DimensionType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
public class ChunkBiomeContainer implements BiomeManager.NoiseBiomeSource {
private static final Logger LOGGER = LogManager.getLogger();
private static final int WIDTH_BITS = Mth.ceillog2(16) - 2;
private static final int HORIZONTAL_MASK;
public static final int MAX_SIZE;
private final IdMap<Biome> biomeRegistry;
private final Biome[] biomes;
private final int quartMinY;
private final int quartHeight;
protected ChunkBiomeContainer(IdMap<Biome> idMap, LevelHeightAccessor levelHeightAccessor, Biome[] biomes) {
this.biomeRegistry = idMap;
this.biomes = biomes;
this.quartMinY = QuartPos.fromBlock(levelHeightAccessor.getMinBuildHeight());
this.quartHeight = QuartPos.fromBlock(levelHeightAccessor.getHeight()) - 1;
}
@Environment(EnvType.CLIENT)
public ChunkBiomeContainer(IdMap<Biome> idMap, LevelHeightAccessor levelHeightAccessor, int[] is) {
this(idMap, levelHeightAccessor, new Biome[is.length]);
for(int i = 0; i < this.biomes.length; ++i) {
int j = is[i];
Biome biome = (Biome)idMap.byId(j);
if (biome == null) {
LOGGER.warn("Received invalid biome id: {}", j);
this.biomes[i] = (Biome)idMap.byId(0);
} else {
this.biomes[i] = biome;
}
}
}
public ChunkBiomeContainer(IdMap<Biome> idMap, LevelHeightAccessor levelHeightAccessor, ChunkPos chunkPos, BiomeSource biomeSource) {
this(idMap, levelHeightAccessor, chunkPos, biomeSource, (int[])null);
}
public ChunkBiomeContainer(IdMap<Biome> idMap, LevelHeightAccessor levelHeightAccessor, ChunkPos chunkPos, BiomeSource biomeSource, @Nullable int[] is) {
this(idMap, levelHeightAccessor, new Biome[(1 << WIDTH_BITS + WIDTH_BITS) * ceilDiv(levelHeightAccessor.getHeight(), 4)]);
int i = QuartPos.fromBlock(chunkPos.getMinBlockX());
int j = this.quartMinY;
int k = QuartPos.fromBlock(chunkPos.getMinBlockZ());
int l;
if (is != null) {
for(l = 0; l < is.length; ++l) {
this.biomes[l] = (Biome)idMap.byId(is[l]);
if (this.biomes[l] == null) {
this.biomes[l] = biomeForIndex(biomeSource, i, j, k, l);
}
}
} else {
for(l = 0; l < this.biomes.length; ++l) {
this.biomes[l] = biomeForIndex(biomeSource, i, j, k, l);
}
}
}
private static int ceilDiv(int i, int j) {
return (i + j - 1) / j;
}
private static Biome biomeForIndex(BiomeSource biomeSource, int i, int j, int k, int l) {
int m = l & HORIZONTAL_MASK;
int n = l >> WIDTH_BITS + WIDTH_BITS;
int o = l >> WIDTH_BITS & HORIZONTAL_MASK;
return biomeSource.getNoiseBiome(i + m, j + n, k + o);
}
public int[] writeBiomes() {
int[] is = new int[this.biomes.length];
for(int i = 0; i < this.biomes.length; ++i) {
is[i] = this.biomeRegistry.getId(this.biomes[i]);
}
return is;
}
public Biome getNoiseBiome(int biomeX, int biomeY, int biomeZ) {
int i = biomeX & HORIZONTAL_MASK;
int j = Mth.clamp(biomeY - this.quartMinY, 0, this.quartHeight);
int k = biomeZ & HORIZONTAL_MASK;
return this.biomes[j << WIDTH_BITS + WIDTH_BITS | k << WIDTH_BITS | i];
}
static {
HORIZONTAL_MASK = (1 << WIDTH_BITS) - 1;
MAX_SIZE = 1 << WIDTH_BITS + WIDTH_BITS + DimensionType.BITS_FOR_Y - 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment