global.cooldown = 0;
ServerEvents.tick(event =>{
  if(global.cooldown > 0){
    global.cooldown--
  }
})
PlayerEvents.chat((event) => {
  const { player, message, level } = event;
  const kubejsreload = [
    "client_scripts",
    "config",
    "lang",
    "server_scripts",
    "startup_scripts",
    "textures",
  ];
  if (message == "re") {
    kubejsreload.forEach((c) => level.runCommandSilent(`kubejs reload ${c}`));
    player.tell(Text.of("Reloaded All Scripts!").green());
  }
});

ServerEvents.command((event) => {
  const { server,commandName} = event;
  if (commandName== "rtp") {
    console.log('lockedcommand')
    server.tell(Text.of("locked command!").green());
    event.cancel();

  }
});

//execute in nwtc:void run tp @p 1000 80 1000

//扫帚
ItemEvents.rightClicked('item.rightclicked', event => {
    if (event.item == 'nwtc:broom' && global.cooldown != 0){
        let ceil = Math.ceil(global.cooldown/20);
          event.player.tell( [
          Text.of({"translate": "nwtc.servercommands.cooldown.1"}).red(),
          Text.of({"translate": "nwtc.servercommands.cooldown.2"}).green(),
          Text.of(ceil).yellow(),
          Text.of({"translate": "nwtc.servercommands.cooldown.3"}).green(),
      
          
      
      ]);
      }
      if (event.item == 'nwtc:broom' && global.cooldown == 0){
        global.cooldown = 1220;
        //event.server.runCommandSilent(`kill @e[type=!player]`)
        event.server.runCommandSilent(`kill @e[type=item]`)
        event.player.tell( [
        Text.of({"translate": "nwtc.servercommands.clearlag"}).red(),
        
      ]);
      
     
      event.item.setCount(event.item.getCount()-1)
      }
    
})
BlockEvents.rightClicked('block.right_click', event => {

//虚空钥匙
if (event.item.id == "nwtc:void_keys")
{
  if (event.block.id.includes('stone')) 
  {
    event.item.setDamageValue(event.item.getDamageValue()+1)
    event.block.set('minecraft:air')
    event.server.runCommandSilent(`execute in lobby:void_dimension run teleport ${event.player.name.string} 2 79 -14`) 
    event.player.tell(Text.translate('nwtc.servercommands.void_keys.correct'));
    if (event.item.getDamageValue()>= 10)
    {
      event.item.setCount(event.item.getCount()-1)
      event.player.tell(Text.translate('nwtc.servercommands.keyroll.broken'));
    }
  }
  else
  {
    event.player.tell(Text.translate('nwtc.servercommands.keys.error'));
    event.cancel();

  }

}
//矿工钥匙
if (event.item.id == "nwtc:mining_keys")
{ 
  if (event.block.id.includes('stone')) 
  { 
    event.item.setDamageValue(event.item.getDamageValue()+1)
    event.block.set('minecraft:air')
    event.server.runCommandSilent(`execute in lobby:mining_dimension run teleport ${event.player.name.string} 195 21 563`) 
    event.player.tell(Text.translate('nwtc.servercommands.mining_keys.correct'));
    if (event.item.getDamageValue()>= 10)
    {
      event.item.setCount(event.item.getCount()-1)
      event.player.tell(Text.translate('nwtc.servercommands.keyroll.broken'));
    }
  }
  else
  //if (event.block.id != 'minecraft:stone')
  {
    event.player.tell(Text.translate('nwtc.servercommands.keys.error'));
    event.cancel();
  }
}
//魔杖
if (event.item.id == "nwtc:keyroll") {
  if (event.block.id== "solarcraft:invincible_solar_stone" || event.block.id== "minecraft:bedrock") 
  {
    event.block.set('minecraft:air')
    event.item.setDamageValue(event.item.getDamageValue() + 2)
    event.player.tell(Text.translate('nwtc.servercommands.keyroll.correct'));
    if (event.item.getDamageValue()>= 20)
    {
      event.item.setCount(event.item.getCount()-1)
      event.player.tell(Text.translate('nwtc.servercommands.keyroll.broken'));
    }
  }
  if (event.item.id == "nwtc:keyroll" && event.block.id== "minecraft:stone") 
  {
    event.block.set('minecraft:bedrock')
    event.item.setDamageValue(event.item.getDamageValue() + 1)
    event.player.tell(Text.translate('nwtc.servercommands.keyroll.correct'));
    if (event.item.getDamageValue()>= 20)
    {
      event.item.setCount(event.item.getCount()-1)
      event.player.tell(Text.translate('nwtc.servercommands.keyroll.broken'));
    }
  }
}


})


global.rtp_cooldown = 0;
ServerEvents.tick(event =>{
  if(global.rtp_cooldown > 0){
    global.rtp_cooldown--
  }
})
ItemEvents.rightClicked('rtp', event => {
  if (event.item == 'nwtc:tpfeather' && global.rtp_cooldown != 0){
      let ceil = Math.ceil(global.rtp_cooldown/20);
        event.player.tell( [
        Text.of({"translate": "nwtc.servercommands.cooldown.1"}).red(),
        Text.of({"translate": "nwtc.servercommands.cooldown.2"}).green(),
        Text.of(ceil).yellow(),
        Text.of({"translate": "nwtc.servercommands.cooldown.3"}).green(),
    ]);
  }
    let tps_x = Math.ceil(Math.random()*1800);
    let tps_y = Math.ceil(Math.random()*100);
    let tps_z = Math.ceil(Math.random()*1000);
  if (event.item == 'nwtc:tpfeather' && global.rtp_cooldown == 0 && event.level.dimension !="lobby:void_dimension"){
      
    
 
      global.rtp_cooldown = 1920;
      //event.server.runCommandSilent(`teleport @p ${tps_x} ${tps_y} ${tps_z}`) 
      event.server.runCommandSilent(`execute in ${event.level.dimension} run setblock ${tps_x} ${tps_y + 1} ${tps_z} minecraft:air`)
      event.server.runCommandSilent(`execute in ${event.level.dimension} run setblock ${tps_x} ${tps_y + 2} ${tps_z} minecraft:air`)  
      event.server.runCommandSilent(`execute in ${event.level.dimension} run teleport ${event.player.name.string} ${tps_x} ${tps_y} ${tps_z}`) 
      event.player.tell( [
        Text.of({"translate": "nwtc.servercommands.rtp.correct"}).red(),
        Text.of(tps_x).green(),
        Text.of("，   "),
        Text.of(tps_y).yellow(),
        Text.of("，   "),
        Text.of(tps_z).green(),
    ]);
    event.player.give(Item.of('nwtc:tpfeather',`
    {display:{Lore:['{\"color\":\"#bbff55\",\"translate\":\"Location of last tpr\"}',
    '{\"text\":\"§6 ${tps_x}，${tps_y}，${tps_z}\"}',
    '{\"italic\":\"false\",\"color\":\"yellow\",\"text\":\" ${event.level.dimension}\"}'
    ],Name:'{\"italic\":\"false\",\"bold\":\"true\",\"color\":\"#FF0000\",\"translate\":\"Awright5\\'s Feather\"}'}}
    `)) 
    event.item.setCount(event.item.getCount()-1)
    event.server.runCommandSilent(`execute as ${event.player.name.string} run effect give ${event.player.name.string} minecraft:slow_falling 20`)
    event.server.runCommandSilent(`execute as ${event.player.name.string} run effect give ${event.player.name.string} minecraft:night_vision 20`)
    }
    if (event.item == 'nwtc:tpfeather' && global.rtp_cooldown == 0 && event.level.dimension =="lobby:void_dimension"){
      event.player.tell( [
        Text.of({"translate": "nwtc.servercommands.rtp.error"}).red(),
    ]);
    }

  
})

ItemEvents.rightClicked('unlock_all_progressions_book', event => {
//进度手册
if (event.item == 'nwtc:unlock_all_progressions_book'){
  
  event.server.runCommandSilent(`execute as ${event.player.name.string} run solarcraft progressions unlock all`) 
  event.player.tell( [
    Text.of({"translate": "nwtc.servercommands.unlock_all_progressions_book.correct"}).red(),
  ]);
  event.item.setCount(event.item.getCount()-1)
}
})
ItemEvents.rightClicked('unlock_all_fragment_book', event => {
  //片断手册
  if (event.item == 'nwtc:unlock_all_fragment_book'){
    
    event.server.runCommandSilent(`execute as ${event.player.name.string} run solarcraft fragments unlockall`) 
    event.player.tell( [
      Text.of({"translate": "nwtc.servercommands.unlock_all_fragment_book.correct"}).red(),
    ]);
    event.item.setCount(event.item.getCount()-1)
  }
  })

//减少难度
global.modify_difficulty_items_cooldown = 0;
ServerEvents.tick(event =>{
  if(global.modify_difficulty_items_cooldown > 0){
    global.modify_difficulty_items_cooldown--
  }
})
global.change_difficulty_items_cooldown = 0;
ServerEvents.tick(event =>{
  if(global.change_difficulty_items_cooldown > 0){
    global.change_difficulty_items_cooldown--
  }
})
ItemEvents.rightClicked('modify_difficulty_items', event => {
  if (event.item == 'nwtc:charred_meat' && global.change_difficulty_items_cooldown != 0 ){
    let ceil = Math.ceil(global.change_difficulty_items_cooldown/20);
      event.player.tell( [
      Text.of({"translate": "nwtc.servercommands.cooldown.1"}).red(),
      Text.of({"translate": "nwtc.servercommands.cooldown.2"}).green(),
      Text.of(ceil).yellow(),
      Text.of({"translate": "nwtc.servercommands.cooldown.3"}).green(),
  ]);
}
  if (event.item == 'nwtc:charred_meat' && !event.item.nbt && global.change_difficulty_items_cooldown == 0 ){
    event.player.give(Item.of('nwtc:charred_meat',`
    {display:{Lore:['{\"color\":\"#bbff55\",\"translate\":\"There will be a price\"}',
    '{\"italic\":\"false\",\"color\":\"yellow\",\"text\":\" ${event.player.name.string}\"}'
    ],Name:'{\"italic\":\"false\",\"bold\":\"true\",\"color\":\"#FF0000\",\"translate\":\"Devil whisper"}'}}
    `)) 
    event.item.setCount(event.item.getCount()-1)
    global.change_difficulty_items_cooldown = 1920;
  }
  else if (event.item == 'nwtc:charred_meat' && event.item.nbt && global.change_difficulty_items_cooldown == 0){
    event.player.tell( [
      Text.of({"translate": "nwtc.servercommands.modify_difficulty.1"}).red(),
      Text.of(event.player.name.string).yellow(),
      Text.of({"translate": "nwtc.servercommands.modify_difficulty.2"}).green(),
      Text.of({"translate": "nwtc.servercommands.modify_difficulty.3"}).yellow(),
    ]);
    event.server.runCommandSilent(`execute as ${event.player.name.string} run improvedmobs difficulty player ${event.player.username} add -5`) 
    event.server.runCommandSilent(`execute as ${event.player.name.string} run improvedmobs difficulty add -5`)
    //event.server.runCommandSilent(`execute as ${event.player.name.string} run apoth spawn_boss ${event.player.name.string} apotheosis:overworld/husk`)
    event.server.runCommandSilent(`execute as ${event.player.name.string} run effect give ${event.player.name.string} minecraft:weakness 200`)
    event.server.runCommandSilent(`execute as ${event.player.name.string} run effect give ${event.player.name.string} minecraft:hunger 200`)
    event.item.setCount(event.item.getCount()-1)
  }
  })


  ItemEvents.rightClicked('coin_point', event => {
    //开槽
    if (event.item == 'nwtc:coin_point'){
      
      event.server.runCommandSilent(`execute as ${event.player.name.string} run curios set ring ${event.player.name.string} 10`) 
      event.player.tell( [
      Text.of({"translate": "nwtc.servercommands.rings_king.correct"}).red(),
      ]);
      event.item.setCount(event.item.getCount()-1)
    }
    })

   