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.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.entity.EntityType;
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.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
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.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((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LeavesBlock.DISTANCE, 7)).<Comparable, Boolean>setValue((Property<Comparable>)LeavesBlock.PERSISTENT, false));
    }
    
    @Override
    public boolean isRandomlyTicking(final BlockState byj) {
        return byj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) == 7 && !byj.<Boolean>getValue((Property<Boolean>)LeavesBlock.PERSISTENT);
    }
    
    @Override
    public void randomTick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!byj.<Boolean>getValue((Property<Boolean>)LeavesBlock.PERSISTENT) && byj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) == 7) {
            Block.dropResources(byj, xd, fk);
            xd.removeBlock(fk, false);
        }
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        xd.setBlock(fk, updateDistance(byj, xd, fk), 3);
    }
    
    @Override
    public int getLightBlock(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return 1;
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        final int integer8 = getDistanceAt(byj3) + 1;
        if (integer8 != 1 || byj1.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE) != integer8) {
            bjx.getBlockTicks().scheduleTick(fk5, this, 1);
        }
        return byj1;
    }
    
    private static BlockState updateDistance(final BlockState byj, final LevelAccessor bjx, final BlockPos fk) {
        int integer4 = 7;
        try (final BlockPos.PooledMutableBlockPos b5 = BlockPos.PooledMutableBlockPos.acquire()) {
            for (final Direction fp10 : Direction.values()) {
                b5.set((Vec3i)fk).move(fp10);
                integer4 = Math.min(integer4, getDistanceAt(bjx.getBlockState(b5)) + 1);
                if (integer4 == 1) {
                    break;
                }
            }
        }
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)LeavesBlock.DISTANCE, integer4);
    }
    
    private static int getDistanceAt(final BlockState byj) {
        if (BlockTags.LOGS.contains(byj.getBlock())) {
            return 0;
        }
        if (byj.getBlock() instanceof LeavesBlock) {
            return byj.<Integer>getValue((Property<Integer>)LeavesBlock.DISTANCE);
        }
        return 7;
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        if (!bjw.isRainingAt(fk.above())) {
            return;
        }
        if (random.nextInt(15) != 1) {
            return;
        }
        final BlockPos fk2 = fk.below();
        final BlockState byj2 = bjw.getBlockState(fk2);
        if (byj2.canOcclude() && byj2.isFaceSturdy(bjw, fk2, Direction.UP)) {
            return;
        }
        final double double8 = fk.getX() + random.nextFloat();
        final double double9 = fk.getY() - 0.05;
        final double double10 = fk.getZ() + random.nextFloat();
        bjw.addParticle(ParticleTypes.DRIPPING_WATER, double8, double9, double10, 0.0, 0.0, 0.0);
    }
    
    @Override
    public boolean isSuffocating(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return false;
    }
    
    @Override
    public boolean isValidSpawn(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final EntityType<?> aku) {
        return aku == EntityType.OCELOT || aku == EntityType.PARROT;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LeavesBlock.DISTANCE, LeavesBlock.PERSISTENT);
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return updateDistance(((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Boolean>setValue((Property<Comparable>)LeavesBlock.PERSISTENT, true), bcq.getLevel(), bcq.getClickedPos());
    }
    
    static {
        DISTANCE = BlockStateProperties.DISTANCE;
        PERSISTENT = BlockStateProperties.PERSISTENT;
    }
}
