import { world, Vector } from "@minecraft/server"
import { setTimeout } from "../timers.js"
export function portal() {
    for (const player of world.getPlayers()) {
        const { x, y, z } = player.location;
        if (player.hasTag('teleport_to_end')) {
            player.removeTag('teleport_to_end');
                player.teleport(new Vector(x + 2, y, z - 2), { dimension: world.getDimension("the end") });
        }
        else if (player.hasTag('teleport_to_overworld')) {
            player.removeTag('teleport_to_overworld');
            setTimeout(async () => {
                const { successCount: portals } = await player.runCommandAsync(`testfor @e[type=betterend:check_portal_overworld,r=100]`);
                if (portals == 0) {
                    player.removeTag('waiting_load');
                    await player.runCommandAsync(`structure load eternal_portal2 ~-2 ~-2 ~-2`);
                }
                else {
                    player.removeTag('waiting_load');
                }
            }, 5000);
            player.teleport(new Vector(x + 2, y, z - 2), { dimension: world.getDimension("overworld") });
            player.addTag('waiting_load');
        }
        else if (player.hasTag('waiting_load')) {
            player.onScreenDisplay.setTitle('LOADING...');
            player.teleport(new Vector(x, y, z));
        }
    }
}