diff --git a/src/main/java/org/betterx/bclib/api/v2/levelgen/structures/TemplateStructure.java b/src/main/java/org/betterx/bclib/api/v2/levelgen/structures/TemplateStructure.java index c235a0c3..6e390efd 100644 --- a/src/main/java/org/betterx/bclib/api/v2/levelgen/structures/TemplateStructure.java +++ b/src/main/java/org/betterx/bclib/api/v2/levelgen/structures/TemplateStructure.java @@ -3,11 +3,14 @@ package org.betterx.bclib.api.v2.levelgen.structures; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.core.BlockPos; +import net.minecraft.core.Holder; +import net.minecraft.core.QuartPos; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.ExtraCodecs; import net.minecraft.util.RandomSource; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.NoiseColumn; +import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Mirror; import net.minecraft.world.level.block.Rotation; @@ -92,17 +95,21 @@ public abstract class TemplateStructure extends Structure { @Override public Optional findGenerationPoint(GenerationContext ctx) { + final Config config = randomConfig(ctx.random()); + if (config == null) return Optional.empty(); + + ChunkPos chunkPos = ctx.chunkPos(); + final int x = chunkPos.getMinBlockX(); + final int z = chunkPos.getMinBlockZ(); + if (!hasValidBiomeAtRandomHeight(ctx, x, z)) + return Optional.empty(); + WorldGenerationContext worldGenerationContext = new WorldGenerationContext( ctx.chunkGenerator(), ctx.heightAccessor() ); - final Config config = randomConfig(ctx.random()); - if (config == null) return Optional.empty(); - ChunkPos chunkPos = ctx.chunkPos(); - final int x = chunkPos.getMinBlockX(); - final int z = chunkPos.getMinBlockZ(); - StructureTemplate structureTemplate = ctx.structureTemplateManager().getOrCreate(config.location); + StructureTemplate structureTemplate = ctx.structureTemplateManager().getOrCreate(config.location); final BiPredicate isCorrectBase; final int searchStep; @@ -215,6 +222,27 @@ public abstract class TemplateStructure extends Structure { } + private boolean hasValidBiomeAtRandomHeight(GenerationContext ctx, int x, int z) { + final int randomY = ctx.random() + .nextIntBetweenInclusive( + ctx.heightAccessor().getMinBuildHeight(), + ctx.heightAccessor().getMaxBuildHeight() + ); + + Holder holder = ctx.chunkGenerator() + .getBiomeSource() + .getNoiseBiome( + QuartPos.fromBlock(x), + QuartPos.fromBlock(randomY), + QuartPos.fromBlock(z), + ctx.randomState().sampler() + ); + if (!ctx.validBiome().test(holder)) { + return false; + } + return true; + } + private float airRatio(NoiseColumn column, int y, int height, int searchStep) { int airCount = 0; for (int i = y; i < y + height && i > y - height; i += searchStep) {