package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public abstract class GrowingPlantHeadBlock extends GrowingPlantBlock implements BonemealableBlock {
    public static final IntegerProperty AGE;
    private final double growPerTickProbability;
    
    protected GrowingPlantHeadBlock(final Properties c, final Direction fz, final VoxelShape dfg, final boolean boolean4, final double double5) {
        super(c, fz, dfg, boolean4);
        this.growPerTickProbability = double5;
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)GrowingPlantHeadBlock.AGE, 0));
    }
    
    public BlockState getStateForPlacement(final LevelAccessor bqc) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)GrowingPlantHeadBlock.AGE, bqc.getRandom().nextInt(25));
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.canSurvive(zd, fu)) {
            zd.destroyBlock(fu, true);
        }
    }
    
    @Override
    public boolean isRandomlyTicking(final BlockState cfj) {
        return cfj.<Integer>getValue((Property<Integer>)GrowingPlantHeadBlock.AGE) < 25;
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (cfj.<Integer>getValue((Property<Integer>)GrowingPlantHeadBlock.AGE) < 25 && random.nextDouble() < this.growPerTickProbability) {
            final BlockPos fu2 = fu.relative(this.growthDirection);
            if (this.canGrowInto(zd.getBlockState(fu2))) {
                zd.setBlockAndUpdate(fu2, ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)GrowingPlantHeadBlock.AGE));
            }
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz == this.growthDirection.getOpposite() && !cfj1.canSurvive(bqc, fu5)) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        if (fz == this.growthDirection && cfj3.is(this)) {
            return this.getBodyBlock().defaultBlockState();
        }
        if (this.scheduleFluidTicks) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(GrowingPlantHeadBlock.AGE);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        return this.canGrowInto(bpg.getBlockState(fu.relative(this.growthDirection)));
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bqb, final Random random, final BlockPos fu, final BlockState cfj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        BlockPos fu2 = fu.relative(this.growthDirection);
        for (int integer7 = Math.min(cfj.<Integer>getValue((Property<Integer>)GrowingPlantHeadBlock.AGE) + 1, 25), integer8 = this.getBlocksToGrowWhenBonemealed(random), integer9 = 0; integer9 < integer8 && this.canGrowInto(zd.getBlockState(fu2)); fu2 = fu2.relative(this.growthDirection), integer7 = Math.min(integer7 + 1, 25), ++integer9) {
            zd.setBlockAndUpdate(fu2, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)GrowingPlantHeadBlock.AGE, integer7));
        }
    }
    
    protected abstract int getBlocksToGrowWhenBonemealed(final Random random);
    
    protected abstract boolean canGrowInto(final BlockState cfj);
    
    @Override
    protected GrowingPlantHeadBlock getHeadBlock() {
        return this;
    }
    
    static {
        AGE = BlockStateProperties.AGE_25;
    }
}
