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.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.material.Material;
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.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
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 CactusBlock extends Block {
    public static final IntegerProperty AGE;
    protected static final VoxelShape COLLISION_SHAPE;
    protected static final VoxelShape OUTLINE_SHAPE;
    
    protected CactusBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, 0));
    }
    
    @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) {
        final BlockPos fu2 = fu.above();
        if (!zd.isEmptyBlock(fu2)) {
            return;
        }
        int integer7;
        for (integer7 = 1; zd.getBlockState(fu.below(integer7)).is(this); ++integer7) {}
        if (integer7 >= 3) {
            return;
        }
        final int integer8 = cfj.<Integer>getValue((Property<Integer>)CactusBlock.AGE);
        if (integer8 == 15) {
            zd.setBlockAndUpdate(fu2, this.defaultBlockState());
            final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, 0);
            zd.setBlock(fu, cfj2, 4);
            cfj2.neighborChanged(zd, fu2, this, fu, false);
        }
        else {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, integer8 + 1), 4);
        }
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return CactusBlock.COLLISION_SHAPE;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return CactusBlock.OUTLINE_SHAPE;
    }
    
    @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) {
        for (final Direction fz6 : Direction.Plane.HORIZONTAL) {
            final BlockState cfj2 = bqd.getBlockState(fu.relative(fz6));
            final Material cxd8 = cfj2.getMaterial();
            if (cxd8.isSolid() || bqd.getFluidState(fu.relative(fz6)).is(FluidTags.LAVA)) {
                return false;
            }
        }
        final BlockState cfj3 = bqd.getBlockState(fu.below());
        return (cfj3.is(Blocks.CACTUS) || cfj3.is(Blocks.SAND) || cfj3.is(Blocks.RED_SAND)) && !bqd.getBlockState(fu.above()).getMaterial().isLiquid();
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        aom.hurt(DamageSource.CACTUS, 1.0f);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(CactusBlock.AGE);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        AGE = BlockStateProperties.AGE_15;
        COLLISION_SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 15.0, 15.0);
        OUTLINE_SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 16.0, 15.0);
    }
}
