import { Entity, EntityDamageCause, TicksPerSecond, world } from '@minecraft/server';

/**
 * @param {Entity} damagingEntity
 * @param {Entity} hitEntity 
 */
function poison_effect(damagingEntity, hitEntity) {
    try {
        if (!damagingEntity || !hitEntity) return;

        const poison = damagingEntity.getComponent("minecraft:type_family")?.hasTypeFamily('venomous');
        const chance = 0.5;

        if (poison && chance <= Math.random()) hitEntity.addEffect('poison', 2 * TicksPerSecond, { amplifier: 1, showParticles: true });
    } catch (error) {

    }
}

function thorns_effect(damagingEntity, hitEntity) {
    try {
        if (!damagingEntity || !hitEntity) return;

        const thorns = hitEntity.getComponent("minecraft:type_family")?.hasTypeFamily('thorns');

        if (thorns) damagingEntity.applyDamage(2, { cause: EntityDamageCause.entityAttack });

    } catch (error) {

    }
}

world.afterEvents.entityHitEntity.subscribe((e) => {
    poison_effect(e.damagingEntity, e.hitEntity)
    thorns_effect(e.damagingEntity, e.hitEntity)
})

