import { world } from '@minecraft/server'

world.afterEvents.entityHurt.subscribe(event => {
    const attacker = event.damageSource.damagingEntity
    const dinosaur = event.hurtEntity

    if (!attacker || !dinosaur) return
    if (!dinosaur.typeId.startsWith('cyd_dino:hunter_')) return

    const rideable = dinosaur.getComponent('minecraft:rideable')
    if (rideable && rideable.getRiders().length >= 1) {
        const mountedNpc = rideable.getRiders()[0]
        if (!mountedNpc) return

        try {
            attacker.addTag('hunter_target')
            mountedNpc.triggerEvent('cyd_dino:defend_dino')
        } catch ($) { }
    }
})