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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
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.OreConfiguration;

public class NoSurfaceOreFeature extends Feature<OreConfiguration> {
    NoSurfaceOreFeature(final Codec<OreConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final OreConfiguration coc) {
        final int integer8 = random.nextInt(coc.size + 1);
        final BlockPos.MutableBlockPos a9 = new BlockPos.MutableBlockPos();
        for (int integer9 = 0; integer9 < integer8; ++integer9) {
            this.offsetTargetPos(a9, random, fu, Math.min(integer9, 7));
            if (coc.target.getPredicate().test(bqu.getBlockState(a9)) && !this.isFacingAir(bqu, a9)) {
                bqu.setBlock(a9, coc.state, 2);
            }
        }
        return true;
    }
    
    private void offsetTargetPos(final BlockPos.MutableBlockPos a, final Random random, final BlockPos fu, final int integer) {
        final int integer2 = this.getRandomPlacementInOneAxisRelativeToOrigin(random, integer);
        final int integer3 = this.getRandomPlacementInOneAxisRelativeToOrigin(random, integer);
        final int integer4 = this.getRandomPlacementInOneAxisRelativeToOrigin(random, integer);
        a.setWithOffset(fu, integer2, integer3, integer4);
    }
    
    private int getRandomPlacementInOneAxisRelativeToOrigin(final Random random, final int integer) {
        return Math.round((random.nextFloat() - random.nextFloat()) * integer);
    }
    
    private boolean isFacingAir(final LevelAccessor bqc, final BlockPos fu) {
        final BlockPos.MutableBlockPos a4 = new BlockPos.MutableBlockPos();
        for (final Direction fz8 : Direction.values()) {
            a4.setWithOffset(fu, fz8);
            if (bqc.getBlockState(a4).isAir()) {
                return true;
            }
        }
        return false;
    }
}
