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

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.KelpBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.Heightmap;
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.NoneFeatureConfiguration;

public class KelpFeature extends Feature<NoneFeatureConfiguration> {
    public KelpFeature(final Function<Dynamic<?>, ? extends NoneFeatureConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final NoneFeatureConfiguration cgv) {
        int integer7 = 0;
        final int integer8 = bjx.getHeight(Heightmap.Types.OCEAN_FLOOR, fk.getX(), fk.getZ());
        BlockPos fk2 = new BlockPos(fk.getX(), integer8, fk.getZ());
        if (bjx.getBlockState(fk2).getBlock() == Blocks.WATER) {
            final BlockState byj10 = Blocks.KELP.defaultBlockState();
            final BlockState byj11 = Blocks.KELP_PLANT.defaultBlockState();
            for (int integer9 = 1 + random.nextInt(10), integer10 = 0; integer10 <= integer9; ++integer10) {
                if (bjx.getBlockState(fk2).getBlock() == Blocks.WATER && bjx.getBlockState(fk2.above()).getBlock() == Blocks.WATER && byj11.canSurvive(bjx, fk2)) {
                    if (integer10 == integer9) {
                        bjx.setBlock(fk2, ((AbstractStateHolder<O, BlockState>)byj10).<Comparable, Integer>setValue((Property<Comparable>)KelpBlock.AGE, random.nextInt(4) + 20), 2);
                        ++integer7;
                    }
                    else {
                        bjx.setBlock(fk2, byj11, 2);
                    }
                }
                else if (integer10 > 0) {
                    final BlockPos fk3 = fk2.below();
                    if (byj10.canSurvive(bjx, fk3) && bjx.getBlockState(fk3.below()).getBlock() != Blocks.KELP) {
                        bjx.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj10).<Comparable, Integer>setValue((Property<Comparable>)KelpBlock.AGE, random.nextInt(4) + 20), 2);
                        ++integer7;
                        break;
                    }
                    break;
                }
                fk2 = fk2.above();
            }
        }
        return integer7 > 0;
    }
}
