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.level.block.state.StateDefinition;
import net.minecraft.world.entity.projectile.Projectile;
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.BlockBehaviour;
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(((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Boolean>setValue((Property<Comparable>)TntBlock.UNSTABLE, false));
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        if (bqb.hasNeighborSignal(fu)) {
            explode(bqb, fu);
            bqb.removeBlock(fu, false);
        }
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (bqb.hasNeighborSignal(fu3)) {
            explode(bqb, fu3);
            bqb.removeBlock(fu3, false);
        }
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        if (!bqb.isClientSide() && !bec.isCreative() && cfj.<Boolean>getValue((Property<Boolean>)TntBlock.UNSTABLE)) {
            explode(bqb, fu);
        }
        super.playerWillDestroy(bqb, fu, cfj, bec);
    }
    
    @Override
    public void wasExploded(final Level bqb, final BlockPos fu, final Explosion bpt) {
        if (bqb.isClientSide) {
            return;
        }
        final PrimedTnt bbh5 = new PrimedTnt(bqb, fu.getX() + 0.5, fu.getY(), fu.getZ() + 0.5, bpt.getSourceMob());
        bbh5.setFuse((short)(bqb.random.nextInt(bbh5.getLife() / 4) + bbh5.getLife() / 8));
        bqb.addFreshEntity(bbh5);
    }
    
    public static void explode(final Level bqb, final BlockPos fu) {
        explode(bqb, fu, null);
    }
    
    private static void explode(final Level bqb, final BlockPos fu, @Nullable final LivingEntity aoy) {
        if (bqb.isClientSide) {
            return;
        }
        final PrimedTnt bbh4 = new PrimedTnt(bqb, fu.getX() + 0.5, fu.getY(), fu.getZ() + 0.5, aoy);
        bqb.addFreshEntity(bbh4);
        bqb.playSound(null, bbh4.getX(), bbh4.getY(), bbh4.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0f, 1.0f);
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final ItemStack bki8 = bec.getItemInHand(anf);
        final Item bke9 = bki8.getItem();
        if (bke9 == Items.FLINT_AND_STEEL || bke9 == Items.FIRE_CHARGE) {
            explode(bqb, fu, bec);
            bqb.setBlock(fu, Blocks.AIR.defaultBlockState(), 11);
            if (!bec.isCreative()) {
                if (bke9 == Items.FLINT_AND_STEEL) {
                    bki8.<Player>hurtAndBreak(1, bec, bec -> bec.broadcastBreakEvent(anf));
                }
                else {
                    bki8.shrink(1);
                }
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        return super.use(cfj, bqb, fu, bec, anf, deh);
    }
    
    @Override
    public void onProjectileHit(final Level bqb, final BlockState cfj, final BlockHitResult deh, final Projectile bes) {
        if (!bqb.isClientSide) {
            final Entity aom6 = bes.getOwner();
            if (bes.isOnFire()) {
                final BlockPos fu7 = deh.getBlockPos();
                explode(bqb, fu7, (aom6 instanceof LivingEntity) ? ((LivingEntity)aom6) : null);
                bqb.removeBlock(fu7, false);
            }
        }
    }
    
    @Override
    public boolean dropFromExplosion(final Explosion bpt) {
        return false;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TntBlock.UNSTABLE);
    }
    
    static {
        UNSTABLE = BlockStateProperties.UNSTABLE;
    }
}
