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.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.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.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(((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        final VoxelShape cwf6 = (byj.<BambooLeaves>getValue(BambooBlock.LEAVES) == BambooLeaves.LARGE) ? BambooBlock.LARGE_SHAPE : BambooBlock.SMALL_SHAPE;
        final Vec3 cvl7 = byj.getOffset(bjg, fk);
        return cwf6.move(cvl7.x, cvl7.y, cvl7.z);
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        return false;
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        final Vec3 cvl6 = byj.getOffset(bjg, fk);
        return BambooBlock.COLLISION_SHAPE.move(cvl6.x, cvl6.y, cvl6.z);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final FluidState coj3 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        if (!coj3.isEmpty()) {
            return null;
        }
        final BlockState byj4 = bcq.getLevel().getBlockState(bcq.getClickedPos().below());
        if (!byj4.is(BlockTags.BAMBOO_PLANTABLE_ON)) {
            return null;
        }
        final Block bph5 = byj4.getBlock();
        if (bph5 == Blocks.BAMBOO_SAPLING) {
            return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.AGE, 0);
        }
        if (bph5 == Blocks.BAMBOO) {
            final int integer6 = (byj4.<Integer>getValue((Property<Integer>)BambooBlock.AGE) > 0) ? 1 : 0;
            return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.AGE, integer6);
        }
        return Blocks.BAMBOO_SAPLING.defaultBlockState();
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!byj.canSurvive(xd, fk)) {
            xd.destroyBlock(fk, true);
            return;
        }
        if (byj.<Integer>getValue((Property<Integer>)BambooBlock.STAGE) != 0) {
            return;
        }
        if (random.nextInt(3) == 0 && xd.isEmptyBlock(fk.above()) && xd.getRawBrightness(fk.above(), 0) >= 9) {
            final int integer6 = this.getHeightBelowUpToMax(xd, fk) + 1;
            if (integer6 < 16) {
                this.growBamboo(byj, xd, fk, random, integer6);
            }
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        return bjz.getBlockState(fk.below()).is(BlockTags.BAMBOO_PLANTABLE_ON);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!byj1.canSurvive(bjx, fk5)) {
            bjx.getBlockTicks().scheduleTick(fk5, this, 1);
        }
        if (fp == Direction.UP && byj3.getBlock() == Blocks.BAMBOO && byj3.<Integer>getValue((Property<Integer>)BambooBlock.AGE) > byj1.<Integer>getValue((Property<Integer>)BambooBlock.AGE)) {
            bjx.setBlock(fk5, ((AbstractStateHolder<O, BlockState>)byj1).<Comparable>cycle((Property<Comparable>)BambooBlock.AGE), 2);
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        final int integer6 = this.getHeightAboveUpToMax(bjg, fk);
        final int integer7 = this.getHeightBelowUpToMax(bjg, fk);
        return integer6 + integer7 + 1 < 16 && bjg.getBlockState(fk.above(integer6)).<Integer>getValue((Property<Integer>)BambooBlock.STAGE) != 1;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        int integer6 = this.getHeightAboveUpToMax(xd, fk);
        final int integer7 = this.getHeightBelowUpToMax(xd, fk);
        int integer8 = integer6 + integer7 + 1;
        for (int integer9 = 1 + random.nextInt(2), integer10 = 0; integer10 < integer9; ++integer10) {
            final BlockPos fk2 = fk.above(integer6);
            final BlockState byj2 = xd.getBlockState(fk2);
            if (integer8 >= 16 || byj2.<Integer>getValue((Property<Integer>)BambooBlock.STAGE) == 1 || !xd.isEmptyBlock(fk2.above())) {
                return;
            }
            this.growBamboo(byj2, xd, fk2, random, integer8);
            ++integer6;
            ++integer8;
        }
    }
    
    @Override
    public float getDestroyProgress(final BlockState byj, final Player ayj, final BlockGetter bjg, final BlockPos fk) {
        if (ayj.getMainHandItem().getItem() instanceof SwordItem) {
            return 1.0f;
        }
        return super.getDestroyProgress(byj, ayj, bjg, fk);
    }
    
    protected void growBamboo(final BlockState byj, final Level bjw, final BlockPos fk, final Random random, final int integer) {
        final BlockState byj2 = bjw.getBlockState(fk.below());
        final BlockPos fk2 = fk.below(2);
        final BlockState byj3 = bjw.getBlockState(fk2);
        BambooLeaves byx10 = BambooLeaves.NONE;
        if (integer >= 1) {
            if (byj2.getBlock() != Blocks.BAMBOO || byj2.<BambooLeaves>getValue(BambooBlock.LEAVES) == BambooLeaves.NONE) {
                byx10 = BambooLeaves.SMALL;
            }
            else if (byj2.getBlock() == Blocks.BAMBOO && byj2.<BambooLeaves>getValue(BambooBlock.LEAVES) != BambooLeaves.NONE) {
                byx10 = BambooLeaves.LARGE;
                if (byj3.getBlock() == Blocks.BAMBOO) {
                    bjw.setBlock(fk.below(), ((AbstractStateHolder<O, BlockState>)byj2).<BambooLeaves, BambooLeaves>setValue(BambooBlock.LEAVES, BambooLeaves.SMALL), 3);
                    bjw.setBlock(fk2, ((AbstractStateHolder<O, BlockState>)byj3).<BambooLeaves, BambooLeaves>setValue(BambooBlock.LEAVES, BambooLeaves.NONE), 3);
                }
            }
        }
        final int integer2 = (byj.<Integer>getValue((Property<Integer>)BambooBlock.AGE) == 1 || byj3.getBlock() == Blocks.BAMBOO) ? 1 : 0;
        final int integer3 = ((integer >= 11 && random.nextFloat() < 0.25f) || integer == 15) ? 1 : 0;
        bjw.setBlock(fk.above(), ((((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)BambooBlock.AGE, integer2)).setValue(BambooBlock.LEAVES, byx10)).<Comparable, Integer>setValue((Property<Comparable>)BambooBlock.STAGE, integer3), 3);
    }
    
    protected int getHeightAboveUpToMax(final BlockGetter bjg, final BlockPos fk) {
        int integer4;
        for (integer4 = 0; integer4 < 16 && bjg.getBlockState(fk.above(integer4 + 1)).getBlock() == Blocks.BAMBOO; ++integer4) {}
        return integer4;
    }
    
    protected int getHeightBelowUpToMax(final BlockGetter bjg, final BlockPos fk) {
        int integer4;
        for (integer4 = 0; integer4 < 16 && bjg.getBlockState(fk.below(integer4 + 1)).getBlock() == 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;
    }
}
