package net.minecraft.world.level.block;

import java.util.List;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.AbstractFlowerFeature;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.configurations.DecoratedFeatureConfiguration;
import net.minecraft.server.level.ServerLevel;
import java.util.Random;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;

public class GrassBlock extends SpreadingSnowyDirtBlock implements BonemealableBlock {
    public GrassBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return bjg.getBlockState(fk.above()).isAir();
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        final BlockPos fk2 = fk.above();
        final BlockState byj2 = Blocks.GRASS.defaultBlockState();
        int integer8 = 0;
    Label_0282_Outer:
        while (integer8 < 128) {
            BlockPos fk3 = fk2;
            int integer9 = 0;
            while (true) {
                while (integer9 < integer8 / 16) {
                    fk3 = fk3.offset(random.nextInt(3) - 1, (random.nextInt(3) - 1) * random.nextInt(3) / 2, random.nextInt(3) - 1);
                    if (xd.getBlockState(fk3.below()).getBlock() == this) {
                        if (!xd.getBlockState(fk3).isCollisionShapeFullBlock(xd, fk3)) {
                            ++integer9;
                            continue Label_0282_Outer;
                        }
                    }
                    ++integer8;
                    continue Label_0282_Outer;
                }
                final BlockState byj3 = xd.getBlockState(fk3);
                if (byj3.getBlock() == byj2.getBlock() && random.nextInt(10) == 0) {
                    ((BonemealableBlock)byj2.getBlock()).performBonemeal(xd, random, fk3, byj3);
                }
                if (!byj3.isAir()) {
                    continue;
                }
                BlockState byj4;
                if (random.nextInt(8) == 0) {
                    final List<ConfiguredFeature<?, ?>> list12 = xd.getBiome(fk3).getFlowerFeatures();
                    if (list12.isEmpty()) {
                        continue;
                    }
                    final ConfiguredFeature<?, ?> cdc13 = ((DecoratedFeatureConfiguration)list12.get(0).config).feature;
                    byj4 = ((AbstractFlowerFeature)cdc13.feature).getRandomFlower(random, fk3, (FeatureConfiguration)cdc13.config);
                }
                else {
                    byj4 = byj2;
                }
                if (byj4.canSurvive(xd, fk3)) {
                    xd.setBlock(fk3, byj4, 3);
                }
                continue;
            }
        }
    }
}
