import { Direction, EquipmentSlot, ItemStack, system, world } from "@minecraft/server";
import { getPlayerEquipment } from "../utils/my_API";
export var BPDynamicProperty;
(function (BPDynamicProperty) {
    // Portal properties
    BPDynamicProperty["DualityPortalId"] = "blocky_portal:dualityPortalId";
    BPDynamicProperty["lastPortalUsed"] = "blocky_portal:lastPortalUsed";
})(BPDynamicProperty || (BPDynamicProperty = {}));
export var BPStaticProperty;
(function (BPStaticProperty) {
    BPStaticProperty["isLinked"] = "blocky_portal:is_linked";
    BPStaticProperty["rotation"] = "blocky_portal:rotation";
    BPStaticProperty["orientation"] = "blocky_portal:orientation";
})(BPStaticProperty || (BPStaticProperty = {}));
const portalToColorConversionTable = {
    "blocky_portal:red_portal": "red",
    "blocky_portal:green_portal": "green",
    "blocky_portal:orange_portal": "orange",
    "blocky_portal:blue_portal": "blue",
    "blocky_portal:yellow_portal": "yellow",
    "blocky_portal:purple_portal": "purple",
    "blocky_portal:white_portal": "white",
    "blocky_portal:black_portal": "black",
    "blocky_portal:pink_portal": "pink",
    "blocky_portal:cyan_portal": "cyan"
};
const portalBlasterToColorConversionTable = {
    "blocky_portal:red_portal_blaster": "red",
    "blocky_portal:green_portal_blaster": "green",
    "blocky_portal:orange_portal_blaster": "orange",
    "blocky_portal:blue_portal_blaster": "blue",
    "blocky_portal:yellow_portal_blaster": "yellow",
    "blocky_portal:purple_portal_blaster": "purple",
    "blocky_portal:white_portal_blaster": "white",
    "blocky_portal:black_portal_blaster": "black",
    "blocky_portal:pink_portal_blaster": "pink",
    "blocky_portal:cyan_portal_blaster": "cyan"
};
const portalPairs = {
    "blocky_portal:red_portal": "blocky_portal:green_portal",
    "blocky_portal:green_portal": "blocky_portal:red_portal",
    "blocky_portal:orange_portal": "blocky_portal:blue_portal",
    "blocky_portal:blue_portal": "blocky_portal:orange_portal",
    "blocky_portal:yellow_portal": "blocky_portal:purple_portal",
    "blocky_portal:purple_portal": "blocky_portal:yellow_portal",
    "blocky_portal:white_portal": "blocky_portal:black_portal",
    "blocky_portal:black_portal": "blocky_portal:white_portal",
    "blocky_portal:pink_portal": "blocky_portal:cyan_portal",
    "blocky_portal:cyan_portal": "blocky_portal:pink_portal"
};
export const portalBlasterIds = [
    "blocky_portal:red_portal_blaster",
    "blocky_portal:green_portal_blaster",
    "blocky_portal:yellow_portal_blaster",
    "blocky_portal:white_portal_blaster",
    "blocky_portal:black_portal_blaster",
    "blocky_portal:purple_portal_blaster",
    "blocky_portal:orange_portal_blaster",
    "blocky_portal:pink_portal_blaster",
    "blocky_portal:cyan_portal_blaster",
    "blocky_portal:blue_portal_blaster"
];
function handlePortalBlasterUse(event) {
    if (!portalBlasterIds.includes(event.itemStack.typeId)) {
        return;
    }
    const player = event.source;
    if (player.isSneaking) {
        switchPortalBlaster(event);
    }
    else {
        playPortalBlasterSound(event.source);
        firePortal(event);
        //firePortalProjectile(event);
    }
}
function playPortalBlasterSound(player) {
    system.run(() => {
        player.playSound('blocky.portal.blaster.shoot', { location: player.location });
    });
}
function switchPortalBlaster(event) {
    let itemStack = event.itemStack;
    let player = event.source;
    let currentBlasterId = itemStack.typeId;
    let nextBlasterId = getNextPortalBlasterId(currentBlasterId);
    system.run(() => {
        getPlayerEquipment(player).setEquipment(EquipmentSlot.Mainhand, new ItemStack(nextBlasterId));
    });
}
function getNextPortalBlasterId(currentBlasterId) {
    const blasterPairs = {
        "blocky_portal:red_portal_blaster": "blocky_portal:green_portal_blaster",
        "blocky_portal:green_portal_blaster": "blocky_portal:red_portal_blaster",
        "blocky_portal:orange_portal_blaster": "blocky_portal:blue_portal_blaster",
        "blocky_portal:blue_portal_blaster": "blocky_portal:orange_portal_blaster",
        "blocky_portal:yellow_portal_blaster": "blocky_portal:purple_portal_blaster",
        "blocky_portal:purple_portal_blaster": "blocky_portal:yellow_portal_blaster",
        "blocky_portal:white_portal_blaster": "blocky_portal:black_portal_blaster",
        "blocky_portal:black_portal_blaster": "blocky_portal:white_portal_blaster",
        "blocky_portal:pink_portal_blaster": "blocky_portal:cyan_portal_blaster",
        "blocky_portal:cyan_portal_blaster": "blocky_portal:pink_portal_blaster"
    };
    return blasterPairs[currentBlasterId] || currentBlasterId;
}
function firePortal(event) {
    let itemStack = event.itemStack;
    let player = event.source;
    //
    let blockFromView = player.getBlockFromViewDirection();
    if (blockFromView === undefined) {
        return;
    }
    //check that the block can accept a portal
    let block = blockFromView.block;
    const validPlacementBlocks = [
        "minecraft:stone",
        "minecraft:granite",
        "minecraft:polished_granite",
        "minecraft:diorite",
        "minecraft:polished_diorite",
        "minecraft:andesite",
        "minecraft:polished_andesite",
        "minecraft:deepslate",
        "minecraft:cobbled_deepslate",
        "minecraft:polished_deepslate",
        "minecraft:calcite",
        "minecraft:tuff",
        "minecraft:dripstone_block",
        "minecraft:grass_block",
        "minecraft:dirt",
        "minecraft:coarse_dirt",
        "minecraft:podzol",
        "minecraft:dirt_with_roots",
        "minecraft:mud",
        "minecraft:crimson_nylium",
        "minecraft:warped_nylium",
        "minecraft:cobblestone",
        "minecraft:oak_planks",
        "minecraft:spruce_planks",
        "minecraft:birch_planks",
        "minecraft:jungle_planks",
        "minecraft:acacia_planks",
        "minecraft:dark_oak_planks",
        "minecraft:mangrove_planks",
        "minecraft:crimson_planks",
        "minecraft:warped_planks",
        "minecraft:sand",
        "minecraft:red_sand",
        "minecraft:gravel",
        "minecraft:coal_ore",
        "minecraft:deepslate_coal_ore",
        "minecraft:iron_ore",
        "minecraft:deepslate_iron_ore",
        "minecraft:copper_ore",
        "minecraft:deepslate_copper_ore",
        "minecraft:gold_ore",
        "minecraft:deepslate_gold_ore",
        "minecraft:redstone_ore",
        "minecraft:deepslate_redstone_ore",
        "minecraft:emerald_ore",
        "minecraft:deepslate_emerald_ore",
        "minecraft:lapis_ore",
        "minecraft:deepslate_lapis_ore",
        "minecraft:diamond_ore",
        "minecraft:deepslate_diamond_ore",
        "minecraft:nether_gold_ore",
        "minecraft:quartz_ore",
        "minecraft:ancient_debris",
        "minecraft:coal_block",
        "minecraft:raw_iron_block",
        "minecraft:raw_copper_block",
        "minecraft:raw_gold_block",
        "minecraft:amethyst_block",
        "minecraft:iron_block",
        "minecraft:copper_block",
        "minecraft:gold_block",
        "minecraft:diamond_block",
        "minecraft:netherite_block",
        "minecraft:exposed_copper",
        "minecraft:weathered_copper",
        "minecraft:oxidized_copper",
        "minecraft:cut_copper",
        "minecraft:exposed_cut_copper",
        "minecraft:weathered_cut_copper",
        "minecraft:oxidized_cut_copper",
        "minecraft:waxed_copper",
        "minecraft:waxed_exposed_copper",
        "minecraft:waxed_weathered_copper",
        "minecraft:waxed_oxidized_copper",
        "minecraft:waxed_cut_copper",
        "minecraft:waxed_exposed_cut_copper",
        "minecraft:waxed_weathered_cut_copper",
        "minecraft:waxed_oxidized_cut_copper",
        "minecraft:oak_log",
        "minecraft:spruce_log",
        "minecraft:birch_log",
        "minecraft:jungle_log",
        "minecraft:acacia_log",
        "minecraft:dark_oak_log",
        "minecraft:mangrove_log",
        "minecraft:crimson_stem",
        "minecraft:warped_stem",
        "minecraft:stripped_oak_log",
        "minecraft:stripped_spruce_log",
        "minecraft:stripped_birch_log",
        "minecraft:stripped_jungle_log",
        "minecraft:stripped_acacia_log",
        "minecraft:stripped_dark_oak_log",
        "minecraft:stripped_mangrove_log",
        "minecraft:stripped_crimson_stem",
        "minecraft:stripped_warped_stem",
        "minecraft:stripped_oak_wood",
        "minecraft:stripped_spruce_wood",
        "minecraft:stripped_birch_wood",
        "minecraft:stripped_jungle_wood",
        "minecraft:stripped_acacia_wood",
        "minecraft:stripped_dark_oak_wood",
        "minecraft:stripped_mangrove_wood",
        "minecraft:stripped_crimson_hyphae",
        "minecraft:stripped_warped_hyphae",
        "minecraft:oak_wood",
        "minecraft:spruce_wood",
        "minecraft:birch_wood",
        "minecraft:jungle_wood",
        "minecraft:acacia_wood",
        "minecraft:dark_oak_wood",
        "minecraft:mangrove_wood",
        "minecraft:crimson_hyphae",
        "minecraft:warped_hyphae",
        "minecraft:sponge",
        "minecraft:lapis_block",
        "minecraft:sandstone",
        "minecraft:chiseled_sandstone",
        "minecraft:cut_sandstone",
        "minecraft:grass",
        "minecraft:white_wool",
        "minecraft:orange_wool",
        "minecraft:magenta_wool",
        "minecraft:light_blue_wool",
        "minecraft:yellow_wool",
        "minecraft:lime_wool",
        "minecraft:pink_wool",
        "minecraft:gray_wool",
        "minecraft:light_gray_wool",
        "minecraft:cyan_wool",
        "minecraft:purple_wool",
        "minecraft:blue_wool",
        "minecraft:brown_wool",
        "minecraft:green_wool",
        "minecraft:red_wool",
        "minecraft:black_wool",
        "minecraft:moss_block",
        "minecraft:smooth_quartz",
        "minecraft:smooth_red_sandstone",
        "minecraft:smooth_sandstone",
        "minecraft:smooth_stone",
        "minecraft:brick_block",
        "minecraft:bookshelf",
        "minecraft:mossy_cobblestone",
        "minecraft:obsidian",
        "minecraft:purpur_block",
        "minecraft:crafting_table",
        "minecraft:furnace",
        "minecraft:snow",
        "minecraft:clay",
        "minecraft:jukebox",
        "minecraft:pumpkin",
        "minecraft:carved_pumpkin",
        "minecraft:lit_pumpkin",
        "minecraft:netherrack",
        "minecraft:soul_sand",
        "minecraft:soul_soil",
        "minecraft:basalt",
        "minecraft:polished_basalt",
        "minecraft:smooth_basalt",
        "minecraft:glowstone",
        "minecraft:stone_bricks",
        "minecraft:mossy_stone_bricks",
        "minecraft:cracked_stone_bricks",
        "minecraft:chiseled_stone_bricks",
        "minecraft:packed_mud",
        "minecraft:mud_bricks",
        "minecraft:deepslate_bricks",
        "minecraft:cracked_deepslate_bricks",
        "minecraft:deepslate_tiles",
        "minecraft:cracked_deepslate_tiles",
        "minecraft:chiseled_deepslate",
        "minecraft:brown_mushroom_block",
        "minecraft:red_mushroom_block",
        "minecraft:melon_block",
        "minecraft:mycelium",
        "minecraft:nether_brick",
        "minecraft:cracked_nether_bricks",
        "minecraft:chiseled_nether_bricks",
        "minecraft:sculk",
        "minecraft:end_stone",
        "minecraft:end_bricks",
        "minecraft:emerald_block",
        "minecraft:chiseled_quartz_block",
        "minecraft:quartz_block",
        "minecraft:quartz_bricks",
        "minecraft:quartz_pillar",
        "minecraft:white_terracotta",
        "minecraft:orange_terracotta",
        "minecraft:magenta_terracotta",
        "minecraft:light_blue_terracotta",
        "minecraft:yellow_terracotta",
        "minecraft:lime_terracotta",
        "minecraft:pink_terracotta",
        "minecraft:gray_terracotta",
        "minecraft:light_gray_terracotta",
        "minecraft:cyan_terracotta",
        "minecraft:purple_terracotta",
        "minecraft:blue_terracotta",
        "minecraft:brown_terracotta",
        "minecraft:green_terracotta",
        "minecraft:red_terracotta",
        "minecraft:black_terracotta",
        "minecraft:hay_block",
        "minecraft:hardened_clay",
        "minecraft:packed_ice",
        "minecraft:prismarine",
        "minecraft:prismarine_bricks",
        "minecraft:dark_prismarine",
        "minecraft:sea_lantern",
        "minecraft:red_sandstone",
        "minecraft:chiseled_red_sandstone",
        "minecraft:cut_red_sandstone",
        "minecraft:magma",
        "minecraft:warped_wart_block",
        "minecraft:red_nether_brick",
        "minecraft:bone_block",
        "minecraft:white_glazed_terracotta",
        "minecraft:orange_glazed_terracotta",
        "minecraft:magenta_glazed_terracotta",
        "minecraft:light_blue_glazed_terracotta",
        "minecraft:yellow_glazed_terracotta",
        "minecraft:lime_glazed_terracotta",
        "minecraft:pink_glazed_terracotta",
        "minecraft:gray_glazed_terracotta",
        "minecraft:silver_glazed_terracotta",
        "minecraft:cyan_glazed_terracotta",
        "minecraft:purple_glazed_terracotta",
        "minecraft:blue_glazed_terracotta",
        "minecraft:brown_glazed_terracotta",
        "minecraft:green_glazed_terracotta",
        "minecraft:red_glazed_terracotta",
        "minecraft:black_glazed_terracotta",
        "minecraft:white_concrete",
        "minecraft:orange_concrete",
        "minecraft:magenta_concrete",
        "minecraft:light_blue_concrete",
        "minecraft:yellow_concrete",
        "minecraft:lime_concrete",
        "minecraft:pink_concrete",
        "minecraft:gray_concrete",
        "minecraft:light_gray_concrete",
        "minecraft:cyan_concrete",
        "minecraft:purple_concrete",
        "minecraft:blue_concrete",
        "minecraft:brown_concrete",
        "minecraft:green_concrete",
        "minecraft:red_concrete",
        "minecraft:black_concrete",
        "minecraft:white_concrete_powder",
        "minecraft:orange_concrete_powder",
        "minecraft:magenta_concrete_powder",
        "minecraft:light_blue_concrete_powder",
        "minecraft:yellow_concrete_powder",
        "minecraft:lime_concrete_powder",
        "minecraft:pink_concrete_powder",
        "minecraft:gray_concrete_powder",
        "minecraft:light_gray_concrete_powder",
        "minecraft:cyan_concrete_powder",
        "minecraft:purple_concrete_powder",
        "minecraft:blue_concrete_powder",
        "minecraft:brown_concrete_powder",
        "minecraft:green_concrete_powder",
        "minecraft:red_concrete_powder",
        "minecraft:black_concrete_powder",
        "minecraft:dead_tube_coral_block",
        "minecraft:dead_brain_coral_block",
        "minecraft:dead_bubble_coral_block",
        "minecraft:dead_fire_coral_block",
        "minecraft:dead_horn_coral_block",
        "minecraft:tube_coral_block",
        "minecraft:brain_coral_block",
        "minecraft:bubble_coral_block",
        "minecraft:fire_coral_block",
        "minecraft:horn_coral_block",
        "minecraft:blue_ice",
        "minecraft:redstone_block",
        "minecraft:piston",
        "minecraft:sticky_piston",
        "minecraft:observer",
        "minecraft:dispenser",
        "minecraft:dropper",
        "minecraft:target",
        "minecraft:tnt",
        "minecraft:redstone_lamp",
        "minecraft:noteblock",
        "minecraft:double_cut_copper_slab",
        "minecraft:acacia_double_slab",
        "minecraft:andesite_double_slab",
        "minecraft:bamboo_double_slab",
        "minecraft:bamboo_mosaic_double_slab",
        "minecraft:birch_double_slab",
        "minecraft:blackstone_double_slab",
        "minecraft:brick_double_slab",
        "minecraft:cherry_double_slab",
        "minecraft:cobbled_deepslate_double_slab",
        "minecraft:cobblestone_double_slab",
        "minecraft:crimson_double_slab",
        "minecraft:cut_red_sandstone_double_slab",
        "minecraft:cut_sandstone_double_slab",
        "minecraft:dark_oak_double_slab",
        "minecraft:dark_prismarine_double_slab",
        "minecraft:deepslate_brick_double_slab",
        "minecraft:deepslate_tile_double_slab",
        "minecraft:diorite_double_slab",
        "minecraft:end_stone_brick_double_slab",
        "minecraft:exposed_double_cut_copper_slab",
        "minecraft:granite_double_slab",
        "minecraft:jungle_double_slab",
        "minecraft:mangrove_double_slab",
        "minecraft:mossy_cobblestone_double_slab",
        "minecraft:mossy_stone_brick_double_slab",
        "minecraft:mud_brick_double_slab",
        "minecraft:nether_brick_double_slab",
        "minecraft:normal_stone_double_slab",
        "minecraft:oak_double_slab",
        "minecraft:oxidized_double_cut_copper_slab",
        "minecraft:petrified_oak_double_slab",
        "minecraft:polished_andesite_double_slab",
        "minecraft:polished_blackstone_brick_double_slab",
        "minecraft:polished_deepslate_double_slab",
        "minecraft:polished_diorite_double_slab",
        "minecraft:polished_granite_double_slab",
        "minecraft:polished_tuff_double_slab",
        "minecraft:prismarine_brick_double_slab",
        "minecraft:prismarine_double_slab",
        "minecraft:purpur_double_slab",
        "minecraft:quartz_double_slab",
        "minecraft:red_nether_brick_double_slab",
        "minecraft:red_sandstone_double_slab",
        "minecraft:sandstone_double_slab",
        "minecraft:smooth_quartz_double_slab",
        "minecraft:smooth_red_sandstone_double_slab",
        "minecraft:smooth_sandstone_double_slab",
        "minecraft:smooth_stone_double_slab",
        "minecraft:spruce_double_slab",
        "minecraft:stone_brick_double_slab",
        "minecraft:tuff_brick_double_slab",
        "minecraft:tuff_double_slab",
        "minecraft:warped_double_slab",
        "minecraft:waxed_double_cut_copper_slab",
        "minecraft:waxed_exposed_double_cut_copper_slab",
        "minecraft:waxed_oxidized_double_cut_copper_slab",
        "minecraft:waxed_weathered_double_cut_copper_slab",
        "minecraft:weathered_double_cut_copper_slab",
        "minecraft:double_cut_copper_slab",
        "minecraft:blackstone",
        "minecraft:bamboo_block",
        "minecraft:bamboo_mosaic",
        "minecraft:bamboo_planks",
        "minecraft:stripped_bamboo_block",
        "minecraft:cherry_log",
        "minecraft:cherry_planks",
        "minecraft:cherry_wood",
        "minecraft:stripped_cherry_log",
        "minecraft:stripped_cherry_wood",
        "minecraft:copper_bulb",
        "minecraft:exposed_copper_bulb",
        "minecraft:oxidized_copper_bulb",
        "minecraft:waxed_copper_bulb",
        "minecraft:waxed_exposed_copper_bulb",
        "minecraft:waxed_oxidized_copper_bulb",
        "minecraft:waxed_weathered_copper_bulb",
        "minecraft:weathered_copper_bulb",
        "minecraft:chiseled_copper",
        "minecraft:exposed_chiseled_copper",
        "minecraft:oxidized_chiseled_copper",
        "minecraft:waxed_chiseled_copper",
        "minecraft:waxed_exposed_chiseled_copper",
        "minecraft:waxed_oxidized_chiseled_copper",
        "minecraft:waxed_weathered_chiseled_copper",
        "minecraft:weathered_chiseled_copper",
        "minecraft:tuff_bricks",
        "minecraft:chiseled_tuff",
        "minecraft:chiseled_tuff_bricks",
        "minecraft:polished_tuff",
        "minecraft:polished_blackstone_bricks",
        "minecraft:cracked_polished_blackstone_bricks"
    ];
    if (!block?.isValid() || !validPlacementBlocks.includes(block.typeId)) {
        // Send a message to the player
        player.sendMessage("You cannot place a portal on this block!");
        return;
    }
    //calculate the orientation and rotation of the portal
    let rotation = 0;
    let orientation = 0;
    let location = undefined;
    let face = blockFromView.face;
    switch (face) {
        case Direction.Up: {
            location = block.above(1).center();
            location.y -= 0.5;
            orientation = 2;
            let ry = player.getRotation().y + 180;
            if ((ry >= 0 && ry <= 45) || (ry >= 315 && ry < 360)) {
                rotation = 0;
            }
            else if (ry > 45 && ry <= 135) {
                rotation = 1;
            }
            else if (ry > 135 && ry <= 225) {
                rotation = 2;
            }
            else {
                rotation = 3;
            }
            break;
        }
        case Direction.Down: {
            location = block.below(1).center();
            location.y -= 0.5;
            orientation = 1;
            let ry = player.getRotation().y + 180;
            if ((ry >= 0 && ry <= 45) || (ry >= 315 && ry < 360)) {
                rotation = 0;
            }
            else if (ry > 45 && ry <= 135) {
                rotation = 1;
            }
            else if (ry > 135 && ry <= 225) {
                rotation = 2;
            }
            else {
                rotation = 3;
            }
            break;
        }
        case Direction.North: {
            location = block.north(1).center();
            location.y -= 0.5;
            orientation = 0;
            rotation = 2;
            break;
        }
        case Direction.West: {
            location = block.west(1).center();
            location.y -= 0.5;
            orientation = 0;
            rotation = 1;
            break;
        }
        case Direction.South: {
            location = block.south(1).center();
            location.y -= 0.5;
            orientation = 0;
            rotation = 0;
            break;
        }
        case Direction.East: {
            location = block.east(1).center();
            location.y -= 0.5;
            orientation = 0;
            rotation = 3;
            break;
        }
    }
    //appearance of the portal
    let portal_color = portalBlasterToColorConversionTable[itemStack.typeId];
    let portalIdentifier = `blocky_portal:${portal_color}_portal`;
    system.run(() => {
        //summon
        let portal = player.dimension.spawnEntity(portalIdentifier, location);
        //rotation
        portal.setProperty(BPStaticProperty.rotation, rotation);
        //orientation
        portal.setProperty(BPStaticProperty.orientation, orientation);
        //
        const portal_property = `blocky_portal:${portal_color}PortalId`;
        let last_unique_id = player.getDynamicProperty(portal_property);
        if (last_unique_id != undefined) {
            let last_portal = world.getEntity(last_unique_id);
            if (last_portal !== undefined) {
                last_portal.remove();
            }
        }
        //
        const portalPairId = portalPairs[portalIdentifier];
        const dualityPortalColor = portalToColorConversionTable[portalPairId];
        const dualityPortalId = player.getDynamicProperty(`blocky_portal:${dualityPortalColor}PortalId`);
        portal.setDynamicProperty(BPDynamicProperty.DualityPortalId, dualityPortalId);
        //
        let unique_id = portal.id;
        player.setDynamicProperty(portal_property, unique_id);
        if(dualityPortalId != undefined){
            world.getEntity(dualityPortalId)?.setDynamicProperty(BPDynamicProperty.DualityPortalId, unique_id);
        }
    });
}
function firePortalProjectile(event) {
    let itemStack = event.itemStack;
    let player = event.source;
    //appearance of the projectile
    let projectile_color = portalBlasterToColorConversionTable[itemStack.typeId];
    let projectileIdentifier = `blocky_portal:${projectile_color}_portal_bullet`;
    system.run(() => {
        //summon
        let projectile = player.dimension.spawnEntity(projectileIdentifier, player.location);
        projectile.applyImpulse(player.getViewDirection());
    });
}
world.beforeEvents.itemUse.subscribe(event => {
    handlePortalBlasterUse(event);
});
world.afterEvents.entityHitEntity.subscribe(event => {
    removePortalOnHit(event);
});
function removePortalOnHit(event) {
    if (event.damagingEntity.typeId !== "minecraft:player") {
        return;
    }
    if (!event.hitEntity.matches({ families: ["blocky_portal:portal"] })) {
        return;
    }
    const player = event.damagingEntity;
    player.playSound('blocky_portal:portal_hit');
    const portal = event.hitEntity;
    let scoreboard = world.scoreboard.getObjective(portalDestroyTimeId);
    if (scoreboard === undefined) {
        scoreboard = world.scoreboard.addObjective(portalDestroyTimeId, portalDestroyTimeId);
    }
    if (scoreboard.hasParticipant(portal) == false) {
        scoreboard.setScore(portal, 0);
    }
    scoreboard.addScore(portal, 20);
    const score = scoreboard.getScore(portal);
    if (score >= 50) {
        portal.remove();
    }
}
const portalDestroyTimeId = 'blocky_portal:portalDestroyTime';
system.runInterval(() => {
    runPortalDestroyTime();
}, 1);
function runPortalDestroyTime() {
    let scoreboard = world.scoreboard.getObjective(portalDestroyTimeId);
    if (scoreboard === undefined) {
        scoreboard = world.scoreboard.addObjective(portalDestroyTimeId, portalDestroyTimeId);
    }
    scoreboard.getParticipants().forEach(participant => {
        let score = scoreboard.getScore(participant) || 0;
        if (score > 0) {
            score = score - 1;
            scoreboard.setScore(participant, score);
        }
        else {
            scoreboard.removeParticipant(participant);
        }
    });
}