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

/* 
Checks if the player joins for the first time
*/
export function playerJoins(player) {
  if (!player.hasTag("cbcl_rm_registered_player")) {

    const WorldFirstLoadedObjective =
      world.scoreboard.getObjective("cbcl_rm_WorldFirstLoaded") ??
      world.scoreboard.addObjective("cbcl_rm_WorldFirstLoaded", "cbcl_rm_WorldFirstLoaded");

    player.runCommand("/scoreboard players add var cbcl_rm_WorldFirstLoaded 1");

    const WorldFirstLoadedScore = WorldFirstLoadedObjective.getScore("var");
    if (WorldFirstLoadedScore == 1) {
    }

    player.runCommand(`/tellraw @s {"rawtext":[{"text":"§r§aRevive Add-On installed!"}]}`);
    player.runCommand("/gamerule sendcommandfeedback false");

    let reviveGiven = world.getDynamicProperty("cbcl_rm_reviveGiven");
    if (!reviveGiven) {
      player.runCommand("/give @s cbcl_rm:revive_settings");

      player.runCommand(`/tellraw @s {"rawtext":[{"text":"§r§aYou received the Revive Settings item because you are an operator in this world!"}]}`);

      world.setDynamicProperty("cbcl_rm_reviveSettingsPlayer", player.id);

      world.setDynamicProperty("cbcl_rm_reviveGiven", true);
    }

    player.addTag("cbcl_rm_registered_player");
  }
}
