import { Block, Dimension, Entity } from "@minecraft/server";

/**
 * Evento onTick del componente mobSensorComponent
 * Addon: Electrical Creativity BP
 * @param {{entity: Entity, block: Block, dimension: Dimension}} event
 */
export const mobSensorOnTickEvent = ({entity, block, dimension}) => {
    try {
        const power = block.permutation.getState('nps_ec:power');
        const filter = block.permutation.getState('nps_ec:filter');
        const distance = block.permutation.getState('nps_ec:distance');

        if (power) {
            const entities = dimension.getEntities({ location: block.location, maxDistance: distance, families: [filter] })

            const isActive = block.permutation.getState('nps_ec:active');
            const shouldBeActive = entities.length > 0 ? 1 : 0;

            if (isActive !== shouldBeActive) {
                const permutation = block.permutation.withState('nps_ec:active', shouldBeActive);
                block.setPermutation(permutation);
            }
        } else {
            const permutation = block.permutation.withState('nps_ec:active', 0);
            block.setPermutation(permutation);
        }
    } catch { }
}