import * as MC from "@minecraft/server";

const run = (callback, interval = 20) => {
    MC.system.runInterval(() => callback(MC.world.getAllPlayers()), interval);
}

const onHitEntity = (callback) => {
    MC.world.afterEvents.entityHitEntity.subscribe((event) => callback(event));
}

const onHurtEntity = (callback) => {
    MC.world.afterEvents.entityHurt.subscribe((event) => callback(event));
}

const onDeathEntity = (callback) => {
    MC.world.beforeEvents.entityRemove.subscribe(({ removedEntity }) => {
        const health = removedEntity.getComponent("minecraft:health")?.currentValue;
        if (health === 0) {
            return callback(removedEntity);
        }
    });
}

const onItem = (callback) => {
    MC.world.afterEvents.itemUse.subscribe((event) => callback(event));
}

const onInteract = (callback) => {
    MC.world.beforeEvents.playerInteractWithBlock.subscribe((event) => callback(event));
}

export { run, onHitEntity, onHurtEntity, onDeathEntity, onItem, onInteract }