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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mosshroom_fungus_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:mosshroom_fungus_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) {
            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 huge_mosshroom_01 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else if (randomVariation <= 33.2) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load huge_mosshroom_02 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else if (randomVariation <= 49.8) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load huge_mosshroom_03 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else if (randomVariation <= 66.4) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load huge_mosshroom_04 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else if (randomVariation <= 83) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load huge_mosshroom_05 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else if (randomVariation <= 99.6) {
              block.setPermutation(BlockPermutation.resolve("minecraft:air"));
              block.dimension.runCommandAsync(`structure load huge_mosshroom_06 ${blockLocation.x - 2} ${blockLocation.y} ${blockLocation.z - 2}`);
            }
            else return;
          }
          else return;
        });
      }
      else return;
    }
  });
})
