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.item.SwordItem;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.Vec3;
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.StateDefinition;
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.state.properties.BambooLeaves;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.VoxelShape;

public class BambooBlock extends Block implements BonemealableBlock {
    protected static final VoxelShape SMALL_SHAPE;
    protected static final VoxelShape LARGE_SHAPE;
    protected static final VoxelShape COLLISION_SHAPE;
    public static final IntegerProperty AGE;
    public static final EnumProperty<BambooLeaves> LEAVES;
    public static final IntegerProperty STAGE;
    
    public BambooBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)BambooBlock.AGE, 0)).setValue(BambooBlock.LEAVES, BambooLeaves.NONE)).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.STAGE, 0));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(BambooBlock.AGE, BambooBlock.LEAVES, BambooBlock.STAGE);
    }
    
    @Override
    public OffsetType getOffsetType() {
        return OffsetType.XZ;
    }
    
    @Override
    public boolean propagatesSkylightDown(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        final VoxelShape dfg6 = (cfj.<BambooLeaves>getValue(BambooBlock.LEAVES) == BambooLeaves.LARGE) ? BambooBlock.LARGE_SHAPE : BambooBlock.SMALL_SHAPE;
        final Vec3 dem7 = cfj.getOffset(bpg, fu);
        return dfg6.move(dem7.x, dem7.y, dem7.z);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        final Vec3 dem6 = cfj.getOffset(bpg, fu);
        return BambooBlock.COLLISION_SHAPE.move(dem6.x, dem6.y, dem6.z);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final FluidState cxa3 = bin.getLevel().getFluidState(bin.getClickedPos());
        if (!cxa3.isEmpty()) {
            return null;
        }
        final BlockState cfj4 = bin.getLevel().getBlockState(bin.getClickedPos().below());
        if (!cfj4.is(BlockTags.BAMBOO_PLANTABLE_ON)) {
            return null;
        }
        if (cfj4.is(Blocks.BAMBOO_SAPLING)) {
            return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.AGE, 0);
        }
        if (cfj4.is(Blocks.BAMBOO)) {
            final int integer5 = (cfj4.<Integer>getValue((Property<Integer>)BambooBlock.AGE) > 0) ? 1 : 0;
            return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.AGE, integer5);
        }
        return Blocks.BAMBOO_SAPLING.defaultBlockState();
    }
    
    @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>)BambooBlock.STAGE) == 0;
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (cfj.<Integer>getValue((Property<Integer>)BambooBlock.STAGE) != 0) {
            return;
        }
        if (random.nextInt(3) == 0 && zd.isEmptyBlock(fu.above()) && zd.getRawBrightness(fu.above(), 0) >= 9) {
            final int integer6 = this.getHeightBelowUpToMax(zd, fu) + 1;
            if (integer6 < 16) {
                this.growBamboo(cfj, zd, fu, random, integer6);
            }
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        return bqd.getBlockState(fu.below()).is(BlockTags.BAMBOO_PLANTABLE_ON);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (!cfj1.canSurvive(bqc, fu5)) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        if (fz == Direction.UP && cfj3.is(Blocks.BAMBOO) && cfj3.<Integer>getValue((Property<Integer>)BambooBlock.AGE) > cfj1.<Integer>getValue((Property<Integer>)BambooBlock.AGE)) {
            bqc.setBlock(fu5, ((StateHolder<O, BlockState>)cfj1).<Comparable>cycle((Property<Comparable>)BambooBlock.AGE), 2);
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        final int integer6 = this.getHeightAboveUpToMax(bpg, fu);
        final int integer7 = this.getHeightBelowUpToMax(bpg, fu);
        return integer6 + integer7 + 1 < 16 && bpg.getBlockState(fu.above(integer6)).<Integer>getValue((Property<Integer>)BambooBlock.STAGE) != 1;
    }
    
    @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) {
        int integer6 = this.getHeightAboveUpToMax(zd, fu);
        final int integer7 = this.getHeightBelowUpToMax(zd, fu);
        int integer8 = integer6 + integer7 + 1;
        for (int integer9 = 1 + random.nextInt(2), integer10 = 0; integer10 < integer9; ++integer10) {
            final BlockPos fu2 = fu.above(integer6);
            final BlockState cfj2 = zd.getBlockState(fu2);
            if (integer8 >= 16 || cfj2.<Integer>getValue((Property<Integer>)BambooBlock.STAGE) == 1 || !zd.isEmptyBlock(fu2.above())) {
                return;
            }
            this.growBamboo(cfj2, zd, fu2, random, integer8);
            ++integer6;
            ++integer8;
        }
    }
    
    @Override
    public float getDestroyProgress(final BlockState cfj, final Player bec, final BlockGetter bpg, final BlockPos fu) {
        if (bec.getMainHandItem().getItem() instanceof SwordItem) {
            return 1.0f;
        }
        return super.getDestroyProgress(cfj, bec, bpg, fu);
    }
    
    protected void growBamboo(final BlockState cfj, final Level bqb, final BlockPos fu, final Random random, final int integer) {
        final BlockState cfj2 = bqb.getBlockState(fu.below());
        final BlockPos fu2 = fu.below(2);
        final BlockState cfj3 = bqb.getBlockState(fu2);
        BambooLeaves cfw10 = BambooLeaves.NONE;
        if (integer >= 1) {
            if (!cfj2.is(Blocks.BAMBOO) || cfj2.<BambooLeaves>getValue(BambooBlock.LEAVES) == BambooLeaves.NONE) {
                cfw10 = BambooLeaves.SMALL;
            }
            else if (cfj2.is(Blocks.BAMBOO) && cfj2.<BambooLeaves>getValue(BambooBlock.LEAVES) != BambooLeaves.NONE) {
                cfw10 = BambooLeaves.LARGE;
                if (cfj3.is(Blocks.BAMBOO)) {
                    bqb.setBlock(fu.below(), ((StateHolder<O, BlockState>)cfj2).<BambooLeaves, BambooLeaves>setValue(BambooBlock.LEAVES, BambooLeaves.SMALL), 3);
                    bqb.setBlock(fu2, ((StateHolder<O, BlockState>)cfj3).<BambooLeaves, BambooLeaves>setValue(BambooBlock.LEAVES, BambooLeaves.NONE), 3);
                }
            }
        }
        final int integer2 = (cfj.<Integer>getValue((Property<Integer>)BambooBlock.AGE) == 1 || cfj3.is(Blocks.BAMBOO)) ? 1 : 0;
        final int integer3 = ((integer >= 11 && random.nextFloat() < 0.25f) || integer == 15) ? 1 : 0;
        bqb.setBlock(fu.above(), ((((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)BambooBlock.AGE, integer2)).setValue(BambooBlock.LEAVES, cfw10)).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.STAGE, integer3), 3);
    }
    
    protected int getHeightAboveUpToMax(final BlockGetter bpg, final BlockPos fu) {
        int integer4;
        for (integer4 = 0; integer4 < 16 && bpg.getBlockState(fu.above(integer4 + 1)).is(Blocks.BAMBOO); ++integer4) {}
        return integer4;
    }
    
    protected int getHeightBelowUpToMax(final BlockGetter bpg, final BlockPos fu) {
        int integer4;
        for (integer4 = 0; integer4 < 16 && bpg.getBlockState(fu.below(integer4 + 1)).is(Blocks.BAMBOO); ++integer4) {}
        return integer4;
    }
    
    static {
        SMALL_SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 16.0, 11.0);
        LARGE_SHAPE = Block.box(3.0, 0.0, 3.0, 13.0, 16.0, 13.0);
        COLLISION_SHAPE = Block.box(6.5, 0.0, 6.5, 9.5, 16.0, 9.5);
        AGE = BlockStateProperties.AGE_1;
        LEAVES = BlockStateProperties.BAMBOO_LEAVES;
        STAGE = BlockStateProperties.STAGE;
    }
}
