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.levelgen.ChunkGeneratorSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
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.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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return MushroomBlock.SHAPE;
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, BlockPos fk, final Random random) {
        if (random.nextInt(25) == 0) {
            int integer6 = 5;
            final int integer7 = 4;
            for (final BlockPos fk2 : BlockPos.betweenClosed(fk.offset(-4, -1, -4), fk.offset(4, 1, 4))) {
                if (xd.getBlockState(fk2).getBlock() == this && --integer6 <= 0) {
                    return;
                }
            }
            BlockPos fk3 = fk.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
            for (int integer8 = 0; integer8 < 4; ++integer8) {
                if (xd.isEmptyBlock(fk3) && byj.canSurvive(xd, fk3)) {
                    fk = fk3;
                }
                fk3 = fk.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
            }
            if (xd.isEmptyBlock(fk3) && byj.canSurvive(xd, fk3)) {
                xd.setBlock(fk3, byj, 2);
            }
        }
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return byj.isSolidRender(bjg, fk);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        final BlockState byj2 = bjz.getBlockState(fk2);
        final Block bph7 = byj2.getBlock();
        return bph7 == Blocks.MYCELIUM || bph7 == Blocks.PODZOL || (bjz.getRawBrightness(fk, 0) < 13 && this.mayPlaceOn(byj2, bjz, fk2));
    }
    
    public boolean growMushroom(final ServerLevel xd, final BlockPos fk, final BlockState byj, final Random random) {
        xd.removeBlock(fk, false);
        ConfiguredFeature<HugeMushroomFeatureConfiguration, ?> cdc6;
        if (this == Blocks.BROWN_MUSHROOM) {
            cdc6 = Feature.HUGE_BROWN_MUSHROOM.configured(BiomeDefaultFeatures.HUGE_BROWN_MUSHROOM_CONFIG);
        }
        else {
            if (this != Blocks.RED_MUSHROOM) {
                xd.setBlock(fk, byj, 3);
                return false;
            }
            cdc6 = Feature.HUGE_RED_MUSHROOM.configured(BiomeDefaultFeatures.HUGE_RED_MUSHROOM_CONFIG);
        }
        if (cdc6.place(xd, xd.getChunkSource().getGenerator(), random, fk)) {
            return true;
        }
        xd.setBlock(fk, byj, 3);
        return false;
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return true;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return random.nextFloat() < 0.4;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        this.growMushroom(xd, fk, byj, random);
    }
    
    @Override
    public boolean hasPostProcess(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return true;
    }
    
    static {
        SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 6.0, 11.0);
    }
}
