import * as mc from "@minecraft/server";
import * as effects from "../effects";
import * as selectors from "../utils/selectors";
import * as vector from "../utils/vector";

export function tick1(entity) {
    if (entity.hasTag("hf_mzs_hit"))
        return;
    selectors.union({
        location: {
            x: entity.location.x,
            y: entity.location.y,
            z: entity.location.z
        },
        maxDistance: 1.5,
    }, ["player", "mob"], entity.dimension, entity.id)
        .forEach(element => {
            if (element.isValid())
                element.applyDamage(3);
                effects.applyFreezeEffect(element, 60);
        });

    let dir;
    if (entity.getDynamicProperty("hf_mzs:x_dir") !== undefined)
        dir = {
            x: entity.getDynamicProperty("hf_mzs:x_dir"),
            y: entity.getDynamicProperty("hf_mzs:y_dir"),
            z: entity.getDynamicProperty("hf_mzs:z_dir")
        }
    else
        dir = vector.down;
    const block = entity.dimension.getBlockFromRay(entity.location, dir, {
        maxDistance: 1.4,
        includeLiquidBlocks: false,
        includePassableBlocks: false,
    });
    entity.teleport(vector.add(entity.location, vector.multiply(dir, 1.4)));
    if (block !== undefined) {
        entity.addTag("hf_mzs_hit");
        entity.runCommand("/tp @s ~~-4~");
    }
}