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.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.levelgen.ChunkGeneratorSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.levelgen.feature.configurations.BlockPileConfiguration;

public class BlockPileFeature extends Feature<BlockPileConfiguration> {
    public BlockPileFeature(final Function<Dynamic<?>, ? extends BlockPileConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final BlockPileConfiguration cgd) {
        if (fk.getY() < 5) {
            return false;
        }
        final int integer7 = 2 + random.nextInt(2);
        final int integer8 = 2 + random.nextInt(2);
        for (final BlockPos fk2 : BlockPos.betweenClosed(fk.offset(-integer7, 0, -integer8), fk.offset(integer7, 1, integer8))) {
            final int integer9 = fk.getX() - fk2.getX();
            final int integer10 = fk.getZ() - fk2.getZ();
            if (integer9 * integer9 + integer10 * integer10 <= random.nextFloat() * 10.0f - random.nextFloat() * 6.0f) {
                this.tryPlaceBlock(bjx, fk2, random, cgd);
            }
            else {
                if (random.nextFloat() >= 0.031) {
                    continue;
                }
                this.tryPlaceBlock(bjx, fk2, random, cgd);
            }
        }
        return true;
    }
    
    private boolean mayPlaceOn(final LevelAccessor bjx, final BlockPos fk, final Random random) {
        final BlockPos fk2 = fk.below();
        final BlockState byj6 = bjx.getBlockState(fk2);
        if (byj6.getBlock() == Blocks.GRASS_PATH) {
            return random.nextBoolean();
        }
        return byj6.isFaceSturdy(bjx, fk2, Direction.UP);
    }
    
    private void tryPlaceBlock(final LevelAccessor bjx, final BlockPos fk, final Random random, final BlockPileConfiguration cgd) {
        if (bjx.isEmptyBlock(fk) && this.mayPlaceOn(bjx, fk, random)) {
            bjx.setBlock(fk, cgd.stateProvider.getState(random, fk), 4);
        }
    }
}
