import {
  world
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('we:fancy_dead_bush_break', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else if (heldItem.typeId !== 'minecraft:shears') {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush\"`)
      }
    }
  });

initEvent.blockComponentRegistry.registerCustomComponent('we:thorny_roots_break', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else if (heldItem.typeId !== 'minecraft:shears') {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/thorny_roots\"`)
      }
    }
  });

initEvent.blockComponentRegistry.registerCustomComponent('we:cattails_break', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/chance_cattails\"`)
      } else if (heldItem.typeId !== 'minecraft:shears') {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/chance_cattails\"`)
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/cattails\"`)
      }
    }
  });

initEvent.blockComponentRegistry.registerCustomComponent('we:rose_bush_break', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      var blockId = block.typeId.replace('we:', '');
      blockId = blockId.replace('_block', '');

      e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/${blockId}\"`)
      
    }
  });
});