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.block.state.StateDefinition;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import javax.annotation.Nullable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.PrimedTnt;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
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;

public class TntBlock extends Block {
    public static final BooleanProperty UNSTABLE;
    
    public TntBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Boolean>setValue((Property<Comparable>)TntBlock.UNSTABLE, false));
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj4.getBlock() == byj1.getBlock()) {
            return;
        }
        if (bjw.hasNeighborSignal(fk)) {
            explode(bjw, fk);
            bjw.removeBlock(fk, false);
        }
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (bjw.hasNeighborSignal(fk3)) {
            explode(bjw, fk3);
            bjw.removeBlock(fk3, false);
        }
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        if (!bjw.isClientSide() && !ayj.isCreative() && byj.<Boolean>getValue((Property<Boolean>)TntBlock.UNSTABLE)) {
            explode(bjw, fk);
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    @Override
    public void wasExploded(final Level bjw, final BlockPos fk, final Explosion bjp) {
        if (bjw.isClientSide) {
            return;
        }
        final PrimedTnt awc5 = new PrimedTnt(bjw, fk.getX() + 0.5f, fk.getY(), fk.getZ() + 0.5f, bjp.getSourceMob());
        awc5.setFuse((short)(bjw.random.nextInt(awc5.getLife() / 4) + awc5.getLife() / 8));
        bjw.addFreshEntity(awc5);
    }
    
    public static void explode(final Level bjw, final BlockPos fk) {
        explode(bjw, fk, null);
    }
    
    private static void explode(final Level bjw, final BlockPos fk, @Nullable final LivingEntity akz) {
        if (bjw.isClientSide) {
            return;
        }
        final PrimedTnt awc4 = new PrimedTnt(bjw, fk.getX() + 0.5, fk.getY(), fk.getZ() + 0.5, akz);
        bjw.addFreshEntity(awc4);
        bjw.playSound(null, awc4.getX(), awc4.getY(), awc4.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0f, 1.0f);
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final ItemStack ben8 = ayj.getItemInHand(ajk);
        final Item bei9 = ben8.getItem();
        if (bei9 == Items.FLINT_AND_STEEL || bei9 == Items.FIRE_CHARGE) {
            explode(bjw, fk, ayj);
            bjw.setBlock(fk, Blocks.AIR.defaultBlockState(), 11);
            if (!ayj.isCreative()) {
                if (bei9 == Items.FLINT_AND_STEEL) {
                    ben8.<Player>hurtAndBreak(1, ayj, ayj -> ayj.broadcastBreakEvent(ajk));
                }
                else {
                    ben8.shrink(1);
                }
            }
            return InteractionResult.SUCCESS;
        }
        return super.use(byj, bjw, fk, ayj, ajk, cvg);
    }
    
    @Override
    public void onProjectileHit(final Level bjw, final BlockState byj, final BlockHitResult cvg, final Entity akq) {
        if (!bjw.isClientSide && akq instanceof AbstractArrow) {
            final AbstractArrow ayn6 = (AbstractArrow)akq;
            final Entity akq2 = ayn6.getOwner();
            if (ayn6.isOnFire()) {
                final BlockPos fk8 = cvg.getBlockPos();
                explode(bjw, fk8, (akq2 instanceof LivingEntity) ? ((LivingEntity)akq2) : null);
                bjw.removeBlock(fk8, false);
            }
        }
    }
    
    @Override
    public boolean dropFromExplosion(final Explosion bjp) {
        return false;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TntBlock.UNSTABLE);
    }
    
    static {
        UNSTABLE = BlockStateProperties.UNSTABLE;
    }
}
