import { world, system, BlockPermutation, GameMode } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:blooming_corrupted_sapling_growth", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockLocation = block.location;
      const playerEquippable = player.getComponent("equippable");
      const itemUsed = playerEquippable.getEquipment("Mainhand");
      function decreaseItem () {
        if (!player.matches({ gameMode: GameMode.creative })) {
          const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
          playerEquippable.setEquipment("Mainhand", decrementItem)
        }
      }
      if (itemUsed === undefined) return;
      if (block.typeId === "nicothekid:blooming_corrupted_sapling_block" && itemUsed.typeId === "minecraft:bone_meal") {
        system.run(() => {
          let randomChance = Math.random() * (100.0 - 0.0) + 0.0;
          decreaseItem ();
          block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.dimension.spawnParticle("minecraft:crop_growth_emitter", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
          if (randomChance <= 40.0) {
            let randomVariation = Math.random() * (100.0 - 0.0) + 0.0;
            if (randomVariation <= 16.6) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_small_01 ${blockLocation.x - 4} ${blockLocation.y - 1} ${blockLocation.z - 4}`);
            }
            else if (randomVariation <= 33.2) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_small_02 ${blockLocation.x - 4} ${blockLocation.y - 1} ${blockLocation.z - 4}`);
            }
            else if (randomVariation <= 49.8) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_small_03 ${blockLocation.x - 4} ${blockLocation.y - 1} ${blockLocation.z - 4}`);
            }
            else if (randomVariation <= 66.4) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_small_04 ${blockLocation.x - 4} ${blockLocation.y - 1} ${blockLocation.z - 4}`);
            }
            else if (randomVariation <= 83) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_small_05 ${blockLocation.x - 4} ${blockLocation.y - 1} ${blockLocation.z - 4}`);
            }
            else if (randomVariation <= 99.6) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load blooming_corrupted_tree_large_01 ${blockLocation.x - 8} ${blockLocation.y - 1} ${blockLocation.z - 8}`);
            }
            else return;
          }
          else return;
        });
      }
      else return;
    }
  });
});
