import { world } from '@minecraft/server';

export function cancelDimensionChange(player) {
        const scoreboard = world.scoreboard;
        const cooldown = scoreboard.getObjective('nether_time') ?? scoreboard.addObjective('nether_time', 'nether_time');
        const cooldownTime = cooldown.getScore(player) ?? cooldown.setScore(player, 100); // scoreboard init not working ??

        if (cooldownTime > 0) {

                if (cooldownTime === 100) {
                        world.sendMessage('§4§l[Gamemaster]§r§2 Entering the Nether is forbidden!');
                }

                const cooldownInSeconds = cooldownTime / 20;
                if (Number.isInteger(cooldownInSeconds)) {
                        //world.sendMessage('§4§l[Gamemaster]§r§2 Teleport in ' + cooldownInSeconds.toString());
                        player.runCommand('title @s actionbar §4§l[Gamemaster]§r§2 Teleport in: §a'+ cooldownInSeconds.toString() )
                }

                cooldown.addScore(player, -1);
                return;
        } else {
                player.runCommand('execute in overworld run tp @s 22 71 8')
                //player.teleport({ x: 22, y: 71, z: 8 }, { dimension: world.getDimension("overworld") });
                world.sendMessage('§4§l[Gamemaster]§r§2 Teleporting back to worldspawn.');
                cooldown.setScore(player, 100);
                return;
        }
}