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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Blocks;
import java.util.Iterator;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.levelgen.feature.configurations.BlockPileConfiguration;

public class BlockPileFeature extends Feature<BlockPileConfiguration> {
    public BlockPileFeature(final Codec<BlockPileConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final BlockPileConfiguration cnf) {
        if (fu.getY() < 5) {
            return false;
        }
        final int integer8 = 2 + random.nextInt(2);
        final int integer9 = 2 + random.nextInt(2);
        for (final BlockPos fu2 : BlockPos.betweenClosed(fu.offset(-integer8, 0, -integer9), fu.offset(integer8, 1, integer9))) {
            final int integer10 = fu.getX() - fu2.getX();
            final int integer11 = fu.getZ() - fu2.getZ();
            if (integer10 * integer10 + integer11 * integer11 <= random.nextFloat() * 10.0f - random.nextFloat() * 6.0f) {
                this.tryPlaceBlock(bqu, fu2, random, cnf);
            }
            else {
                if (random.nextFloat() >= 0.031) {
                    continue;
                }
                this.tryPlaceBlock(bqu, fu2, random, cnf);
            }
        }
        return true;
    }
    
    private boolean mayPlaceOn(final LevelAccessor bqc, final BlockPos fu, final Random random) {
        final BlockPos fu2 = fu.below();
        final BlockState cfj6 = bqc.getBlockState(fu2);
        if (cfj6.is(Blocks.GRASS_PATH)) {
            return random.nextBoolean();
        }
        return cfj6.isFaceSturdy(bqc, fu2, Direction.UP);
    }
    
    private void tryPlaceBlock(final LevelAccessor bqc, final BlockPos fu, final Random random, final BlockPileConfiguration cnf) {
        if (bqc.isEmptyBlock(fu) && this.mayPlaceOn(bqc, fu, random)) {
            bqc.setBlock(fu, cnf.stateProvider.getState(random, fu), 4);
        }
    }
}
