import * as SERVER from '@minecraft/server';
//deco_lab//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:deco_lab', {
    onTick: e => deco_labX(e.block)
});

});
function weightedRandom(items) {
  const totalWeight = items.reduce((sum, item) => sum + item.weight, 0);
  let random = Math.random() * totalWeight;
  for (const item of items) {
    if (random < item.weight) return item.value;
    random -= item.weight;
  }
}

// Simplificado: cada estructura tiene un peso (probabilidad)
const genstructuresB2 = [
  { value: 'structure load deco_lab_in1 ~~~', weight: 3 },
  { value: 'structure load deco_lab_in2 ~~~', weight: 3 },
  { value: 'structure load cordy1_deco ~-3~~-3', weight: 8 },
  { value: 'structure load cordy2_deco ~-3~~-3', weight: 8 },
  { value: 'structure load cordy3_deco ~-3~~-3', weight: 8 },
  { value: 'structure load deco_city_cordy1 ~-3~~-3', weight: 5 },
  { value: 'structure load deco_city_cordy2 ~-3~~-3', weight: 5 },
  { value: 'structure load deco_city_cordy3 ~-3~~-3', weight: 5 },
  { value: 'structure load sacrificio1 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio2 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio3 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio4 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio5 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio6 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio7 ~-2~~-2', weight: 1 },
  { value: 'structure load sacrificio8 ~-2~~-2', weight: 1 },
  { value: 'setblock ~~~ barrel', weight: 2 },
  { value: 'setblock ~~~ xaierg:bloque_precaucion', weight: 8 }
];

const genstructuresB3 = [
  { value: 'setblock ~~~ barrel', weight: 3 },
  { value: 'setblock ~~~ xaierg:bloque_precaucion', weight: 4 }
];

function deco_labX(block) {
  const summon = weightedRandom(genstructuresB2);
  const summon2 = weightedRandom(genstructuresB3);

  block.dimension.getEntities({ location: block.location, maxDistance: 20 })
    .forEach(entity => {
      if (entity.typeId == 'minecraft:player') {
        block.dimension.getPlayers().forEach(player => {
          player.runCommand(
            Math.random() < 0.25
              ? `execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run ${summon}`
              : `execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run ${summon2}`
          );
          block.setType('xaierg:bloque_precaucion');
        });
      }
    });
}
// core_anti_cordyceps//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:core_anti_cordyceps', {
    onPlayerInteract: e => {
      
      const equippable = e.player.getComponent("minecraft:equippable");
      if (!equippable) return;
      
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem() || mainhand.typeId !== "xaierg:bomba_funcida_c")
          return;
      
      e.player.runCommand(`clear @s xaierg:bomba_funcida_c 0 1`);
 
      e.player.runCommand(`summon xaierg:totem_anti_cordyceps ${e.block.location.x} ${e.block.location.y+1} ${e.block.location.z}`);
e.player.runCommand(`particle xaierg:polvo ${e.block.location.x} ${e.block.location.y+1} ${e.block.location.z}`);
    
  e.dimension.playSound("polvo.amb", effectLocation);
       }
});

});
// enemigos_lab//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:enemigos_lab', {
    onPlayerInteract: e => {
      const abierto = e.block.permutation.withState("xaierg:abierto", true)
      e.block.setPermutation(
          abierto
      );
      e.player.runCommand(`fill ${e.block.location.x+1} ${e.block.location.y+4} ${e.block.location.z+1} ${e.block.location.x-1} ${e.block.location.y+1} ${e.block.location.z-1} air`);
      e.player.runCommand(`summon xaierg:totem_lab_cordy ${e.block.location.x} ${e.block.location.y+1} ${e.block.location.z}`);
e.player.runCommand(`particle xaierg:polvo ${e.block.location.x} ${e.block.location.y+1} ${e.block.location.z}`);
    
  e.dimension.playSound("polvo.amb", effectLocation);
       }
});

});
//cables_amb//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:cables_amb', {
    onTick: e => cables_ambX(e.block)
});

});
function cables_ambX(block) {  
 
  const effectLocation = block.center();
  block.dimension.getEntities({ location: block.location, maxDistance: 128 })
  .forEach(entity => {
      if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {    
            if (Math.random() < 0.2) {
            player.runCommand(`playsound abandonado.amb @a[r=16] ${block.location.x} ${block.location.y} ${block.location.z}`);
          }
           player.runCommand(`playsound elect.amb @a[r=16] ${block.location.x} ${block.location.y} ${block.location.z}`);
            player.runCommand(`particle xaierg:chispa_elec1 ${block.location.x} ${block.location.y} ${block.location.z}`);
               
           
          });
            
      } 
  });  
      
   }
//lab_amb//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:lab_amb', {
    onTick: e => lab_ambX(e.block)
});

});
function lab_ambX(block) {  

  block.dimension.getEntities({ location: block.location, maxDistance: 20 })
  .forEach(entity => {
      if (entity.typeId == 'minecraft:player') {
          block.dimension.getPlayers().forEach(player => {    
            player.runCommand(`playsound abandonado_lab.amb @a[r=20] ${block.location.x} ${block.location.y} ${block.location.z}`);
               
          });
            
      } 
  });  
      
   }
//interruptor//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:interruptor1', {
      onPlayerInteract: e => {     
        const effectLocation = e.block.center();
        e.dimension.playSound("polvo.amb", effectLocation);
        e.dimension.spawnParticle(
            "xaierg:polvo",
            effectLocation
        );
        
        e.player.runCommand(`execute positioned ${e.block.location.x} ${e.block.location.y} ${e.block.location.z} run structure load ascensor1 ~-3~-9~-1`);
        e.player.runCommand(`execute positioned ${e.block.location.x} ${e.block.location.y} ${e.block.location.z} run setblock ~1~-2~ fire`); 
         },
  });
  
  });
  //interruptor2//
  SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:interruptor2', {
      onPlayerInteract: e => {     
        const effectLocation = e.block.center();
        e.dimension.playSound("polvo.amb", effectLocation);
        e.dimension.spawnParticle(
            "xaierg:polvo",
            effectLocation
        );
        
        e.player.runCommand(`execute positioned ${e.block.location.x} ${e.block.location.y} ${e.block.location.z} run structure load ascensor2 ~3~-5~-2`);
        e.player.runCommand(`execute positioned ${e.block.location.x} ${e.block.location.y} ${e.block.location.z} run setblock ~~-1~ fire`); 
         
         },
  });
  
  });

  //lbg//
  SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:lgb', {
      onTick: e => lbgX(e.block)
  });
  
  });
    function lbgX(block) {  
      block.dimension.getEntities({ location: block.location, maxDistance: 128 })
      .forEach(entity => {
          if (entity.typeId == 'minecraft:player') {
              block.dimension.getPlayers().forEach(player => {    
                player.runCommand(`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load lgb ~~-30~1`);   
    block.setType('minecraft:stone');
              });
                
          } 
      });  
          
       }
       //lsb//
  SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:lsb', {
      onTick: e => lsbX(e.block)
  });
  
  });
    function lsbX(block) {  
      block.dimension.getEntities({ location: block.location, maxDistance: 64 })
      .forEach(entity => {
          if (entity.typeId == 'minecraft:player') {
              block.dimension.getPlayers().forEach(player => {    
                player.runCommand(`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load lsb ~1~-5~-11`);   
    block.setType('minecraft:stone');
              });
                
          } 
      });  
          
       }
            //lsb2//
  SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:lsb2', {
      onTick: e => lsb2X(e.block)
  });
  
  });
    function lsb2X(block) {  
      block.dimension.getEntities({ location: block.location, maxDistance: 16 })
      .forEach(entity => {
          if (entity.typeId == 'minecraft:player') {
              block.dimension.getPlayers().forEach(player => {    
                player.runCommand(`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load lsb2 ~1~-5~-17`);   
    block.setType('minecraft:stone');
              });
                
          } 
      });  
          
       }
       //hop_tlb//
  SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('xaierg:hop_tlb', {
      onTick: e => hop_tlbX(e.block)
  });
  
  });
    function hop_tlbX(block) {  
      const labSector = [
        'hop_tlb_a1 ~1 ~ ~-2',
        'hop_tlb_a2 ~1 ~-11 ~-2',
        'hop_tlb_a3 ~1 ~ ~-8',
        'hop_tlb_a4 ~1 ~-12 ~-6',
        'hop_tlb_a1 ~1 ~ ~-2',
        'hop_tlb_a2 ~1 ~-11 ~-2',
        'hop_tlb_a3 ~1 ~ ~-8',
        'hop_tlb_a4 ~1 ~-12 ~-6',
        'hop_tlb_a1 ~1 ~ ~-2',
        'hop_tlb_a2 ~1 ~-11 ~-2',
        'hop_tlb_a3 ~1 ~ ~-8',
        'hop_tlb_a4 ~1 ~-12 ~-6',
        'cam_lab_a1 ~1 ~ ~-2',
      'cam_lab_a2 ~1 ~-4 ~-2',
      'cam_lab_a3 ~1 ~ ~-2',
      'cam_lab_a4 ~1 ~ ~-2',
      'cam_lab_a5 ~1 ~ ~-2',
      'cam_lab_a6 ~1 ~ ~-2',
      'cam_lab_a7 ~1 ~ ~-2',
      'cam_lab_a8 ~1 ~-8 ~-8',
      'cam_lab_a9 ~1 ~ ~-2',
      'cam_lab_a10 ~1 ~ ~-2',
        'cam_lab_a1 ~1 ~ ~-2',
      'cam_lab_a2 ~1 ~-4 ~-2',
      'cam_lab_a3 ~1 ~ ~-2',
      'cam_lab_a4 ~1 ~ ~-2',
      'cam_lab_a5 ~1 ~ ~-2',
      'cam_lab_a6 ~1 ~ ~-2',
      'cam_lab_a7 ~1 ~ ~-2',
      'cam_lab_a8 ~1 ~-8 ~-8',
      'cam_lab_a9 ~1 ~ ~-2',
      'cam_lab_a10 ~1 ~ ~-2'
      ];
      const randomlabSector = labSector[Math.floor(Math.random() * labSector.length)];
      block.dimension.getEntities({ location: block.location, maxDistance: 16 })
      .forEach(entity => {
          if (entity.typeId == 'minecraft:player') {
              block.dimension.getPlayers().forEach(player => {    
                player.runCommand(Math.random() < 0.095 ? `setblock ${block.location.x} ${block.location.y} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load ${randomlabSector}`);   
    block.setType('minecraft:stone');
              });
                
          } 
      });  
          
       }
//hop_tlbh//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:hop_tlbh', {
    onTick: e => hop_tlbhX(e.block)
});

});
  function hop_tlbhX(block) {  
    const labSector = [
      'hop_tlbh_a1 ~-2 ~ ~1',
      'hop_tlbh_a2 ~-2 ~ ~1',
      'hop_tlbh_a1 ~-2 ~ ~1',
      'hop_tlbh_a2 ~-2 ~ ~1',
      'hop_tlbh_a1 ~-2 ~ ~1',
      'hop_tlbh_a2 ~-2 ~ ~1',
      'cam_labh_a1 ~-2 ~ ~1',
      'cam_labh_a2 ~-2 ~ ~1',
      'cam_labh_a3 ~-2 ~ ~1',
      'cam_labh_a4 ~-2 ~ ~1',
      'cam_labh_a5 ~-2 ~ ~1',
      'cam_labh_a6 ~-2 ~ ~1',
      'cam_labh_a1 ~-2 ~ ~1',
      'cam_labh_a2 ~-2 ~ ~1',
      'cam_labh_a3 ~-2 ~ ~1',
      'cam_labh_a4 ~-2 ~ ~1',
      'cam_labh_a5 ~-2 ~ ~1',
      'cam_labh_a6 ~-2 ~ ~1'
    ];
    const randomlabSector = labSector[Math.floor(Math.random() * labSector.length)];
    block.dimension.getEntities({ location: block.location, maxDistance: 16 })
    .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
            block.dimension.getPlayers().forEach(player => {   
              player.runCommand(Math.random() < 0.5 ? `setblock ${block.location.x} ${block.location.y+1} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y+1} ${block.location.z} run summon xaierg:runner_laboratorio`);    
              player.runCommand(Math.random() < 0.095 ? `setblock ${block.location.x} ${block.location.y} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load ${randomlabSector}`);   
  block.setType('minecraft:stone');
            });
              
        } 
    });  
        
     }

//hop_tlb1//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:hop_tlb1', {
    onTick: e => hop_tlb1X(e.block)
});

});
  function hop_tlb1X(block) {  
    const labSector = [
      'cam_lab_a1 ~1 ~ ~-2',
      'cam_lab_a2 ~1 ~-4 ~-2',
      'cam_lab_a3 ~1 ~ ~-2',
      'cam_lab_a4 ~1 ~ ~-2',
      'cam_lab_a5 ~1 ~ ~-2',
      'cam_lab_a6 ~1 ~ ~-2',
      'cam_lab_a7 ~1 ~ ~-2',
      'cam_lab_a8 ~1 ~-8 ~-8',
      'cam_lab_a9 ~1 ~ ~-2',
      'cam_lab_a10 ~1 ~ ~-2'
    ];
    const randomlabSector = labSector[Math.floor(Math.random() * labSector.length)];
    block.dimension.getEntities({ location: block.location, maxDistance: 16 })
    .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
            block.dimension.getPlayers().forEach(player => {    
              player.runCommand(Math.random() < 0.085 ? `setblock ${block.location.x} ${block.location.y} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load ${randomlabSector}`);   
  block.setType('minecraft:stone');
            });
              
        } 
    });  
        
     }
//hop_tlbh1//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:hop_tlbh1', {
    onTick: e => hop_tlbh1X(e.block)
});

});
  function hop_tlbh1X(block) {  
    const labSector = [
      'cam_labh_a1 ~-2 ~ ~1',
      'cam_labh_a2 ~-2 ~ ~1',
      'cam_labh_a3 ~-2 ~ ~1',
      'cam_labh_a4 ~-2 ~ ~1',
      'cam_labh_a5 ~-2 ~ ~1',
      'cam_labh_a6 ~-2 ~ ~1'
    ];
    const randomlabSector = labSector[Math.floor(Math.random() * labSector.length)];
    block.dimension.getEntities({ location: block.location, maxDistance: 16 })
    .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
            block.dimension.getPlayers().forEach(player => {    
              player.runCommand(Math.random() < 0.085 ? `setblock ${block.location.x} ${block.location.y} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load ${randomlabSector}`);   
  block.setType('minecraft:stone');
            });
              
        } 
    });  
        
     }
//sector_tlb//
SERVER.world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('xaierg:sector_tlb', {
    onTick: e => sector_tlbX(e.block)
});

});
  function sector_tlbX(block) {  
    const labSector = [
      'sector_cam_a1 ~ ~-6 ~',
      'sector_cam_a2 ~ ~-6 ~',
      'sector_cam_a3 ~ ~-6 ~',
      'sector_cam_a4 ~ ~-6 ~',
      'sector_cam_a5 ~ ~-6 ~'
    ];
    const randomlabSector = labSector[Math.floor(Math.random() * labSector.length)];
    block.dimension.getEntities({ location: block.location, maxDistance: 16 })
    .forEach(entity => {
        if (entity.typeId == 'minecraft:player') {
            block.dimension.getPlayers().forEach(player => {    
              player.runCommand(Math.random() < 0.55 ? `setblock ${block.location.x} ${block.location.y} ${block.location.z} stone`:`execute positioned ${block.location.x} ${block.location.y} ${block.location.z} run structure load ${randomlabSector}`);   
  block.setType('minecraft:stone');
            });
              
        } 
    });  
        
     }