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.material.FluidState;
import java.util.Iterator;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
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.level.block.state.BlockBehaviour;
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(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return SugarCaneBlock.SHAPE;
    }
    
    @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 void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (zd.isEmptyBlock(fu.above())) {
            int integer6;
            for (integer6 = 1; zd.getBlockState(fu.below(integer6)).is(this); ++integer6) {}
            if (integer6 < 3) {
                final int integer7 = cfj.<Integer>getValue((Property<Integer>)SugarCaneBlock.AGE);
                if (integer7 == 15) {
                    zd.setBlockAndUpdate(fu.above(), this.defaultBlockState());
                    zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, 0), 4);
                }
                else {
                    zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SugarCaneBlock.AGE, integer7 + 1), 4);
                }
            }
        }
    }
    
    @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);
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockState cfj2 = bqd.getBlockState(fu.below());
        if (cfj2.getBlock() == this) {
            return true;
        }
        if (cfj2.is(Blocks.GRASS_BLOCK) || cfj2.is(Blocks.DIRT) || cfj2.is(Blocks.COARSE_DIRT) || cfj2.is(Blocks.PODZOL) || cfj2.is(Blocks.SAND) || cfj2.is(Blocks.RED_SAND)) {
            final BlockPos fu2 = fu.below();
            for (final Direction fz8 : Direction.Plane.HORIZONTAL) {
                final BlockState cfj3 = bqd.getBlockState(fu2.relative(fz8));
                final FluidState cxa10 = bqd.getFluidState(fu2.relative(fz8));
                if (cxa10.is(FluidTags.WATER) || cfj3.is(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);
    }
}
