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.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.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.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(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, 0));
    }
    
    @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;
        }
        final BlockPos fk2 = fk.above();
        if (!xd.isEmptyBlock(fk2)) {
            return;
        }
        int integer7;
        for (integer7 = 1; xd.getBlockState(fk.below(integer7)).getBlock() == this; ++integer7) {}
        if (integer7 >= 3) {
            return;
        }
        final int integer8 = byj.<Integer>getValue((Property<Integer>)CactusBlock.AGE);
        if (integer8 == 15) {
            xd.setBlockAndUpdate(fk2, this.defaultBlockState());
            final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, 0);
            xd.setBlock(fk, byj2, 4);
            byj2.neighborChanged(xd, fk2, this, fk, false);
        }
        else {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)CactusBlock.AGE, integer8 + 1), 4);
        }
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return CactusBlock.COLLISION_SHAPE;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return CactusBlock.OUTLINE_SHAPE;
    }
    
    @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) {
        for (final Direction fp6 : Direction.Plane.HORIZONTAL) {
            final BlockState byj2 = bjz.getBlockState(fk.relative(fp6));
            final Material coo8 = byj2.getMaterial();
            if (coo8.isSolid() || bjz.getFluidState(fk.relative(fp6)).is(FluidTags.LAVA)) {
                return false;
            }
        }
        final Block bph5 = bjz.getBlockState(fk.below()).getBlock();
        return (bph5 == Blocks.CACTUS || bph5 == Blocks.SAND || bph5 == Blocks.RED_SAND) && !bjz.getBlockState(fk.above()).getMaterial().isLiquid();
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        akq.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 byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        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);
    }
}
