package net.minecraft.world.level.levelgen.feature;

import net.minecraft.world.level.levelgen.ChunkGeneratorSettings;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.BeardedStructureStart;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeManager;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.ChunkPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.levelgen.feature.configurations.VillageConfiguration;

public class VillageFeature extends StructureFeature<VillageConfiguration> {
    public VillageFeature(final Function<Dynamic<?>, ? extends VillageConfiguration> function) {
        super(function);
    }
    
    @Override
    protected ChunkPos getPotentialFeatureChunkFromLocationWithOffset(final ChunkGenerator<?> caa, final Random random, final int integer3, final int integer4, final int integer5, final int integer6) {
        final int integer7 = ((ChunkGeneratorSettings)caa.getSettings()).getVillagesSpacing();
        final int integer8 = ((ChunkGeneratorSettings)caa.getSettings()).getVillagesSeparation();
        final int integer9 = integer3 + integer7 * integer5;
        final int integer10 = integer4 + integer7 * integer6;
        final int integer11 = (integer9 < 0) ? (integer9 - integer7 + 1) : integer9;
        final int integer12 = (integer10 < 0) ? (integer10 - integer7 + 1) : integer10;
        int integer13 = integer11 / integer7;
        int integer14 = integer12 / integer7;
        ((WorldgenRandom)random).setLargeFeatureWithSalt(caa.getSeed(), integer13, integer14, 10387312);
        integer13 *= integer7;
        integer14 *= integer7;
        integer13 += random.nextInt(integer7 - integer8);
        integer14 += random.nextInt(integer7 - integer8);
        return new ChunkPos(integer13, integer14);
    }
    
    @Override
    public boolean isFeatureChunk(final BiomeManager bkv, final ChunkGenerator<?> caa, final Random random, final int integer4, final int integer5, final Biome bkt) {
        final ChunkPos bjh8 = this.getPotentialFeatureChunkFromLocationWithOffset(caa, random, integer4, integer5, 0, 0);
        return integer4 == bjh8.x && integer5 == bjh8.z && caa.isBiomeValidStartForStructure(bkt, this);
    }
    
    @Override
    public StructureStartFactory getStartFactory() {
        return FeatureStart::new;
    }
    
    @Override
    public String getFeatureName() {
        return "Village";
    }
    
    @Override
    public int getLookupRange() {
        return 8;
    }
    
    public static class FeatureStart extends BeardedStructureStart {
        public FeatureStart(final StructureFeature<?> cfl, final int integer2, final int integer3, final BoundingBox clb, final int integer5, final long long6) {
            super(cfl, integer2, integer3, clb, integer5, long6);
        }
        
        @Override
        public void generatePieces(final ChunkGenerator<?> caa, final StructureManager cmo, final int integer3, final int integer4, final Biome bkt) {
            final VillageConfiguration chm7 = caa.<VillageConfiguration>getStructureConfiguration(bkt, Feature.VILLAGE);
            final BlockPos fk8 = new BlockPos(integer3 * 16, 0, integer4 * 16);
            VillagePieces.addPieces(caa, cmo, fk8, this.pieces, this.random, chm7);
            this.calculateBoundingBox();
        }
    }
}
