package net.minecraft.world.level.block;

import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.FallingBlockEntity;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public class FallingBlock extends Block {
    public FallingBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        bjw.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(bjw));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        bjx.getBlockTicks().scheduleTick(fk5, this, this.getTickDelay(bjx));
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!isFree(xd.getBlockState(fk.below())) || fk.getY() < 0) {
            return;
        }
        final FallingBlockEntity awa6 = new FallingBlockEntity(xd, fk.getX() + 0.5, fk.getY(), fk.getZ() + 0.5, xd.getBlockState(fk));
        this.falling(awa6);
        xd.addFreshEntity(awa6);
    }
    
    protected void falling(final FallingBlockEntity awa) {
    }
    
    @Override
    public int getTickDelay(final LevelReader bjz) {
        return 2;
    }
    
    public static boolean isFree(final BlockState byj) {
        final Block bph2 = byj.getBlock();
        final Material coo3 = byj.getMaterial();
        return byj.isAir() || bph2 == Blocks.FIRE || coo3.isLiquid() || coo3.isReplaceable();
    }
    
    public void onLand(final Level bjw, final BlockPos fk, final BlockState byj3, final BlockState byj4) {
    }
    
    public void onBroken(final Level bjw, final BlockPos fk) {
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        if (random.nextInt(16) == 0) {
            final BlockPos fk2 = fk.below();
            if (isFree(bjw.getBlockState(fk2))) {
                final double double7 = fk.getX() + (double)random.nextFloat();
                final double double8 = fk.getY() - 0.05;
                final double double9 = fk.getZ() + (double)random.nextFloat();
                bjw.addParticle(new BlockParticleOption(ParticleTypes.FALLING_DUST, byj), double7, double8, double9, 0.0, 0.0, 0.0);
            }
        }
    }
    
    public int getDustColor(final BlockState byj) {
        return -16777216;
    }
}
