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.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.Vec3i;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
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.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class LeavesBlock extends Block {
    public static final IntegerProperty DISTANCE;
    public static final BooleanProperty PERSISTENT;
    
    public LeavesBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LeavesBlock.DISTANCE, 7)).<Comparable, Boolean>setValue((Property<Comparable>)LeavesBlock.PERSISTENT, false));
    }
    
    @Override
    public VoxelShape getBlockSupportShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return Shapes.empty();
    }
    
    @Override
    public boolean isRandomlyTicking(final BlockState cfj) {
        return cfj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) == 7 && !cfj.<Boolean>getValue((Property<Boolean>)LeavesBlock.PERSISTENT);
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)LeavesBlock.PERSISTENT) && cfj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) == 7) {
            Block.dropResources(cfj, zd, fu);
            zd.removeBlock(fu, false);
        }
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        zd.setBlock(fu, updateDistance(cfj, zd, fu), 3);
    }
    
    @Override
    public int getLightBlock(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return 1;
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        final int integer8 = getDistanceAt(cfj3) + 1;
        if (integer8 != 1 || cfj1.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) != integer8) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        return cfj1;
    }
    
    private static BlockState updateDistance(final BlockState cfj, final LevelAccessor bqc, final BlockPos fu) {
        int integer4 = 7;
        final BlockPos.MutableBlockPos a5 = new BlockPos.MutableBlockPos();
        for (final Direction fz9 : Direction.values()) {
            a5.setWithOffset(fu, fz9);
            integer4 = Math.min(integer4, getDistanceAt(bqc.getBlockState(a5)) + 1);
            if (integer4 == 1) {
                break;
            }
        }
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)LeavesBlock.DISTANCE, integer4);
    }
    
    private static int getDistanceAt(final BlockState cfj) {
        if (BlockTags.LOGS.contains(cfj.getBlock())) {
            return 0;
        }
        if (cfj.getBlock() instanceof LeavesBlock) {
            return cfj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE);
        }
        return 7;
    }
    
    @Override
    public void animateTick(final BlockState cfj, final Level bqb, final BlockPos fu, final Random random) {
        if (!bqb.isRainingAt(fu.above())) {
            return;
        }
        if (random.nextInt(15) != 1) {
            return;
        }
        final BlockPos fu2 = fu.below();
        final BlockState cfj2 = bqb.getBlockState(fu2);
        if (cfj2.canOcclude() && cfj2.isFaceSturdy(bqb, fu2, Direction.UP)) {
            return;
        }
        final double double8 = fu.getX() + random.nextDouble();
        final double double9 = fu.getY() - 0.05;
        final double double10 = fu.getZ() + random.nextDouble();
        bqb.addParticle(ParticleTypes.DRIPPING_WATER, double8, double9, double10, 0.0, 0.0, 0.0);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LeavesBlock.DISTANCE, LeavesBlock.PERSISTENT);
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return updateDistance(((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Boolean>setValue((Property<Comparable>)LeavesBlock.PERSISTENT, true), bin.getLevel(), bin.getClickedPos());
    }
    
    static {
        DISTANCE = BlockStateProperties.DISTANCE;
        PERSISTENT = BlockStateProperties.PERSISTENT;
    }
}
