package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
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.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 bxs, final Properties c) {
        super(c);
        this.treeGrower = bxs;
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SaplingBlock.STAGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return SaplingBlock.SHAPE;
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        super.tick(byj, xd, fk, random);
        if (xd.getMaxLocalRawBrightness(fk.above()) >= 9 && random.nextInt(7) == 0) {
            this.advanceTree(xd, fk, byj, random);
        }
    }
    
    public void advanceTree(final ServerLevel xd, final BlockPos fk, final BlockState byj, final Random random) {
        if (byj.<Integer>getValue((Property<Integer>)SaplingBlock.STAGE) == 0) {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)SaplingBlock.STAGE), 4);
        }
        else {
            this.treeGrower.growTree(xd, xd.getChunkSource().getGenerator(), fk, byj, random);
        }
    }
    
    @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 bjw.random.nextFloat() < 0.45;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        this.advanceTree(xd, fk, byj, 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);
    }
}
