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

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.GrowingPlantHeadBlock;
import net.minecraft.util.Mth;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Blocks;
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.core.Direction;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class WeepingVinesFeature extends Feature<NoneFeatureConfiguration> {
    private static final Direction[] DIRECTIONS;
    
    public WeepingVinesFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BlockPos fu, final NoneFeatureConfiguration coa) {
        if (!bqu.isEmptyBlock(fu)) {
            return false;
        }
        final BlockState cfj8 = bqu.getBlockState(fu.above());
        if (!cfj8.is(Blocks.NETHERRACK) && !cfj8.is(Blocks.NETHER_WART_BLOCK)) {
            return false;
        }
        this.placeRoofNetherWart(bqu, random, fu);
        this.placeRoofWeepingVines(bqu, random, fu);
        return true;
    }
    
    private void placeRoofNetherWart(final LevelAccessor bqc, final Random random, final BlockPos fu) {
        bqc.setBlock(fu, Blocks.NETHER_WART_BLOCK.defaultBlockState(), 2);
        final BlockPos.MutableBlockPos a5 = new BlockPos.MutableBlockPos();
        final BlockPos.MutableBlockPos a6 = new BlockPos.MutableBlockPos();
        for (int integer7 = 0; integer7 < 200; ++integer7) {
            a5.setWithOffset(fu, random.nextInt(6) - random.nextInt(6), random.nextInt(2) - random.nextInt(5), random.nextInt(6) - random.nextInt(6));
            if (bqc.isEmptyBlock(a5)) {
                int integer8 = 0;
                for (final Direction fz12 : WeepingVinesFeature.DIRECTIONS) {
                    final BlockState cfj13 = bqc.getBlockState(a6.setWithOffset(a5, fz12));
                    if (cfj13.is(Blocks.NETHERRACK) || cfj13.is(Blocks.NETHER_WART_BLOCK)) {
                        ++integer8;
                    }
                    if (integer8 > 1) {
                        break;
                    }
                }
                if (integer8 == 1) {
                    bqc.setBlock(a5, Blocks.NETHER_WART_BLOCK.defaultBlockState(), 2);
                }
            }
        }
    }
    
    private void placeRoofWeepingVines(final LevelAccessor bqc, final Random random, final BlockPos fu) {
        final BlockPos.MutableBlockPos a5 = new BlockPos.MutableBlockPos();
        for (int integer6 = 0; integer6 < 100; ++integer6) {
            a5.setWithOffset(fu, random.nextInt(8) - random.nextInt(8), random.nextInt(2) - random.nextInt(7), random.nextInt(8) - random.nextInt(8));
            if (bqc.isEmptyBlock(a5)) {
                final BlockState cfj7 = bqc.getBlockState(a5.above());
                if (cfj7.is(Blocks.NETHERRACK) || cfj7.is(Blocks.NETHER_WART_BLOCK)) {
                    int integer7 = Mth.nextInt(random, 1, 8);
                    if (random.nextInt(6) == 0) {
                        integer7 *= 2;
                    }
                    if (random.nextInt(5) == 0) {
                        integer7 = 1;
                    }
                    final int integer8 = 17;
                    final int integer9 = 25;
                    placeWeepingVinesColumn(bqc, random, a5, integer7, 17, 25);
                }
            }
        }
    }
    
    public static void placeWeepingVinesColumn(final LevelAccessor bqc, final Random random, final BlockPos.MutableBlockPos a, final int integer4, final int integer5, final int integer6) {
        for (int integer7 = 0; integer7 <= integer4; ++integer7) {
            if (bqc.isEmptyBlock(a)) {
                if (integer7 == integer4 || !bqc.isEmptyBlock(a.below())) {
                    bqc.setBlock(a, ((StateHolder<O, BlockState>)Blocks.WEEPING_VINES.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)GrowingPlantHeadBlock.AGE, Mth.nextInt(random, integer5, integer6)), 2);
                    break;
                }
                bqc.setBlock(a, Blocks.WEEPING_VINES_PLANT.defaultBlockState(), 2);
            }
            a.move(Direction.DOWN);
        }
    }
    
    static {
        DIRECTIONS = Direction.values();
    }
}
