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.material.FluidState;
import java.util.Iterator;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
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.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class SugarCaneBlock extends Block {
    public static final IntegerProperty AGE;
    protected static final VoxelShape SHAPE;
    
    protected SugarCaneBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return SugarCaneBlock.SHAPE;
    }
    
    @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);
        }
        else if (xd.isEmptyBlock(fk.above())) {
            int integer6;
            for (integer6 = 1; xd.getBlockState(fk.below(integer6)).getBlock() == this; ++integer6) {}
            if (integer6 < 3) {
                final int integer7 = byj.<Integer>getValue((Property<Integer>)SugarCaneBlock.AGE);
                if (integer7 == 15) {
                    xd.setBlockAndUpdate(fk.above(), this.defaultBlockState());
                    xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, 0), 4);
                }
                else {
                    xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, integer7 + 1), 4);
                }
            }
        }
    }
    
    @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);
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final Block bph5 = bjz.getBlockState(fk.below()).getBlock();
        if (bph5 == this) {
            return true;
        }
        if (bph5 == Blocks.GRASS_BLOCK || bph5 == Blocks.DIRT || bph5 == Blocks.COARSE_DIRT || bph5 == Blocks.PODZOL || bph5 == Blocks.SAND || bph5 == Blocks.RED_SAND) {
            final BlockPos fk2 = fk.below();
            for (final Direction fp8 : Direction.Plane.HORIZONTAL) {
                final BlockState byj2 = bjz.getBlockState(fk2.relative(fp8));
                final FluidState coj10 = bjz.getFluidState(fk2.relative(fp8));
                if (coj10.is(FluidTags.WATER) || byj2.getBlock() == Blocks.FROSTED_ICE) {
                    return true;
                }
            }
        }
        return false;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SugarCaneBlock.AGE);
    }
    
    static {
        AGE = BlockStateProperties.AGE_15;
        SHAPE = Block.box(2.0, 0.0, 2.0, 14.0, 16.0, 14.0);
    }
}
