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

import java.util.List;
import net.minecraft.world.level.levelgen.structure.StructurePiece;
import java.util.Random;
import net.minecraft.world.level.levelgen.structure.StrongholdPieces;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.StructureStart;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.chunk.ChunkGenerator;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class StrongholdFeature extends StructureFeature<NoneFeatureConfiguration> {
    public StrongholdFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public StructureStartFactory<NoneFeatureConfiguration> getStartFactory() {
        return StrongholdStart::new;
    }
    
    @Override
    protected boolean isFeatureChunk(final ChunkGenerator cha, final BiomeSource brh, final long long3, final WorldgenRandom ciy, final int integer5, final int integer6, final Biome bre, final ChunkPos bph, final NoneFeatureConfiguration coa) {
        return cha.hasStronghold(new ChunkPos(integer5, integer6));
    }
    
    public static class StrongholdStart extends StructureStart<NoneFeatureConfiguration> {
        private final long seed;
        
        public StrongholdStart(final StructureFeature<NoneFeatureConfiguration> cml, final int integer2, final int integer3, final BoundingBox ctd, final int integer5, final long long6) {
            super(cml, integer2, integer3, ctd, integer5, long6);
            this.seed = long6;
        }
        
        @Override
        public void generatePieces(final ChunkGenerator cha, final StructureManager cva, final int integer3, final int integer4, final Biome bre, final NoneFeatureConfiguration coa) {
            int integer5 = 0;
            StrongholdPieces.StartPiece m9;
            do {
                this.pieces.clear();
                this.boundingBox = BoundingBox.getUnknownBox();
                this.random.setLargeFeatureSeed(this.seed + integer5++, integer3, integer4);
                StrongholdPieces.resetPieces();
                m9 = new StrongholdPieces.StartPiece(this.random, (integer3 << 4) + 2, (integer4 << 4) + 2);
                this.pieces.add(m9);
                m9.addChildren(m9, this.pieces, this.random);
                final List<StructurePiece> list10 = m9.pendingChildren;
                while (!list10.isEmpty()) {
                    final int integer6 = this.random.nextInt(list10.size());
                    final StructurePiece cty12 = list10.remove(integer6);
                    cty12.addChildren(m9, this.pieces, this.random);
                }
                this.calculateBoundingBox();
                this.moveBelowSeaLevel(cha.getSeaLevel(), this.random, 10);
            } while (this.pieces.isEmpty() || m9.portalRoomPiece == null);
        }
    }
}
