
import { system, world } from '@minecraft/server';
import { levelHandler } from 'util/levelHandler'
import { getClosestPlayerPosition } from 'util/positionHandler'
import { cancelDimensionChange } from 'util/cancelDimensionChange'


//Main Loop
system.runTimeout(() => {
    system.runInterval(() => {
        try {
            if (system.currentTick % 100 === 0) {
                const ALL_CHUNKLOCKS = world.getDimension('overworld').getEntities({ type: "piew:chunklock" });
                ALL_CHUNKLOCKS.forEach(entity => {
                    //getClosestPlayerPosition(entity)
                    levelHandler()
                });
            }
            const ALL_PLAYERS = world.getAllPlayers();
            ALL_PLAYERS.forEach(player => {
                if (!player.hasTag('init')) {
                    player.runCommand('/dialogue change @e[type=piew:tutorial_npc,c=1] NPC @a');
                    player.addTag('init')
                }
                if (!player.isValid()) return;
                else {
                    if (player.dimension.id !== "minecraft:nether") {
                        const scoreboard = world.scoreboard;
                        const cooldown = scoreboard.getObjective('nether_time') ?? scoreboard.addObjective('nether_time', 'nether_time');

                        cooldown.setScore(player, 100)

                        return;
                    } else {
                        cancelDimensionChange(player)
                    }
                }
            })
        }
        catch (e) { console.warn(e); }

    }, 0);
}, 110);

//world.afterEvents.playerDimensionChange.subscribe(cancelDimensionChange)