import { world,system } from "@minecraft/server";


function isFamily(family, entity) {
    try {
    entity.runCommand(`testfor @s[family=${family}]`)
    return true
    } catch { return false }
  }


world.afterEvents.entityHitEntity.subscribe((evd) => {
    if (evd.damagingEntity.typeId === 'minecraft:player' && evd.hitEntity.typeId === 'edx:volley_bomb') {
    const player = evd.damagingEntity;
    const bomb = evd.hitEntity;
    bomb.applyKnockback(player.getViewDirection().x, player.getViewDirection().z, Math.sqrt(player.getViewDirection().x ** 2 + player.getViewDirection().z ** 2)*4, player.getViewDirection().y*1.3)
}

if ((isFamily('monster', evd.hitEntity) === true || isFamily('mob', evd.hitEntity) === true) && isFamily('inanimate', evd.hitEntity) === false && evd.damagingEntity.typeId === 'minecraft:player' && evd.damagingEntity.hasTag("blood_vial")) {
    const player = evd.damagingEntity;
    let health = player.getComponent("health")
    health.setCurrentValue(health.currentValue+0.20)
    evd.hitEntity.runCommand(`particle edx:blood ~~~`);
}
if ((isFamily('monster', evd.hitEntity) === true || isFamily('mob', evd.hitEntity) === true) && evd.damagingEntity.typeId === 'edx:neoplasm_spider') {
    const mob = evd.hitEntity;
    let health = mob.getComponent("health")
    if (health.currentValue === 0) {
        evd.hitEntity.runCommand(`summon edx:small_neoplasm_spider`);
    }
}

if ((evd.hitEntity.typeId === 'minecraft:creeper' || evd.hitEntity.typeId === 'edx:big_creeper') && (evd.damagingEntity.typeId === 'edx:neoplasm_spider' || evd.damagingEntity.typeId === 'edx:small_neoplasm_spider')) {
    const mob = evd.hitEntity;
    let health = mob.getComponent("health")
    health.setCurrentValue(0)
}

if (evd.damagingEntity.typeId === 'minecraft:player') {
if ( evd.damagingEntity.getComponent("minecraft:inventory").container.getItem(evd.damagingEntity.selectedSlot)?.typeId === 'edx:spider_mandibule' && (isFamily('monster', evd.hitEntity) === true || isFamily('mob', evd.hitEntity) === true)) {
    evd.hitEntity.runCommand(`tag @s add bleeding`);
    evd.hitEntity.runCommand(`tag @s remove bleeding4`);
    evd.hitEntity.runCommand(`tag @s remove bleeding3`);
    evd.hitEntity.runCommand(`tag @s remove bleeding2`);
    evd.hitEntity.runCommand(`tag @s remove bleeding1`);
    
}
}


});