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

world.afterEvents.playerBreakBlock.subscribe(eventData => {
  const player = eventData.player;
  const itemUsed = eventData.itemStackBeforeBreak;
  const brokenBlock = eventData.brokenBlockPermutation;
  const block = eventData.block;
  const blockLocation = eventData.block.location;
  const customLeaves = [
    "nicothekid:corrupted_leaves",
    "nicothekid:mossy_leaves",
    "nicothekid:overgrown_jungle_leaves"
  ]
  let randomChance = Math.random() * (100.0 - 0.0) + 0.0;
  if (player.matches({ gameMode: GameMode.creative })) return;
  if (customLeaves.includes(brokenBlock.type.id)) {
    if (itemUsed === undefined) {
      if (brokenBlock.type.id === "nicothekid:corrupted_leaves") {
        if (randomChance <= 10.0) {
          const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
          block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else if (randomChance <= 15.0) {
          const spawnStick = new ItemStack ("minecraft:stick", 1);
          block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else return;
      }
      if (brokenBlock.type.id === "nicothekid:mossy_leaves") {
        if (randomChance <= 10.0) {
          const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
          block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else if (randomChance <= 15.0) {
          const spawnStick = new ItemStack ("minecraft:stick", 1);
          block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else return;
      }
      if (brokenBlock.type.id === "nicothekid:overgrown_jungle_leaves") {
        if (randomChance <= 10.0) {
          const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
          block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else if (randomChance <= 15.0) {
          const spawnStick = new ItemStack ("minecraft:stick", 1);
          block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else return;
      }
    }
    else {
      if (itemUsed.typeId === "minecraft:shears") {
        if (brokenBlock.type.id === "nicothekid:corrupted_leaves") {
          const spawnCorruptedLeaves = new ItemStack ("nicothekid:corrupted_leaves", 1);
          block.dimension.spawnItem(spawnCorruptedLeaves, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:mossy_leaves") {
          const spawnMossyLeaves = new ItemStack ("nicothekid:mossy_leaves", 1);
          block.dimension.spawnItem(spawnMossyLeaves, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:overgrown_jungle_leaves") {
          const spawnOvergrownJungleLeaves = new ItemStack ("nicothekid:overgrown_jungle_leaves", 1);
          block.dimension.spawnItem(spawnOvergrownJungleLeaves, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
      }
      else {
        const itemEnchantable = itemUsed.getComponent("enchantable");
        if (itemEnchantable) {
          function corruptedLeavesDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.0) {
                  const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 20.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 2) {
                if (randomChance <= 15.0) {
                  const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 25.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 3) {
                if (randomChance <= 20.0) {
                  const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 33.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
            }
            else {
              if (randomChance <= 10.0) {
                const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
                block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else if (randomChance <= 15.0) {
                const spawnStick = new ItemStack ("minecraft:stick", 1);
                block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else return;
            }
          }
          function mossyLeavesDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.0) {
                  const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 20.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 2) {
                if (randomChance <= 15.0) {
                  const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 25.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 3) {
                if (randomChance <= 20.0) {
                  const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 33.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
            }
            else {
              if (randomChance <= 10.0) {
                const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
                block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else if (randomChance <= 15.0) {
                const spawnStick = new ItemStack ("minecraft:stick", 1);
                block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else return;
            }
          }
          function overgrownJungleLeavesDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.0) {
                  const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 20.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 2) {
                if (randomChance <= 15.0) {
                  const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 25.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
              if (fortuneEnchanted.level == 3) {
                if (randomChance <= 20.0) {
                  const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
                  block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else if (randomChance <= 33.0) {
                  const spawnStick = new ItemStack ("minecraft:stick", 1);
                  block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
            }
            else {
              if (randomChance <= 10.0) {
                const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
                block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else if (randomChance <= 15.0) {
                const spawnStick = new ItemStack ("minecraft:stick", 1);
                block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else return;
            }
          }
          const silkTouchEnchanted = itemEnchantable.getEnchantment("silk_touch");
          if (silkTouchEnchanted) return;
          if (brokenBlock.type.id === "nicothekid:corrupted_leaves") {
            corruptedLeavesDrop ();
          }
          if (brokenBlock.type.id === "nicothekid:mossy_leaves") {
            mossyLeavesDrop ();
          }
          if (brokenBlock.type.id === "nicothekid:overgrown_jungle_leaves") {
            overgrownJungleLeavesDrop ();
          }
        }
        else {
          if (brokenBlock.type.id === "nicothekid:corrupted_leaves") {
            if (randomChance <= 10.0) {
              const spawnSapling = new ItemStack ("nicothekid:blooming_corrupted_sapling", 1);
              block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else if (randomChance <= 15.0) {
              const spawnStick = new ItemStack ("minecraft:stick", 1);
              block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else return;
          }
          if (brokenBlock.type.id === "nicothekid:mossy_leaves") {
            if (randomChance <= 10.0) {
              const spawnSapling = new ItemStack ("nicothekid:mossy_plant", 1);
              block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else if (randomChance <= 15.0) {
              const spawnStick = new ItemStack ("minecraft:stick", 1);
              block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else return;
          }
          if (brokenBlock.type.id === "nicothekid:overgrown_jungle_leaves") {
            if (randomChance <= 10.0) {
              const spawnSapling = new ItemStack ("nicothekid:overgrown_jungle_plant", 1);
              block.dimension.spawnItem(spawnSapling, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else if (randomChance <= 15.0) {
              const spawnStick = new ItemStack ("minecraft:stick", 1);
              block.dimension.spawnItem(spawnStick, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else return;
          }
        }
      }
    }
  }
  else return;
});