package net.minecraft.world.level.block;

import net.minecraft.world.level.Level;
import net.minecraft.world.level.levelgen.feature.configurations.HugeMushroomFeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.biome.BiomeDefaultFeatures;
import net.minecraft.world.level.levelgen.feature.Feature;
import java.util.Iterator;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;

public class MushroomBlock extends BushBlock implements BonemealableBlock {
    protected static final VoxelShape SHAPE;
    
    public MushroomBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return MushroomBlock.SHAPE;
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, BlockPos fu, final Random random) {
        if (random.nextInt(25) == 0) {
            int integer6 = 5;
            final int integer7 = 4;
            for (final BlockPos fu2 : BlockPos.betweenClosed(fu.offset(-4, -1, -4), fu.offset(4, 1, 4))) {
                if (zd.getBlockState(fu2).is(this) && --integer6 <= 0) {
                    return;
                }
            }
            BlockPos fu3 = fu.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
            for (int integer8 = 0; integer8 < 4; ++integer8) {
                if (zd.isEmptyBlock(fu3) && cfj.canSurvive(zd, fu3)) {
                    fu = fu3;
                }
                fu3 = fu.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
            }
            if (zd.isEmptyBlock(fu3) && cfj.canSurvive(zd, fu3)) {
                zd.setBlock(fu3, cfj, 2);
            }
        }
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return cfj.isSolidRender(bpg, fu);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockPos fu2 = fu.below();
        final BlockState cfj2 = bqd.getBlockState(fu2);
        return cfj2.is(Blocks.MYCELIUM) || cfj2.is(Blocks.PODZOL) || (bqd.getRawBrightness(fu, 0) < 13 && this.mayPlaceOn(cfj2, bqd, fu2));
    }
    
    public boolean growMushroom(final ServerLevel zd, final BlockPos fu, final BlockState cfj, final Random random) {
        zd.removeBlock(fu, false);
        ConfiguredFeature<HugeMushroomFeatureConfiguration, ?> ckb6;
        if (this == Blocks.BROWN_MUSHROOM) {
            ckb6 = Feature.HUGE_BROWN_MUSHROOM.configured(BiomeDefaultFeatures.HUGE_BROWN_MUSHROOM_CONFIG);
        }
        else {
            if (this != Blocks.RED_MUSHROOM) {
                zd.setBlock(fu, cfj, 3);
                return false;
            }
            ckb6 = Feature.HUGE_RED_MUSHROOM.configured(BiomeDefaultFeatures.HUGE_RED_MUSHROOM_CONFIG);
        }
        if (ckb6.place(zd, zd.structureFeatureManager(), zd.getChunkSource().getGenerator(), random, fu)) {
            return true;
        }
        zd.setBlock(fu, cfj, 3);
        return false;
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        return true;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bqb, final Random random, final BlockPos fu, final BlockState cfj) {
        return random.nextFloat() < 0.4;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        this.growMushroom(zd, fu, cfj, random);
    }
    
    static {
        SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 6.0, 11.0);
    }
}
