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.block.state.StateDefinition;
import net.minecraft.world.level.Level;
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.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.grower.AbstractTreeGrower;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class SaplingBlock extends BushBlock implements BonemealableBlock {
    public static final IntegerProperty STAGE;
    protected static final VoxelShape SHAPE;
    private final AbstractTreeGrower treeGrower;
    
    protected SaplingBlock(final AbstractTreeGrower ces, final Properties c) {
        super(c);
        this.treeGrower = ces;
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SaplingBlock.STAGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return SaplingBlock.SHAPE;
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (zd.getMaxLocalRawBrightness(fu.above()) >= 9 && random.nextInt(7) == 0) {
            this.advanceTree(zd, fu, cfj, random);
        }
    }
    
    public void advanceTree(final ServerLevel zd, final BlockPos fu, final BlockState cfj, final Random random) {
        if (cfj.<Integer>getValue((Property<Integer>)SaplingBlock.STAGE) == 0) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)SaplingBlock.STAGE), 4);
        }
        else {
            this.treeGrower.growTree(zd, zd.getChunkSource().getGenerator(), fu, cfj, random);
        }
    }
    
    @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 bqb.random.nextFloat() < 0.45;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        this.advanceTree(zd, fu, cfj, random);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SaplingBlock.STAGE);
    }
    
    static {
        STAGE = BlockStateProperties.STAGE;
        SHAPE = Block.box(2.0, 0.0, 2.0, 14.0, 12.0, 14.0);
    }
}
