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

const targetIdentifier = "mebesm:miss_delight";

system.runInterval(() => {
    const players = world.getPlayers();
    const targetEntities = world.getDimension("overworld").getEntities({ type: targetIdentifier });

    for (const target of targetEntities) {
        let isBeingLookedAt = false;

        for (const player of players) {
            try {
                const entitiesInView = player.getEntitiesFromViewDirection({
                    maxDistance: 32,
                    ignoreInsaneValues: true
                });

                if (entitiesInView) {
                    for (const entityHit of entitiesInView) {
                        if (entityHit.entity.id === target.id) {
                            isBeingLookedAt = true;
                            break;
                        }
                    }
                }
            } catch (e) { }
            if (isBeingLookedAt) break;
        }

        if (isBeingLookedAt) {
            try {
                target.addEffect("slowness", 20, {
                    amplifier: 255,
                    showParticles: false
                });
            } catch (e) { }
        } else {
            try {
                target.removeEffect("slowness");
            } catch (e) { }
        }
    }
}, 5);