
function getRadiusEntity(dimension, position, radius, family = ["player"]) {
    if (!dimension || !position || radius <= 0) {
        throw new Error("Invalid source, position, or radius for entity search");
    }
    return dimension.getEntities({
        location: position,
        families: family,
        maxDistance: radius,
        excludeFamilies: ["inanimate"],
        excludeTypes: [
            "minecraft:item",
            "minecraft:xp_orb",
            "minecraft:npc",
            "minecraft:arrow",
            "minecraft:thrown_trident",
        ],
    });
}

export function onTick(event) {
    const { block, dimension } = event;
    const blockAbove = block.above();
    if (blockAbove.type.id === 'minecraft:air') {
        const victims = getRadiusEntity(dimension, blockAbove, 1, ["player"]);
        for (let victim of victims) {
            victim.applyDamage(4);
        }
    }
}

const infested = {
    onTick: onTick
};

export default infested