import { world, system } from "@minecraft/server";


system.runInterval(
    () => {
        const players = world.getAllPlayers();
        for (let i = 0; i < players.length; i++) {
            const player = players[i];
            if (!player.hasTag('mm:debug_showstates')) continue;
            try {
                const { block, face } = player.getBlockFromViewDirection();
                if (!block) {
                    player.onScreenDisplay.setActionBar("Not looking at a Block.");
                    return;
                };

                player.onScreenDisplay.setActionBar(
                    `§rblock: §7${block.typeId}§r, face: §7${face}§r, xyz: §6${block.location.x} §r/ §6${block.location.y} §r/ §6${block.location.z}§r,\n`
                    + `data: §7${JSON.stringify(block.permutation.getAllStates(), null, 4)}`
                );
            } catch {
                player.onScreenDisplay.setActionBar("Not looking at a Block.");
            };
        };
    },
);

//SHOW STATES CODE BY KAIOGA - THANK YOU KAIOGOO

const interval = 10 //delay between check
let TPS = 20 //Use this on the rest of your codes

let lastDate = Date.now()
system.runInterval(() => {
    const currDate = Date.now()
    TPS = interval * 50 / (currDate - lastDate) * 20
    lastDate = currDate
    for (const player of world.getPlayers({ tags: ["mm:debug_data"] })) {
        player.onScreenDisplay.setActionBar("TPS:" + ` ${Math.round(TPS)}`);
    }
    for (const player of world.getPlayers({ tags: ["mm:debug_loaders"] })) {
        player.onScreenDisplay.setActionBar("Loaders:" + ` ${world.getDimension("the_end").getEntities({ type: "mm:loader" }).length}    -    TPS:  ${Math.round(TPS)}`);
    }
}, interval)