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

world.afterEvents.entityHitEntity.subscribe(onPlayerHit());

function onPlayerHit() {
    return e => {
        const player = e.damagingEntity;
        const CROWN_ID = "gs_ce:crown";
        if (!(player instanceof Player)) return;

        const equippable = player.getComponent("equippable");
        if (!equippable) return;

        const headSlot = equippable.getEquipmentSlot(EquipmentSlot.Head);
        if (!headSlot) return;

        const item = headSlot.getItem();
        if (!item) return;

        if (headSlot.typeId === CROWN_ID) {
            player.runCommandAsync('/effect @s regeneration 1 2 true');
        }
    };
}
