import { world, system, TicksPerSecond, EntityDamageCause, ItemComponentHitEntityEvent, EntityComponentTypes, ItemComponent, ItemComponentTypes, EquipmentSlot } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe((eventData) => {
  const { itemComponentRegistry } = eventData;

  itemComponentRegistry.registerCustomComponent("cyd_dino:skull_axe", {
    /**
     * 
     * @param {ItemComponentHitEntityEvent} e 
     */
    onHitEntity(e) {
      if (!e.hadEffect) return;
      const { damagingEntity, hitEntity } = e;
      const slots = [EquipmentSlot.Head, EquipmentSlot.Chest, EquipmentSlot.Legs, EquipmentSlot.Feet];
      const container = hitEntity.getComponent(EntityComponentTypes.Equippable);

      if (!container) return;

      for (const slot of slots) {
        const item = container.getEquipment(slot);
        if (!item) continue;

        damageItem(hitEntity, slot, 4);
      }
    },
  });
});

