import { world, system, BlockPermutation } from '@minecraft/server';
const entityTypeId = "sapphire_portality:portal_orange";
const entityTypeIdItem = "minecraft:item";
const list_dimention = ["overworld", "nether", "the_end"]
let orange_Portal_1 = [
    { x: undefined, y: undefined, z: undefined },
    { directionX: 0, directionZ: 0, horizontalStrength: 0, verticalStrength: 0 },
    { full: 0 },
    { x: undefined, y: undefined, z: undefined },
    { x: undefined, y: undefined, z: undefined },
    { x: undefined, y: undefined, z: undefined },
    "",
    { x: undefined, y: undefined, z: undefined },
    { x: 0, z: 0 }
];
let orange_Portal_2 = [
    { x: undefined, y: undefined, z: undefined },
    { directionX: 0, directionZ: 0, horizontalStrength: 0, verticalStrength: 0 },
    { full: 0 },
    { x: undefined, y: undefined, z: undefined },
    { x: undefined, y: undefined, z: undefined },
    { x: undefined, y: undefined, z: undefined },
    "",
    { x: undefined, y: undefined, z: undefined },
    { x: 0, z: 0 }
]
// console.warn("orange_2:", JSON.stringify(orange_Portal_2[0].x, null, 2));
// console.warn("entity.location:", JSON.stringify(entity.location, null, 2));
let entities;
let entity_close;
let orange_teleported = false;
let portal_1 = 0;
let portal_2 = 0;
let orange_item_teleported = false;
function Spawn() {
    if (!orange_teleported || !orange_item_teleported) {
        for (let index = 0; index < list_dimention.length; index++) {
            const var_dimension = list_dimention[index];
            entities = world.getDimension(var_dimension).getEntities({ type: entityTypeId });
            for (const aux of entities) {
                entity_close = aux.id;
            }
            if (orange_Portal_1[0].x != undefined && orange_Portal_2[0].x != undefined) {
                for (const entity of entities) {
                    DetectFullPortal(entity, var_dimension);
                    if (orange_Portal_1[2].full == 1 && orange_Portal_2[2].full == 1) {
                        entity.triggerEvent("animation_turn_on")
                    } else {
                        entity.triggerEvent("animation_turn_off")
                    }
                }
            } else {
                for (const entity of entities) {
                    if (DetectPLacementPortal(entity)) {
                        entity.triggerEvent("animation_edge_off")
                    }
                }
            }
            for (const entity of entities) {
                if (orange_Portal_1[0].x != undefined && orange_Portal_2[0].x != undefined && !areLocationsEqual(orange_Portal_1[0], entity.location) && !areLocationsEqual(orange_Portal_2[0], entity.location)) {
                    entity.triggerEvent("instant_despawn")
                    world.sendMessage("§lThe §6Portals§r§l are already connected.")
                    return
                } else {
                    if (entity.getComponent('skin_id').value != 1) {
                        if (orange_Portal_1[0].x === undefined) {
                            if (orange_Portal_2[0].x !== undefined) {
                                if (!areLocationsEqual(orange_Portal_2[0], entity.location)) {
                                    if (DetectPLacementPortal(entity)) {
                                        orange_Portal_1[0].x = entity.location.x;
                                        orange_Portal_1[0].y = entity.location.y;
                                        orange_Portal_1[0].z = entity.location.z;
                                        entity.triggerEvent("animation_edge_off");
                                        if (entity.getComponent('variant').value == 0) {
                                            entity.triggerEvent("structure_1");
                                        }
                                        portal_1 = 0;
                                    } else if (!DetectPLacementPortal(entity) && portal_1 == 0) {
                                        world.sendMessage("§lRemove the blocks inside the designated area!")
                                        portal_1++;
                                    }
                                    continue;
                                }
                            } else {
                                if (orange_Portal_2[0].x === undefined || areLocationsEqual(orange_Portal_2[0], entity.location)) {
                                    if (DetectPLacementPortal(entity)) {
                                        orange_Portal_1[0].x = entity.location.x;
                                        orange_Portal_1[0].y = entity.location.y;
                                        orange_Portal_1[0].z = entity.location.z;
                                        entity.triggerEvent("animation_edge_off");
                                        if (entity.getComponent('variant').value == 0) {
                                            entity.triggerEvent("structure_1");
                                        }
                                        portal_1 = 0;
                                    } else if (!DetectPLacementPortal(entity) && portal_1 == 0) {
                                        world.sendMessage("§lRemove the blocks inside the designated area!")
                                        portal_1++;
                                    }
                                    continue;
                                }
                            }
                        } else if (orange_Portal_2[0].x === undefined) {
                            if (orange_Portal_1[0].x !== undefined) {
                                if (!areLocationsEqual(orange_Portal_1[0], entity.location)) {
                                    if (DetectPLacementPortal(entity)) {
                                        orange_Portal_2[0].x = entity.location.x;
                                        orange_Portal_2[0].y = entity.location.y;
                                        orange_Portal_2[0].z = entity.location.z;
                                        entity.triggerEvent("animation_edge_off");
                                        if (entity.getComponent('variant').value == 0) {
                                            entity.triggerEvent("structure_1");
                                        }
                                        portal_2 = 0;
                                    } else if (!DetectPLacementPortal(entity) && portal_2 == 0) {
                                        world.sendMessage("§lRemove the blocks inside the designated area!")
                                        portal_2++;
                                    }
                                    continue;
                                }
                            } else {
                                if (orange_Portal_1[0].x === undefined || areLocationsEqual(orange_Portal_1[0], entity.location)) {
                                    if (DetectPLacementPortal(entity)) {
                                        orange_Portal_2[0].x = entity.location.x;
                                        orange_Portal_2[0].y = entity.location.y;
                                        orange_Portal_2[0].z = entity.location.z;
                                        entity.triggerEvent("animation_edge_off");
                                        if (entity.getComponent('variant').value == 0) {
                                            entity.triggerEvent("structure_1");
                                        }
                                        portal_2 = 0;
                                    } else if (!DetectPLacementPortal(entity) && portal_2 == 0) {
                                        world.sendMessage("§lRemove the blocks inside the designated area!")
                                        portal_2++;
                                    }
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /* for(const entity in entities){
         console.warn("orange_1",entity.location.x);
     }*/
    /*player_1.teleport(
        { x: entity.location.x, y: entity.location.y + 1, z: entity.location.z }
    );*/
}
function areLocationsEqual(loc1, loc2) {
    return loc1.x === loc2.x && loc1.y === loc2.y && loc1.z === loc2.z;
}
function orangeTeleportItems(item, entity) {

    if (orange_Portal_1[0].x !== undefined && orange_Portal_2[0].x !== undefined) {
        if (orange_Portal_1[0].x !== undefined) {
            if ((areLocationsEqual(orange_Portal_1[0], entity.location))) {
                if (orange_Portal_1[2].full == 1 && orange_Portal_2[2].full == 1) {
                    item.applyImpulse(orange_Portal_1[3])
                    system.runTimeout(() => {
                        item.teleport(
                            {
                                x: orange_Portal_2[7].x,
                                y: orange_Portal_2[7].y + 1,
                                z: orange_Portal_2[7].z
                            },
                            {
                                dimension: world.getDimension(orange_Portal_2[6]),
                            }
                        );
                    }, 9);
                }
            }
        }
        if (orange_Portal_2[0].x !== undefined) {
            if ((areLocationsEqual(orange_Portal_2[0], entity.location))) {
                if (orange_Portal_1[2].full == 1 && orange_Portal_2[2].full == 1) {
                    item.applyImpulse(orange_Portal_2[3])
                    system.runTimeout(() => {
                        item.teleport(
                            {
                                x: orange_Portal_1[7].x,
                                y: orange_Portal_1[7].y + 1,
                                z: orange_Portal_1[7].z
                            },
                            {
                                dimension: world.getDimension(orange_Portal_1[6]),
                            }
                        );
                    }, 9);
                }
            }
        }
    }
    //const entities = world.getDimension("overworld").getEntities({type: entityTypeId });
}
function Teleport() {
    for (let index = 0; index < list_dimention.length; index++) {
        const var_dimension = list_dimention[index];
        if (!orange_teleported) {
            for (let player of world.getDimension(var_dimension).getPlayers()) {
                const entities = world.getDimension(var_dimension).getEntities({ type: entityTypeId, location: player.location, closest: 2, maxDistance: 2 });
                for (let entity of entities) {
                    const { x, y, z } = player.location;
                    const block = player.dimension.getBlock({ x, y: y - 1, z });
                    if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier')) {
                        system.run(() => {
                            if (player != null || player != undefined || player != anonymous) {
                                orangeTeleport(player, entity);
                                return;
                            }
                        });
                    }
                }
            }
        }
        if (!orange_item_teleported) {
            const entities = world.getDimension(var_dimension).getEntities({ type: entityTypeId });
            for (const entity of entities) {
                const items = world.getDimension(var_dimension).getEntities({ type: entityTypeIdItem, location: entity.location, closest: 2, maxDistance: 2 });
                for (const item of items) {
                    const { x, y, z } = item.location;
                    const block = item.dimension.getBlock({ x, y: y - 1, z });
                    if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier')) {
                        system.run(() => {
                            orangeTeleportItems(item, entity);
                        });
                    }
                }
            }
        }
    }
}


function orangeTeleport(player, entity) {
    if (orange_Portal_1[0].x !== undefined && orange_Portal_2[0].x !== undefined) {
        if (orange_Portal_1[0].x !== undefined) {
            if ((areLocationsEqual(orange_Portal_1[0], entity.location))) {
                if (orange_Portal_1[2].full == 1 && orange_Portal_2[2].full == 1) {
                    orange_teleported = true;
                    player.runCommand("playsound sapphire_portality:teleport @s")
                    player.applyKnockback(orange_Portal_1[1].directionX, orange_Portal_1[1].directionZ, orange_Portal_1[1].horizontalStrength, orange_Portal_1[1].verticalStrength);
                    system.runTimeout(() => {
                        player.teleport(
                            {
                                x: orange_Portal_2[0].x + orange_Portal_2[8].x,
                                y: orange_Portal_2[0].y + 2.5,
                                z: orange_Portal_2[0].z + orange_Portal_2[8].z
                            },
                            {
                                dimension: world.getDimension(orange_Portal_2[6]),
                            }
                        );
                    }, 9);
                    if (orange_Portal_1[6] != orange_Portal_2[6]) {
                        world.afterEvents.playerDimensionChange.subscribe(data => {
                            if (orange_teleported) {
                                const run_1 = system.runInterval(() => {
                                    const valid = player.isValid();
                                    const { x, y, z } = player.location;
                                    const block = player.dimension.getBlock({ x, y: y - 1, z });
                                    if (valid && (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier'))) {
                                        player.runCommand("playsound sapphire_portality:teleport @s")
                                        player.applyKnockback(-orange_Portal_2[1].directionX, -orange_Portal_2[1].directionZ, orange_Portal_2[1].horizontalStrength, orange_Portal_2[1].verticalStrength);
                                        system.clearRun(run_1)
                                        system.runTimeout(() => {
                                            orange_teleported = false;
                                        }, 50);
                                        player.runCommand("scoreboard players set @a sapphire_portality_tick 0")
                                    }
                                }, 10);
                            }
                        })
                    } else {
                        const run_2 = system.runInterval(() => {
                            const valid = player.isValid();
                            const { x, y, z } = player.location;
                            const block = player.dimension.getBlock({ x, y: y - 1, z });
                            if (valid && (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier'))) {
                                player.runCommand("playsound sapphire_portality:teleport @s")
                                player.applyKnockback(-orange_Portal_2[1].directionX, -orange_Portal_2[1].directionZ, orange_Portal_2[1].horizontalStrength, orange_Portal_2[1].verticalStrength);
                                system.clearRun(run_2)
                                system.runTimeout(() => {
                                    orange_teleported = false;
                                }, 50);
                                player.runCommand("scoreboard players set @a sapphire_portality_tick 0")
                            }
                        }, 10);
                    }
                }
            }
        }
        if (orange_Portal_2[0].x !== undefined) {
            if ((areLocationsEqual(orange_Portal_2[0], entity.location))) {
                if (orange_Portal_1[2].full == 1 && orange_Portal_2[2].full == 1) {
                    orange_teleported = true;
                    player.runCommand("playsound sapphire_portality:teleport @s")
                    player.applyKnockback(orange_Portal_2[1].directionX, orange_Portal_2[1].directionZ, orange_Portal_2[1].horizontalStrength, orange_Portal_2[1].verticalStrength);
                    system.runTimeout(() => {
                        player.teleport(
                            {
                                x: orange_Portal_1[0].x + orange_Portal_1[8].x,
                                y: orange_Portal_1[0].y + 2.5,
                                z: orange_Portal_1[0].z + orange_Portal_1[8].z
                            },
                            {
                                dimension: world.getDimension(orange_Portal_1[6]),
                            }
                        );
                    }, 9);
                    if (orange_Portal_1[6] != orange_Portal_2[6]) {
                        world.afterEvents.playerDimensionChange.subscribe(data => {
                            if (orange_teleported) {
                                const run_3 = system.runInterval(() => {
                                    const valid = player.isValid();
                                    const { x, y, z } = player.location;
                                    const block = player.dimension.getBlock({ x, y: y - 1, z });
                                    if (valid && (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier'))) {
                                        player.runCommand("playsound sapphire_portality:teleport @s")
                                        player.applyKnockback(-orange_Portal_1[1].directionX, -orange_Portal_1[1].directionZ, orange_Portal_1[1].horizontalStrength, orange_Portal_1[1].verticalStrength);
                                        system.clearRun(run_3)
                                        system.runTimeout(() => {
                                            orange_teleported = false;
                                        }, 50);
                                        player.runCommand("scoreboard players set @a sapphire_portality_tick 0")
                                    }
                                }, 10);
                            }
                        })
                    } else {
                        const run_4 = system.runInterval(() => {
                            const valid = player.isValid();
                            const { x, y, z } = player.location;
                            const block = player.dimension.getBlock({ x, y: y - 1, z });
                            if (valid && (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:barrier'))) {
                                player.runCommand("playsound sapphire_portality:teleport @s")
                                player.applyKnockback(-orange_Portal_1[1].directionX, -orange_Portal_1[1].directionZ, orange_Portal_1[1].horizontalStrength, orange_Portal_1[1].verticalStrength);
                                system.clearRun(run_4)
                                system.runTimeout(() => {
                                    orange_teleported = false;
                                }, 50);
                                player.runCommand("scoreboard players set @a sapphire_portality_tick 0")
                            }
                        }, 10);
                    }
                }
            }
        }
    }
    //const entities = world.getDimension("overworld").getEntities({type: entityTypeId });
}
function DetectPLacementPortal(entity) {
    let fullorangePortal = 0;
    const { x, y, z } = entity.location;
    const front = [
        { x: x + 1, y: y, z: z },
        { x: x + 2, y: y, z: z },
        { x: x + 2, y: y + 1, z: z },
        { x: x + 2, y: y + 2, z: z },
        { x: x + 2, y: y + 3, z: z },
        { x: x + 2, y: y + 4, z: z },
        { x: x + 1, y: y + 4, z: z },
        { x: x, y: y + 4, z: z },
        { x: x - 1, y: y + 4, z: z },
        { x: x - 2, y: y + 4, z: z },
        { x: x - 2, y: y + 3, z: z },
        { x: x - 2, y: y + 2, z: z },
        { x: x - 2, y: y + 1, z: z },
        { x: x - 2, y: y, z: z },
        { x: x - 1, y: y, z: z },
        { x: x + 1, y: y, z: z + 1 },
        { x: x + 2, y: y, z: z + 1 },
        { x: x + 2, y: y + 1, z: z + 1 },
        { x: x + 2, y: y + 2, z: z + 1 },
        { x: x + 2, y: y + 3, z: z + 1 },
        { x: x + 2, y: y + 4, z: z + 1 },
        { x: x + 1, y: y + 4, z: z + 1 },
        { x: x, y: y + 4, z: z + 1 },
        { x: x - 1, y: y + 4, z: z + 1 },
        { x: x - 2, y: y + 4, z: z + 1 },
        { x: x - 2, y: y + 3, z: z + 1 },
        { x: x - 2, y: y + 2, z: z + 1 },
        { x: x - 2, y: y + 1, z: z + 1 },
        { x: x - 2, y: y, z: z + 1 },
        { x: x - 1, y: y, z: z + 1 },
        { x: x, y: y, z: z + 1 },
        { x: x + 1, y: y, z: z + 2 },
        { x: x + 2, y: y, z: z + 2 },
        { x: x + 2, y: y + 1, z: z + 2 },
        { x: x + 2, y: y + 2, z: z + 2 },
        { x: x + 2, y: y + 3, z: z + 2 },
        { x: x + 2, y: y + 4, z: z + 2 },
        { x: x + 1, y: y + 4, z: z + 2 },
        { x: x, y: y + 4, z: z + 2 },
        { x: x - 1, y: y + 4, z: z + 2 },
        { x: x - 2, y: y + 4, z: z + 2 },
        { x: x - 2, y: y + 3, z: z + 2 },
        { x: x - 2, y: y + 2, z: z + 2 },
        { x: x - 2, y: y + 1, z: z + 2 },
        { x: x - 2, y: y, z: z + 2 },
        { x: x - 1, y: y, z: z + 2 },
        { x: x, y: y, z: z + 2 },
        { x: x, y: y + 1, z: z },
        { x: x, y: y + 1, z: z + 1 },
        { x: x, y: y + 1, z: z + 2 },
        { x: x + 1, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z + 1 },
        { x: x - 1, y: y + 1, z: z + 1 },
        { x: x + 1, y: y + 1, z: z + 2 },
        { x: x - 1, y: y + 1, z: z + 2 },
        { x: x, y: y + 2, z: z },
        { x: x, y: y + 2, z: z + 1 },
        { x: x, y: y + 2, z: z + 2 },
        { x: x + 1, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z + 1 },
        { x: x - 1, y: y + 2, z: z + 1 },
        { x: x + 1, y: y + 2, z: z + 2 },
        { x: x - 1, y: y + 2, z: z + 2 },
        { x: x, y: y + 3, z: z },
        { x: x, y: y + 3, z: z + 1 },
        { x: x, y: y + 3, z: z + 2 },
        { x: x + 1, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z + 1 },
        { x: x - 1, y: y + 3, z: z + 1 },
        { x: x + 1, y: y + 3, z: z + 2 },
        { x: x - 1, y: y + 3, z: z + 2 },
    ];
    const back = [
        { x: x + 1, y: y, z: z },
        { x: x + 2, y: y, z: z },
        { x: x + 2, y: y + 1, z: z },
        { x: x + 2, y: y + 2, z: z },
        { x: x + 2, y: y + 3, z: z },
        { x: x + 2, y: y + 4, z: z },
        { x: x + 1, y: y + 4, z: z },
        { x: x, y: y + 4, z: z },
        { x: x - 1, y: y + 4, z: z },
        { x: x - 2, y: y + 4, z: z },
        { x: x - 2, y: y + 3, z: z },
        { x: x - 2, y: y + 2, z: z },
        { x: x - 2, y: y + 1, z: z },
        { x: x - 2, y: y, z: z },
        { x: x - 1, y: y, z: z },
        { x: x + 1, y: y, z: z - 1 },
        { x: x + 2, y: y, z: z - 1 },
        { x: x + 2, y: y + 1, z: z - 1 },
        { x: x + 2, y: y + 2, z: z - 1 },
        { x: x + 2, y: y + 3, z: z - 1 },
        { x: x + 2, y: y + 4, z: z - 1 },
        { x: x + 1, y: y + 4, z: z - 1 },
        { x: x, y: y + 4, z: z - 1 },
        { x: x - 1, y: y + 4, z: z - 1 },
        { x: x - 2, y: y + 4, z: z - 1 },
        { x: x - 2, y: y + 3, z: z - 1 },
        { x: x - 2, y: y + 2, z: z - 1 },
        { x: x - 2, y: y + 1, z: z - 1 },
        { x: x - 2, y: y, z: z - 1 },
        { x: x - 1, y: y, z: z - 1 },
        { x: x, y: y, z: z - 1 },
        { x: x + 1, y: y, z: z - 2 },
        { x: x + 2, y: y, z: z - 2 },
        { x: x + 2, y: y + 1, z: z - 2 },
        { x: x + 2, y: y + 2, z: z - 2 },
        { x: x + 2, y: y + 3, z: z - 2 },
        { x: x + 2, y: y + 4, z: z - 2 },
        { x: x + 1, y: y + 4, z: z - 2 },
        { x: x, y: y + 4, z: z - 2 },
        { x: x - 1, y: y + 4, z: z - 2 },
        { x: x - 2, y: y + 4, z: z - 2 },
        { x: x - 2, y: y + 3, z: z - 2 },
        { x: x - 2, y: y + 2, z: z - 2 },
        { x: x - 2, y: y + 1, z: z - 2 },
        { x: x - 2, y: y, z: z - 2 },
        { x: x - 1, y: y, z: z - 2 },
        { x: x, y: y, z: z - 2 },
        { x: x, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z - 2 },
        { x: x + 1, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z - 1 },
        { x: x - 1, y: y + 1, z: z - 1 },
        { x: x + 1, y: y + 1, z: z - 2 },
        { x: x - 1, y: y + 1, z: z - 2 },
        { x: x, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z - 2 },
        { x: x + 1, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z - 1 },
        { x: x - 1, y: y + 2, z: z - 1 },
        { x: x + 1, y: y + 2, z: z - 2 },
        { x: x - 1, y: y + 2, z: z - 2 },
        { x: x, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z - 2 },
        { x: x + 1, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z - 1 },
        { x: x - 1, y: y + 3, z: z - 1 },
        { x: x + 1, y: y + 3, z: z - 2 },
        { x: x - 1, y: y + 3, z: z - 2 },
    ];
    const left = [
        { x: x, y: y, z: z + 1 },
        { x: x, y: y, z: z + 2 },
        { x: x, y: y + 1, z: z + 2 },
        { x: x, y: y + 2, z: z + 2 },
        { x: x, y: y + 3, z: z + 2 },
        { x: x, y: y + 4, z: z + 2 },
        { x: x, y: y + 4, z: z + 1 },
        { x: x, y: y + 4, z: z },
        { x: x, y: y + 4, z: z - 1 },
        { x: x, y: y + 4, z: z - 2 },
        { x: x, y: y + 3, z: z - 2 },
        { x: x, y: y + 2, z: z - 2 },
        { x: x, y: y + 1, z: z - 2 },
        { x: x, y: y, z: z - 2 },
        { x: x, y: y, z: z - 1 },
        { x: x + 1, y: y, z: z + 1 },
        { x: x + 1, y: y, z: z + 2 },
        { x: x + 1, y: y + 1, z: z + 2 },
        { x: x + 1, y: y + 2, z: z + 2 },
        { x: x + 1, y: y + 3, z: z + 2 },
        { x: x + 1, y: y + 4, z: z + 2 },
        { x: x + 1, y: y + 4, z: z + 1 },
        { x: x + 1, y: y + 4, z: z },
        { x: x + 1, y: y + 4, z: z - 1 },
        { x: x + 1, y: y + 4, z: z - 2 },
        { x: x + 1, y: y + 3, z: z - 2 },
        { x: x + 1, y: y + 2, z: z - 2 },
        { x: x + 1, y: y + 1, z: z - 2 },
        { x: x + 1, y: y, z: z - 2 },
        { x: x + 1, y: y, z: z - 1 },
        { x: x + 1, y: y, z: z },
        { x: x + 2, y: y, z: z + 1 },
        { x: x + 2, y: y, z: z + 2 },
        { x: x + 2, y: y + 1, z: z + 2 },
        { x: x + 2, y: y + 2, z: z + 2 },
        { x: x + 2, y: y + 3, z: z + 2 },
        { x: x + 2, y: y + 4, z: z + 2 },
        { x: x + 2, y: y + 4, z: z + 1 },
        { x: x + 2, y: y + 4, z: z },
        { x: x + 2, y: y + 4, z: z - 1 },
        { x: x + 2, y: y + 4, z: z - 2 },
        { x: x + 2, y: y + 3, z: z - 2 },
        { x: x + 2, y: y + 2, z: z - 2 },
        { x: x + 2, y: y + 1, z: z - 2 },
        { x: x + 2, y: y, z: z - 2 },
        { x: x + 2, y: y, z: z - 1 },
        { x: x + 2, y: y, z: z },
        { x: x, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z },
        { x: x + 2, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z + 1 },
        { x: x + 1, y: y + 1, z: z - 1 },
        { x: x + 1, y: y + 1, z: z + 1 },
        { x: x + 2, y: y + 1, z: z - 1 },
        { x: x + 2, y: y + 1, z: z + 1 },
        { x: x, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z },
        { x: x + 2, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z + 1 },
        { x: x + 1, y: y + 2, z: z - 1 },
        { x: x + 1, y: y + 2, z: z + 1 },
        { x: x + 2, y: y + 2, z: z - 1 },
        { x: x + 2, y: y + 2, z: z + 1 },
        { x: x, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z },
        { x: x + 2, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z + 1 },
        { x: x + 1, y: y + 3, z: z - 1 },
        { x: x + 1, y: y + 3, z: z + 1 },
        { x: x + 2, y: y + 3, z: z - 1 },
        { x: x + 2, y: y + 3, z: z + 1 },
    ]
    const right = [
        { x: x, y: y, z: z + 1 },
        { x: x, y: y, z: z + 2 },
        { x: x, y: y + 1, z: z + 2 },
        { x: x, y: y + 2, z: z + 2 },
        { x: x, y: y + 3, z: z + 2 },
        { x: x, y: y + 4, z: z + 2 },
        { x: x, y: y + 4, z: z + 1 },
        { x: x, y: y + 4, z: z },
        { x: x, y: y + 4, z: z - 1 },
        { x: x, y: y + 4, z: z - 2 },
        { x: x, y: y + 3, z: z - 2 },
        { x: x, y: y + 2, z: z - 2 },
        { x: x, y: y + 1, z: z - 2 },
        { x: x, y: y, z: z - 2 },
        { x: x, y: y, z: z - 1 },
        { x: x - 1, y: y, z: z + 1 },
        { x: x - 1, y: y, z: z + 2 },
        { x: x - 1, y: y + 1, z: z + 2 },
        { x: x - 1, y: y + 2, z: z + 2 },
        { x: x - 1, y: y + 3, z: z + 2 },
        { x: x - 1, y: y + 4, z: z + 2 },
        { x: x - 1, y: y + 4, z: z + 1 },
        { x: x - 1, y: y + 4, z: z },
        { x: x - 1, y: y + 4, z: z - 1 },
        { x: x - 1, y: y + 4, z: z - 2 },
        { x: x - 1, y: y + 3, z: z - 2 },
        { x: x - 1, y: y + 2, z: z - 2 },
        { x: x - 1, y: y + 1, z: z - 2 },
        { x: x - 1, y: y, z: z - 2 },
        { x: x - 1, y: y, z: z - 1 },
        { x: x - 1, y: y, z: z },
        { x: x - 2, y: y, z: z + 1 },
        { x: x - 2, y: y, z: z + 2 },
        { x: x - 2, y: y + 1, z: z + 2 },
        { x: x - 2, y: y + 2, z: z + 2 },
        { x: x - 2, y: y + 3, z: z + 2 },
        { x: x - 2, y: y + 4, z: z + 2 },
        { x: x - 2, y: y + 4, z: z + 1 },
        { x: x - 2, y: y + 4, z: z },
        { x: x - 2, y: y + 4, z: z - 1 },
        { x: x - 2, y: y + 4, z: z - 2 },
        { x: x - 2, y: y + 3, z: z - 2 },
        { x: x - 2, y: y + 2, z: z - 2 },
        { x: x - 2, y: y + 1, z: z - 2 },
        { x: x - 2, y: y, z: z - 2 },
        { x: x - 2, y: y, z: z - 1 },
        { x: x - 2, y: y, z: z },
        { x: x, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x - 2, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z + 1 },
        { x: x - 1, y: y + 1, z: z - 1 },
        { x: x - 1, y: y + 1, z: z + 1 },
        { x: x - 2, y: y + 1, z: z - 1 },
        { x: x - 2, y: y + 1, z: z + 1 },
        { x: x, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x - 2, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z + 1 },
        { x: x - 1, y: y + 2, z: z - 1 },
        { x: x - 1, y: y + 2, z: z + 1 },
        { x: x - 2, y: y + 2, z: z - 1 },
        { x: x - 2, y: y + 2, z: z + 1 },
        { x: x, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x - 2, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z + 1 },
        { x: x - 1, y: y + 3, z: z - 1 },
        { x: x - 1, y: y + 3, z: z + 1 },
        { x: x - 2, y: y + 3, z: z - 1 },
        { x: x - 2, y: y + 3, z: z + 1 },
    ]
    // console.warn("dir_1:", JSON.stringify(entity.hasTag("dir_1"), null, 2));
    // console.warn("dir_2:", JSON.stringify(entity.hasTag("dir_2"), null, 2));
    // console.warn("dir_3:", JSON.stringify(entity.hasTag("dir_3"), null, 2));
    // console.warn("dir_4:", JSON.stringify(entity.hasTag("dir_4"), null, 2));
    fullorangePortal = 0;
    front.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:air')) {
            fullorangePortal++;
        }
    });
    if (entity.hasTag("dir_4") && fullorangePortal == 74) {
        return true;
    }
    fullorangePortal = 0;
    left.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:air')) {
            fullorangePortal++;
        }
    });
    if (entity.hasTag("dir_2") && fullorangePortal == 74) {
        return true;
    }
    fullorangePortal = 0;
    back.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:air')) {
            fullorangePortal++;
        }
    });
    if (entity.hasTag("dir_1") && fullorangePortal == 74) {
        return true;
    }
    fullorangePortal = 0;
    right.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('sapphire_portality:portal_orange_block') || block?.permutation.matches('sapphire_portality:portal_orange_east_block') || block?.permutation.matches('minecraft:air')) {
            fullorangePortal++;
        }
    });
    if (entity.hasTag("dir_3") && fullorangePortal == 74) {
        return true;
    }
    return false;

}
function DetectFullPortal(entity, var_dimension) {
    let fullorangePortal_empty = 0;
    const { x, y, z } = entity.location;
    const front_empty = [
        { x: x, y: y + 1, z: z },
        { x: x, y: y + 1, z: z + 1 },
        { x: x, y: y + 1, z: z + 2 },
        { x: x + 1, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z + 1 },
        { x: x - 1, y: y + 1, z: z + 1 },
        { x: x + 1, y: y + 1, z: z + 2 },
        { x: x - 1, y: y + 1, z: z + 2 },
        { x: x, y: y + 2, z: z },
        { x: x, y: y + 2, z: z + 1 },
        { x: x, y: y + 2, z: z + 2 },
        { x: x + 1, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z + 1 },
        { x: x - 1, y: y + 2, z: z + 1 },
        { x: x + 1, y: y + 2, z: z + 2 },
        { x: x - 1, y: y + 2, z: z + 2 },
        { x: x, y: y + 3, z: z },
        { x: x, y: y + 3, z: z + 1 },
        { x: x, y: y + 3, z: z + 2 },
        { x: x + 1, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z + 1 },
        { x: x - 1, y: y + 3, z: z + 1 },
        { x: x + 1, y: y + 3, z: z + 2 },
        { x: x - 1, y: y + 3, z: z + 2 },
    ]
    const back_empty = [
        { x: x, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z - 2 },
        { x: x + 1, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z - 1 },
        { x: x - 1, y: y + 1, z: z - 1 },
        { x: x + 1, y: y + 1, z: z - 2 },
        { x: x - 1, y: y + 1, z: z - 2 },
        { x: x, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z - 2 },
        { x: x + 1, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z - 1 },
        { x: x - 1, y: y + 2, z: z - 1 },
        { x: x + 1, y: y + 2, z: z - 2 },
        { x: x - 1, y: y + 2, z: z - 2 },
        { x: x, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z - 2 },
        { x: x + 1, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z - 1 },
        { x: x - 1, y: y + 3, z: z - 1 },
        { x: x + 1, y: y + 3, z: z - 2 },
        { x: x - 1, y: y + 3, z: z - 2 },
    ]
    const left_empty = [
        { x: x, y: y + 1, z: z },
        { x: x + 1, y: y + 1, z: z },
        { x: x + 2, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z + 1 },
        { x: x + 1, y: y + 1, z: z - 1 },
        { x: x + 1, y: y + 1, z: z + 1 },
        { x: x + 2, y: y + 1, z: z - 1 },
        { x: x + 2, y: y + 1, z: z + 1 },
        { x: x, y: y + 2, z: z },
        { x: x + 1, y: y + 2, z: z },
        { x: x + 2, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z + 1 },
        { x: x + 1, y: y + 2, z: z - 1 },
        { x: x + 1, y: y + 2, z: z + 1 },
        { x: x + 2, y: y + 2, z: z - 1 },
        { x: x + 2, y: y + 2, z: z + 1 },
        { x: x, y: y + 3, z: z },
        { x: x + 1, y: y + 3, z: z },
        { x: x + 2, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z + 1 },
        { x: x + 1, y: y + 3, z: z - 1 },
        { x: x + 1, y: y + 3, z: z + 1 },
        { x: x + 2, y: y + 3, z: z - 1 },
        { x: x + 2, y: y + 3, z: z + 1 },
    ]
    const right_empty = [
        { x: x, y: y + 1, z: z },
        { x: x - 1, y: y + 1, z: z },
        { x: x - 2, y: y + 1, z: z },
        { x: x, y: y + 1, z: z - 1 },
        { x: x, y: y + 1, z: z + 1 },
        { x: x - 1, y: y + 1, z: z - 1 },
        { x: x - 1, y: y + 1, z: z + 1 },
        { x: x - 2, y: y + 1, z: z - 1 },
        { x: x - 2, y: y + 1, z: z + 1 },
        { x: x, y: y + 2, z: z },
        { x: x - 1, y: y + 2, z: z },
        { x: x - 2, y: y + 2, z: z },
        { x: x, y: y + 2, z: z - 1 },
        { x: x, y: y + 2, z: z + 1 },
        { x: x - 1, y: y + 2, z: z - 1 },
        { x: x - 1, y: y + 2, z: z + 1 },
        { x: x - 2, y: y + 2, z: z - 1 },
        { x: x - 2, y: y + 2, z: z + 1 },
        { x: x, y: y + 3, z: z },
        { x: x - 1, y: y + 3, z: z },
        { x: x - 2, y: y + 3, z: z },
        { x: x, y: y + 3, z: z - 1 },
        { x: x, y: y + 3, z: z + 1 },
        { x: x - 1, y: y + 3, z: z - 1 },
        { x: x - 1, y: y + 3, z: z + 1 },
        { x: x - 2, y: y + 3, z: z - 1 },
        { x: x - 2, y: y + 3, z: z + 1 },
    ]
    // console.warn("dir_1:", JSON.stringify(entity.hasTag("dir_1"), null, 2));
    // console.warn("dir_2:", JSON.stringify(entity.hasTag("dir_2"), null, 2));
    // console.warn("dir_3:", JSON.stringify(entity.hasTag("dir_3"), null, 2));
    // console.warn("dir_4:", JSON.stringify(entity.hasTag("dir_4"), null, 2));
    fullorangePortal_empty = 0;
    front_empty.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('minecraft:air')) {
            fullorangePortal_empty++;
        }
    });
    if (entity.hasTag("dir_4") && fullorangePortal_empty == 27) {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[1] = { directionX: 0, directionZ: 3, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_1[2].full = 1;
            orange_Portal_1[3] = { x: 0, y: 0.4, z: 0.5 };
            orange_Portal_1[4] = { x: x, y: y, z: z + 5 };
            orange_Portal_1[5] = { x: x, y: y + 1, z: z + 1 };
            orange_Portal_1[6] = var_dimension;
            orange_Portal_1[7] = { x: x, y: y, z: z + 3 };
            orange_Portal_1[8] = { x: 0, z: 2 };
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[1] = { directionX: 0, directionZ: 3, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_2[2].full = 1;
            orange_Portal_2[3] = { x: 0, y: 0.4, z: 0.5 };
            orange_Portal_2[4] = { x: x, y: y, z: z + 5 };
            orange_Portal_2[5] = { x: x, y: y, z: z + 1 };
            orange_Portal_2[6] = var_dimension;
            orange_Portal_2[7] = { x: x, y: y, z: z + 3 };
            orange_Portal_2[8] = { x: 0, z: 2 };
        }
        return;

    } else {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[2].full = 0;
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[2].full = 0;
        }
    }
    fullorangePortal_empty = 0;
    left_empty.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('minecraft:air')) {
            fullorangePortal_empty++;
        }
    });
    if (entity.hasTag("dir_2") && fullorangePortal_empty == 27) {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[1] = { directionX: 3, directionZ: 0, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_1[2].full = 1;
            orange_Portal_1[3] = { x: 0.5, y: 0.4, z: 0 };
            orange_Portal_1[4] = { x: 5, y: 0, z: 0 };
            orange_Portal_1[4] = { x: x + 5, y: y, z: z };
            orange_Portal_1[5] = { x: x + 1, y: y + 1, z: z };
            orange_Portal_1[6] = var_dimension;
            orange_Portal_1[7] = { x: x + 3, y: y, z: z };
            orange_Portal_1[8] = { x: 2, z: 0 };
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[1] = { directionX: 3, directionZ: 0, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_2[2].full = 1;
            orange_Portal_2[3] = { x: 0.5, y: 0.4, z: 0 };
            orange_Portal_2[4] = { x: x + 5, y: y, z: z };
            orange_Portal_2[5] = { x: x + 1, y: y + 1, z: z };
            orange_Portal_2[6] = var_dimension;
            orange_Portal_2[7] = { x: x + 3, y: y, z: z };
            orange_Portal_2[8] = { x: 2, z: 0 };
        }
        return;
    } else {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[2].full = 0;
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[2].full = 0;
        }
    }
    fullorangePortal_empty = 0;
    back_empty.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('minecraft:air')) {
            fullorangePortal_empty++;
        }
    });
    if (entity.hasTag("dir_1") && fullorangePortal_empty == 27) {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[1] = { directionX: 0, directionZ: -3, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_1[2].full = 1;
            orange_Portal_1[3] = { x: 0, y: 0.4, z: z - 0.5 };
            orange_Portal_1[4] = { x: x, y: y, z: z - 5 };
            orange_Portal_1[5] = { x: x, y: y + 1, z: z - 1 };
            orange_Portal_1[6] = var_dimension;
            orange_Portal_1[7] = { x: x, y: y, z: z - 3 };
            orange_Portal_1[8] = { x: 0, z: -2 };
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[1] = { directionX: 0, directionZ: -3, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_2[2].full = 1;
            orange_Portal_2[3] = { x: 0, y: 0.4, z: -0.5 };
            orange_Portal_2[4] = { x: x, y: y, z: z - 5 };
            orange_Portal_2[5] = { x: x, y: y + 1, z: z - 1 };
            orange_Portal_2[6] = var_dimension;
            orange_Portal_2[7] = { x: x, y: y, z: z - 3 };
            orange_Portal_2[8] = { x: 0, z: -2 };
        }
        return;
    } else {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[2].full = 0;
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[2].full = 0;
        }
    }
    fullorangePortal_empty = 0;
    right_empty.forEach(cords => {
        const block = entity.dimension.getBlock(cords);
        if (block?.permutation.matches('minecraft:air')) {
            fullorangePortal_empty++;
        }
    });
    if (entity.hasTag("dir_3") && fullorangePortal_empty == 27) {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[1] = { directionX: -3, directionZ: 0, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_1[2].full = 1;
            orange_Portal_1[3] = { x: -0.5, y: 0.4, z: 0 };
            orange_Portal_1[4] = { x: x - 5, y: y, z: z };
            orange_Portal_1[5] = { x: x - 1, y: y + 1, z: z };
            orange_Portal_1[6] = var_dimension;
            orange_Portal_1[7] = { x: x - 3, y: y, z: z };
            orange_Portal_1[8] = { x: -2, z: 0 };
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[1] = { directionX: -3, directionZ: 0, horizontalStrength: 2.7, verticalStrength: 0.3 };
            orange_Portal_2[2].full = 1;
            orange_Portal_2[3] = { x: -0.5, y: 0.4, z: 0 };
            orange_Portal_2[4] = { x: x - 5, y: y, z: z };
            orange_Portal_2[5] = { x: x - 1, y: y + 1, z: z };
            orange_Portal_2[6] = var_dimension;
            orange_Portal_2[7] = { x: x - 3, y: y, z: z };
            orange_Portal_2[8] = { x: -2, z: 0 };
        }
    } else {
        if (areLocationsEqual(orange_Portal_1[0], entity.location)) {
            orange_Portal_1[2].full = 0;
        } else if (areLocationsEqual(orange_Portal_2[0], entity.location)) {
            orange_Portal_2[2].full = 0;
        }
    }

}
function orangeDespawn() {
    for (let index = 0; index < list_dimention.length; index++) {
        const var_dimension = list_dimention[index];

        entities = world.getDimension(var_dimension).getEntities({ type: entityTypeId });
        for (const entity of entities) {
            entity.triggerEvent("animation_turn_off")
            if (orange_Portal_1[0] != { x: undefined, y: undefined, z: undefined } && orange_Portal_2[0] != { x: undefined, y: undefined, z: undefined } && !areLocationsEqual(orange_Portal_1[0], entity.location) && !areLocationsEqual(orange_Portal_2[0], entity.location)) {
            } else {
                if (entity.getComponent('skin_id').value == 1) {
                    if (orange_Portal_1[0] != { x: undefined, y: undefined, z: undefined }) {
                        if ((areLocationsEqual(orange_Portal_1[0], entity.location))) {
                            orange_Portal_1[0] = { x: undefined, y: undefined, z: undefined }
                            orange_Portal_1[2].full = 0;
                            return
                        }
                    }
                    if (orange_Portal_2[0] != { x: undefined, y: undefined, z: undefined }) {
                        if ((areLocationsEqual(orange_Portal_2[0], entity.location))) {
                            orange_Portal_2[0] = { x: undefined, y: undefined, z: undefined }
                            orange_Portal_2[2].full = 0;
                            return
                        }
                    }
                }
            }
        }
    }
}
function orangeInteract() {

    for (const player of world.getPlayers()) {
        for (let index = 0; index < list_dimention.length; index++) {
            const var_dimension = list_dimention[index];
            entities = world.getDimension(var_dimension).getEntities({ type: entityTypeId, location: player.location, closest: 6, maxDistance: 6 });
            for (const entity of entities) {
                if (orange_Portal_1[0].x !== undefined && orange_Portal_2[0].x !== undefined) {
                    if (orange_Portal_1[0].x !== undefined) {
                        if ((areLocationsEqual(orange_Portal_1[0], entity.location))) {
                            if (orange_Portal_1[6] == "overworld") {
                                player.sendMessage("You are currently on: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§2Overworld§r.");
                            } else if (orange_Portal_1[6] == "nether") {
                                player.sendMessage("You are currently on: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§4Nether§r.");
                            } else {
                                player.sendMessage("You are currently on: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§8The end§r.");
                            }
                            system.runTimeout(() => {
                                if (orange_Portal_2[6] == "overworld") {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§2Overworld§r.");
                                } else if (orange_Portal_2[6] == "nether") {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§4Nether§r.");
                                } else {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§8The end§r.");
                                }
                            }, 50);
                        }
                    }
                    if (orange_Portal_2[0].x !== undefined) {
                        if ((areLocationsEqual(orange_Portal_2[0], entity.location))) {
                            if (orange_Portal_2[6] == "overworld") {
                                player.sendMessage("You are currently on: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§2Overworld§r.");
                            } else if (orange_Portal_2[6] == "nether") {
                                player.sendMessage("You are currently on: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§4Nether§r.");
                            } else {
                                player.sendMessage("You are currently on: §a" + orange_Portal_2[0].x + " " + orange_Portal_2[0].y + " " + orange_Portal_2[0].z + "§r, Dimention §l§8The end§r.");
                            }
                            system.runTimeout(() => {
                                if (orange_Portal_1[6] == "overworld") {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§2Overworld§r.");
                                } else if (orange_Portal_1[6] == "nether") {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§4Nether§r.");
                                } else {
                                    player.sendMessage("The portal conected is in: §a" + orange_Portal_1[0].x + " " + orange_Portal_1[0].y + " " + orange_Portal_1[0].z + "§r, Dimention §l§8The end§r.");
                                }
                            }, 50);
                        }
                    }
                }
            }
        }
    }
}
system.afterEvents.scriptEventReceive.subscribe(data => {
    const id = data.id
    if (id === "portality:orange_teleport") {
        system.run(Teleport);
    };
    if (id === "portality:orange_despawn") {
        system.run(orangeDespawn);
    };
    if (id === "portality:orange_spawn") {
        system.run(Spawn);
    };
    if (id === "portality:orange_interact") {
        system.run(orangeInteract);
    };
})
