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

import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelWriter;
import net.minecraft.util.Mth;
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.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class IceSpikeFeature extends Feature<NoneFeatureConfiguration> {
    public IceSpikeFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, BlockPos fu, final NoneFeatureConfiguration coa) {
        while (bqu.isEmptyBlock(fu) && fu.getY() > 2) {
            fu = fu.below();
        }
        if (!bqu.getBlockState(fu).is(Blocks.SNOW_BLOCK)) {
            return false;
        }
        fu = fu.above(random.nextInt(4));
        final int integer8 = random.nextInt(4) + 7;
        final int integer9 = integer8 / 4 + random.nextInt(2);
        if (integer9 > 1 && random.nextInt(60) == 0) {
            fu = fu.above(10 + random.nextInt(30));
        }
        for (int integer10 = 0; integer10 < integer8; ++integer10) {
            final float float11 = (1.0f - integer10 / (float)integer8) * integer9;
            for (int integer11 = Mth.ceil(float11), integer12 = -integer11; integer12 <= integer11; ++integer12) {
                final float float12 = Mth.abs(integer12) - 0.25f;
                for (int integer13 = -integer11; integer13 <= integer11; ++integer13) {
                    final float float13 = Mth.abs(integer13) - 0.25f;
                    if ((integer12 == 0 && integer13 == 0) || float12 * float12 + float13 * float13 <= float11 * float11) {
                        if ((integer12 != -integer11 && integer12 != integer11 && integer13 != -integer11 && integer13 != integer11) || random.nextFloat() <= 0.75f) {
                            BlockState cfj17 = bqu.getBlockState(fu.offset(integer12, integer10, integer13));
                            Block bvr18 = cfj17.getBlock();
                            if (cfj17.isAir() || Feature.isDirt(bvr18) || bvr18 == Blocks.SNOW_BLOCK || bvr18 == Blocks.ICE) {
                                this.setBlock(bqu, fu.offset(integer12, integer10, integer13), Blocks.PACKED_ICE.defaultBlockState());
                            }
                            if (integer10 != 0 && integer11 > 1) {
                                cfj17 = bqu.getBlockState(fu.offset(integer12, -integer10, integer13));
                                bvr18 = cfj17.getBlock();
                                if (cfj17.isAir() || Feature.isDirt(bvr18) || bvr18 == Blocks.SNOW_BLOCK || bvr18 == Blocks.ICE) {
                                    this.setBlock(bqu, fu.offset(integer12, -integer10, integer13), Blocks.PACKED_ICE.defaultBlockState());
                                }
                            }
                        }
                    }
                }
            }
        }
        int integer10 = integer9 - 1;
        if (integer10 < 0) {
            integer10 = 0;
        }
        else if (integer10 > 1) {
            integer10 = 1;
        }
        for (int integer14 = -integer10; integer14 <= integer10; ++integer14) {
            for (int integer11 = -integer10; integer11 <= integer10; ++integer11) {
                BlockPos fu2 = fu.offset(integer14, -1, integer11);
                int integer15 = 50;
                if (Math.abs(integer14) == 1 && Math.abs(integer11) == 1) {
                    integer15 = random.nextInt(5);
                }
                while (fu2.getY() > 50) {
                    final BlockState cfj18 = bqu.getBlockState(fu2);
                    final Block bvr19 = cfj18.getBlock();
                    if (!cfj18.isAir() && !Feature.isDirt(bvr19) && bvr19 != Blocks.SNOW_BLOCK && bvr19 != Blocks.ICE && bvr19 != Blocks.PACKED_ICE) {
                        break;
                    }
                    this.setBlock(bqu, fu2, Blocks.PACKED_ICE.defaultBlockState());
                    fu2 = fu2.below();
                    if (--integer15 > 0) {
                        continue;
                    }
                    fu2 = fu2.below(random.nextInt(5) + 1);
                    integer15 = random.nextInt(5);
                }
            }
        }
        return true;
    }
}
