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

world.beforeEvents.itemUse.subscribe(event => {
  const player = event.source, item = event.itemStack;
  
  if (item.typeId === "nb:beherit" && !player.hasTag('beherit_cooldown')) {
      
      const sacrifices = player.dimension.getEntities({
          location: player.location,
          maxDistance: 10,
          excludeNames: [player.name],
          excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:armor_stand"]
      });

      if (sacrifices.length === 0) {
          system.run(() => player.runCommandAsync("title @s actionbar §cNo sacrifices found nearby..."));
          return;
      }

      player.runCommandAsync("tag @s add beherit_cooldown");    
      eclipse_cut(player);
  }
});

function eclipse_cut(player) {
    
 const sacrifices = player.dimension.getEntities({
          location: player.location,
          maxDistance: 40,
          excludeNames: [player.name],
          excludeTypes: ["minecraft:item", "minecraft:xp_orb", "minecraft:armor_stand"]
      });
      
system.runInterval(() => {
 if (player.hasTag('beherit_cut')) {
           player.runCommandAsync("particle nb:beherit_chao ~ ~0.1 ~");  
 }
}, 10);
    
   system.runTimeout(() => {    
       player.runCommandAsync("tag @s add beherit_cut"); 
   sacrifices.runCommandAsync("tag @s add sacrificios");          
   }, 1);
}

world.beforeEvents.itemUse.subscribe(event => {
  const player = event.source, item = event.itemStack
  if (item.typeId === "nb:berserk_mode" && !player.hasTag('berserkm_cooldown')) {
      player.runCommandAsync("tag @s add berserkm_cooldown");    
       berserk(player);
  }
});

function berserk(player) {
   system.runTimeout(() => {    
player.runCommandAsync("tag @s add berserk_armor_ativar");
   }, 40);
      system.runTimeout(() => {    
   player.runCommandAsync("title @s actionbar §4§oBerserk Mode Recharged!");    
   player.runCommandAsync("tag @s remove berserkm_cooldown");    
   }, 5600);
}


world.beforeEvents.itemUse.subscribe(event => {
  const player = event.source, item = event.itemStack
  if (item.typeId === "nb:menu_inicial") {
       menu_inicial(player);
  }
});

function menu_inicial(player) {
   system.runTimeout(() => {    
   player.runCommandAsync("dialogue open @s @s menu_raca"); 
   }, 1);
}

world.beforeEvents.itemUse.subscribe(event => {
  const player = event.source, item = event.itemStack
  if (item.typeId === "nb:menu_status") {
       menu_status(player);
  }
});

function menu_status(player) {
   system.runTimeout(() => {    
   player.runCommandAsync("dialogue open @s @s menu"); 
   }, 1);
}