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

const umvuthanaBlocks = [
    "minecraft:stripped_mangrove_log",
    "minecraft:stripped_mangrove_wood",
    "minecraft:stripped_oak_wood",
    "minecraft:acacia_slab",
    "minecraft:red_sandstone_slab",
    "minecraft:acacia_stairs",
    "minecraft:red_sandstone_stairs",
    "minecraft:magma",
    "hf_mzs:clawed_log",
    "minecraft:mangrove_fence",
    "minecraft:iron_bars",
    "minecraft:skull",
    "minecraft:chest"
]
function isUmvuthanaBlock(block) {
    for (let i = 0; i < umvuthanaBlocks.length; i++) {
        //mc.world.sendMessage(`${umvuthanaBlocks[i]}   ${block}`);
        if (umvuthanaBlocks[i] === block)
            return true;
    }
    return false;
}
export function onBreakBlockUmvuthana(event) {
    //mc.world.sendMessage("Block broken");
    if (isUmvuthanaBlock(event.block.typeId))
        mc.system.run(() => {
            markUmvuthanaTarget(event.player);
        });
    return;
}
export function onPlaceBlockUmvuthana(event) {
    //mc.world.sendMessage("Block placed");
    const villages = event.player.dimension.getEntities({
        maxDistance: 32,
        families: ["hf_mzs_structure_umvuthana_village"],
        location: event.player.location
    });
    if (villages[0] === undefined)
        return;
    markUmvuthanaTarget(event.player);
}
export function onHitBlockUmvuthana(event) {
    if (event.hitBlock.typeId === "minecraft:frame")
        markUmvuthanaTarget(event.damagingEntity);
    return;
}

export function markUmvuthanaTarget(player) {
    if (player.typeId.includes("umvuthana"))
        return;
    
    if (player.getGameMode() === "creative")
        return;
    player.addTag("hf_mzs_targetted_by_umvuthana");
    player.setDynamicProperty("hf_mzs:umuthana_target_timer", 60);
}
export function umvuthanaTargetTimePass(player) {
    const time = player.getDynamicProperty("hf_mzs:umuthana_target_timer");
    if (time === undefined)
        return;
    if (time > 0) {
        player.setDynamicProperty("hf_mzs:umuthana_target_timer", time - 1);
    }
    if (time === 0) {
        player.removeTag("hf_mzs_targetted_by_umvuthana");
    }
}