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.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.block.TallSeagrass;
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.SeagrassFeatureConfiguration;

public class SeagrassFeature extends Feature<SeagrassFeatureConfiguration> {
    public SeagrassFeature(final Function<Dynamic<?>, ? extends SeagrassFeatureConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final SeagrassFeatureConfiguration che) {
        int integer7 = 0;
        for (int integer8 = 0; integer8 < che.count; ++integer8) {
            final int integer9 = random.nextInt(8) - random.nextInt(8);
            final int integer10 = random.nextInt(8) - random.nextInt(8);
            final int integer11 = bjx.getHeight(Heightmap.Types.OCEAN_FLOOR, fk.getX() + integer9, fk.getZ() + integer10);
            final BlockPos fk2 = new BlockPos(fk.getX() + integer9, integer11, fk.getZ() + integer10);
            if (bjx.getBlockState(fk2).getBlock() == Blocks.WATER) {
                final boolean boolean13 = random.nextDouble() < che.tallSeagrassProbability;
                final BlockState byj14 = boolean13 ? Blocks.TALL_SEAGRASS.defaultBlockState() : Blocks.SEAGRASS.defaultBlockState();
                if (byj14.canSurvive(bjx, fk2)) {
                    if (boolean13) {
                        final BlockState byj15 = ((AbstractStateHolder<O, BlockState>)byj14).<DoubleBlockHalf, DoubleBlockHalf>setValue(TallSeagrass.HALF, DoubleBlockHalf.UPPER);
                        final BlockPos fk3 = fk2.above();
                        if (bjx.getBlockState(fk3).getBlock() == Blocks.WATER) {
                            bjx.setBlock(fk2, byj14, 2);
                            bjx.setBlock(fk3, byj15, 2);
                        }
                    }
                    else {
                        bjx.setBlock(fk2, byj14, 2);
                    }
                    ++integer7;
                }
            }
        }
        return integer7 > 0;
    }
}
