import { world,ItemStack,system } from "@minecraft/server";
import { SetStartScoreboards } from "../utils/score";



world.afterEvents.playerSpawn.subscribe((eventData) => {
    let { player, initialSpawn } = eventData;
    player.setDynamicProperty('blazeblade_arena',undefined)
    if (!initialSpawn) return;
    const playerjoin = system.runInterval(() => {
      if(player?.scoreboardIdentity)
        {
          try{
            SetStartScoreboards(player)
            const score = world.scoreboard.getObjective("start");
            if(score.getScore(player)==0)
            {
              FirstJoin(player)
            }
            system.clearRun(playerjoin);
          }
          catch(e){}
          
        }
    },10)

    
});

function FirstJoin(player)
{
   var StartMessage=  {rawtext :[{ 
   translate: "message.interact.StartMessage",
    }] };
  player.sendMessage(StartMessage)
  player.runCommand('gamerule sendcommandfeedback false')
  player.runCommand('gamerule commandblockoutput false')
  let inv = player.getComponent("inventory").container
  inv.addItem(new ItemStack('edx:quest_book', 1))
  const score = world.scoreboard.getObjective("start");
  score.setScore(player,1)
}