import * as SERVER from '@minecraft/server';
//cereal ruldrox xd//
SERVER.world.afterEvents.itemCompleteUse.subscribe(event => {
  const { itemStack, source } = event;
  
  const RULDROXevent=[
    '/effect @s regeneration',
    'effect @s speed 10 2',
    'summon pig ~ ~ ~',
    'summon skeleton_horse ~ ~ ~',
  ];
  const randomRULDROXevent = RULDROXevent[Math.floor(Math.random() * RULDROXevent.length)];
  if (itemStack.typeId === "xaierg:cereal_ruldrox" && source.typeId === "minecraft:player") {
    
    source.runCommand(`playsound ambient.weather.thunder @a[r=16]  ~ ~ ~`);
    source.runCommand(Math.random() < 0.5 ?  ` ${randomRULDROXevent}` : null);
    source.runCommand(`ME AGRADECE A RULDROX POR EL CEREAL`);

    
  }
});
//moscas//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:moscas', {
      onRandomTick: e => moscasX(e.block),
  });
});

function moscasX(block) {
  
  block.dimension.getEntities({ location: block.location, maxDistance: 10 })
  .forEach(entity => {
      if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {
              
  const particleMoscas=[
    'xaierg:moscas',
    'xaierg:empty',
    'xaierg:empty'
  ];
  const randomparticleMoscas = particleMoscas[Math.floor(Math.random() * particleMoscas.length)];
            player.runCommand(`execute positioned ${block.location.x} ${block.location.y+1} ${block.location.z} run particle ${randomparticleMoscas}`);
            player.runCommand(`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run playsound moscas.amb @a[r=16] ~ ~ ~`);
            
        player.dimension.playSound("moscas.amb", block.center());
   
        });
      } 
  });
}
//arbusto_gelatina//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:ambient_city', {
      onTick: e => sounds_ambient_cityX(e.block),
  });
});

function sounds_ambient_cityX(block) {
  // Obtén la hora del día
  const time = block.dimension.getTimeOfDay ? block.dimension.getTimeOfDay() : SERVER.world.getTimeOfDay();

  if (time >= 0 && time < 13000) {
    // Día: ejecuta sonidos de ciudad
    block.dimension.getEntities({ location: block.location, maxDistance: 20 })
      .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {
            const ambient = ['block.city.ambient'];
            const randomambient = ambient[Math.floor(Math.random() * ambient.length)];
            player.runCommand(Math.random() < 0.8 ? `playsound ${randomambient} @a[r=20] ${block.location.x} ${block.location.y} ${block.location.z}` : `playsound empty @s`);
          });
        }
      });

      block.dimension.getEntities({ location: block.location, maxDistance: 16 })
  .forEach(entity => {
      if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {
              
  const particleMoscas=[
    'xaierg:mosquitos',
    'xaierg:mariposas1',
    'xaierg:mariposas2',
    'xaierg:mosquitos',
    'xaierg:mariposas1',
    'xaierg:mariposas2',
    'xaierg:pajaros1',
    'xaierg:pajaros1',
    'xaierg:pajaros2'
  ];
  const randomparticleMoscas = particleMoscas[Math.floor(Math.random() * particleMoscas.length)];
            player.runCommand(Math.random() < 0.5 ? `execute positioned ${block.location.x} ${block.location.y+1} ${block.location.z} run particle ${randomparticleMoscas} ~~1~`: `playsound empty @s`);
            
   
        });
      } 
  });

  } else {
    // Noche: ejecuta sonidos o efectos diferentes
    block.dimension.getEntities({ location: block.location, maxDistance: 16 })
      .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {
            // Ejemplo: sonido nocturno
            player.runCommand(Math.random() < 0.5 ? `playsound block.city_noche.ambient @a[r=20] ${block.location.x} ${block.location.y} ${block.location.z}`: `playsound empty @s`);
          });
        }
      });
  }
}

 //luz_encendido//
 SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:luz_encendido', {
    onPlayerInteract: e => {if (!e.player) return;

      const equippable = e.player.getComponent("minecraft:equippable");
      if (!equippable) return;

      

      const abierto = e.block.permutation.withState("xaierg:encendido", true)
      e.block.setPermutation(
          abierto
      );
       const effectLocation = e.block.center();
e.dimension.playSound("polvo.amb", effectLocation);
       }
});

});

//papeles//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:papeles', {
      onPlayerInteract: e => { if (!e.player) return;

        if (e.player.getGameMode() !== "creative") return;
        const equippable = e.player.getComponent("minecraft:equippable");
        if (!equippable) return;
        
        const mainhand = equippable.getEquipmentSlot("Mainhand");
        if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:stick")
            return;
        
        let growth = e.block.permutation.getState("xaierg:growth");
        
        if (growth >= 12) return;
        
        growth += 1;
        e.block.setPermutation(
            e.block.permutation.withState("xaierg:growth", growth)
        );
        
        
        const effectLocation = e.block.center();
        e.dimension.playSound("polvo.amb", effectLocation);
        e.dimension.spawnParticle(
            "xaierg:polvo",
            effectLocation
        );
         },
  });
  
  });
  

//cables//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:cables', {
      onPlayerInteract: e => { if (!e.player) return;

        if (e.player.getGameMode() !== "creative") return;
        const equippable = e.player.getComponent("minecraft:equippable");
        if (!equippable) return;
        
        const mainhand = equippable.getEquipmentSlot("Mainhand");
        if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:stick")
            return;
        
        let growth = e.block.permutation.getState("xaierg:growth");
        
        if (growth >= 2) return;
        
        growth += 1;
        e.block.setPermutation(
            e.block.permutation.withState("xaierg:growth", growth)
        );
        
        const effectLocation = e.block.center();
        e.dimension.playSound("polvo.amb", effectLocation);
        e.dimension.spawnParticle(
            "xaierg:polvo",
            effectLocation
        );
         },
  });
  
  });

//nevera_stage//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:nevera_stage', {
      onPlayerInteract: e => { if (!e.player) return;

        if (e.player.getGameMode() !== "creative") return;
        const equippable = e.player.getComponent("minecraft:equippable");
        if (!equippable) return;
        const mainhand = equippable.getEquipmentSlot("Mainhand");
        if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:stick")
            return;

        let growth = e.block.permutation.getState("xaierg:growth");
       
        if (growth >= 8 ) return;
        
        growth += 1;
        e.block.setPermutation(
            e.block.permutation.withState("xaierg:growth", growth)
        );
        const effectLocation = e.block.center();
        e.dimension.playSound("polvo.amb", effectLocation);
        e.dimension.spawnParticle(
            "xaierg:polvo",
            effectLocation
        );
         },
  });
  
  });
 