import { world, system, BlockPermutation, ItemStack, Dimension, 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 customPlants = [
    "nicothekid:glowcelium_sprouts_tall_block",
    "nicothekid:moss_sprouts_small_block",
    "nicothekid:moss_sprouts_tall_block",
    "nicothekid:mycelium_sprouts_small_block",
    "nicothekid:mycelium_sprouts_tall_block"
  ]
  let randomChance = Math.random() * (100.0 - 0.0) + 0.0;
  if (player.matches({ gameMode: GameMode.creative })) return;
  if (customPlants.includes(brokenBlock.type.id)) {
    if (itemUsed === undefined) {
      if (brokenBlock.type.id === "nicothekid:moss_sprouts_tall_block") {
        if (randomChance <= 5.0) {
          const spawnSmallMossSprouts = new ItemStack ("nicothekid:moss_sprouts_tall", 1);
          block.dimension.spawnItem(spawnSmallMossSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        else return;
      }
      if (brokenBlock.type.id === "nicothekid:mycelium_sprouts_tall_block") {
        if (randomChance <= 5.0) {
          const spawnSmallMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_tall", 1);
          block.dimension.spawnItem(spawnSmallMyceliumSprouts, { 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:glowcelium_sprouts_tall_block") {
          const spawnGlowceliumSproutsTall = new ItemStack ("nicothekid:glowcelium_sprouts_tall", 1);
          block.dimension.spawnItem(spawnGlowceliumSproutsTall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:moss_sprouts_small_block") {
          const spawnMossSproutsSmall = new ItemStack ("nicothekid:moss_sprouts_small", 1);
          block.dimension.spawnItem(spawnMossSproutsSmall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:moss_sprouts_tall_block") {
          const spawnMossSproutsTall = new ItemStack ("nicothekid:moss_sprouts_tall", 1);
          block.dimension.spawnItem(spawnMossSproutsTall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:mycelium_sprouts_small_block") {
          const spawnMyceliumSproutsSmall = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
          block.dimension.spawnItem(spawnMyceliumSproutsSmall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:mycelium_sprouts_tall_block") {
          const spawnMyceliumSproutsTall = new ItemStack ("nicothekid:mycelium_sprouts_tall", 1);
          block.dimension.spawnItem(spawnMyceliumSproutsTall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
      }
      else {
        const itemEnchantable = itemUsed.getComponent("enchantable");
        if (itemEnchantable) {
          function mossSproutsTallDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.0) {
                  const spawnMossSprouts = new ItemStack ("nicothekid:moss_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMossSprouts, { 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 spawnMossSprouts = new ItemStack ("nicothekid:moss_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMossSprouts, { 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 spawnMossSprouts = new ItemStack ("nicothekid:moss_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMossSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
            }
            else {
              if (randomChance <= 10.0) {
                const spawnMossSprouts = new ItemStack ("nicothekid:moss_sprouts_small", 1);
                block.dimension.spawnItem(spawnMossSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              }
              else return;
            }
          }
          function myceliumSproutsTallDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.0) {
                  const spawnMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMyceliumSprouts, { 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 spawnMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMyceliumSprouts, { 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 spawnMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
                  block.dimension.spawnItem(spawnMyceliumSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
                }
                else return;
              }
            }
            else {
              if (randomChance <= 10.0) {
                const spawnMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
                block.dimension.spawnItem(spawnMyceliumSprouts, { 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:moss_sprouts_tall_block") {
            mossSproutsTallDrop ();
          }
          if (brokenBlock.type.id === "nicothekid:mycelium_sprouts_tall_block") {
            myceliumSproutsTallDrop ();
          }
        }
        else {
          if (brokenBlock.type.id === "nicothekid:moss_sprouts_tall_block") {
            if (randomChance <= 10.0) {
              const spawnMossSprouts = new ItemStack ("nicothekid:moss_sprouts_small", 1);
              block.dimension.spawnItem(spawnMossSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else return;
          }
          if (brokenBlock.type.id === "nicothekid:mycelium_sprouts_tall_block") {
            if (randomChance <= 10.0) {
              const spawnMyceliumSprouts = new ItemStack ("nicothekid:mycelium_sprouts_small", 1);
              block.dimension.spawnItem(spawnMyceliumSprouts, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            }
            else return;
          }
        }
      }
    }
  }
  else return;
});
