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

const overworld = world.getDimension("overworld");

let players = world.getPlayers();
let playerCount = players.length;
let lastPlayer = "";

export function onPlayerJoin(player) {
    playerCount = players.length;
    world.sendMessage(String(player.name + " joined!"));
    lastPlayer = player.name;
    player.addTag('joined world');
    player.addTag("joinz");
    let inventory = player.getComponent("inventory").container;

    world.sendMessage(detGamemode(player));
    world.sendMessage(Object.getOwnPropertyNames(GameMode));
    world.sendMessage(String(inventory.size));

}

export function onPlayerLeave(player) {
    playerCount = players.length;
    world.sendMessage(String(lastPlayer + " left!"));
}

export function detGamemode(player) {
    return Object.getOwnPropertyNames(GameMode).find(gameMode => world.getPlayers({ name: player.name, gameMode }).length) ?? 'unknown';
}

export function removeTagsWith(player, tags, startString) {
    for (const tag of tags) {
        if (tag.startsWith(startString)) {
            player.removeTag(tag);
        }
    }
}