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:corrupted_grass_block",
    "nicothekid:glowcelium_sprouts_tall_block",
    "nicothekid:hanging_moss_block",
    "nicothekid:lush_grass_small_block",
    "nicothekid:lush_grass_tall_block",
    "nicothekid:moss_sprouts_small_block",
    "nicothekid:moss_sprouts_tall_block",
    "nicothekid:mycelium_sprouts_small_block",
    "nicothekid:mycelium_sprouts_tall_block",
    "nicothekid:tip_grass_blue_block",
    "nicothekid:tip_grass_light_blue_block",
    "nicothekid:tip_grass_magenta_block",
    "nicothekid:tip_grass_orange_block",
    "nicothekid:tip_grass_pink_block",
    "nicothekid:tip_grass_red_block",
    "nicothekid:tip_grass_white_block",
    "nicothekid:tip_grass_yellow_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:lush_grass_tall_block") {
        if (randomChance <= 10.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: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:corrupted_grass_block") {
          const spawnCorruptedGrass = new ItemStack ("nicothekid:corrupted_grass", 1);
          block.dimension.spawnItem(spawnCorruptedGrass, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        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:hanging_moss_block") {
          const spawnHangingMoss = new ItemStack ("nicothekid:hanging_moss", 1);
          block.dimension.spawnItem(spawnHangingMoss, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:lush_grass_small_block") {
          const spawnLushGrassSmall = new ItemStack ("nicothekid:small_lush_grass", 1);
          block.dimension.spawnItem(spawnLushGrassSmall, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:lush_grass_tall_block") {
          const spawnLushGrassTall = new ItemStack ("nicothekid:tall_lush_grass", 1);
          block.dimension.spawnItem(spawnLushGrassTall, { 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 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_blue_block") {
          const spawnTipGrassBlue = new ItemStack ("nicothekid:tip_grass_blue", 1);
          block.dimension.spawnItem(spawnTipGrassBlue, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_light_blue_block") {
          const spawnTipGrassLightBlue = new ItemStack ("nicothekid:tip_grass_light_blue", 1);
          block.dimension.spawnItem(spawnTipGrassLightBlue, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_magenta_block") {
          const spawnTipGrassMagenta = new ItemStack ("nicothekid:tip_grass_magenta", 1);
          block.dimension.spawnItem(spawnTipGrassMagenta, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_orange_block") {
          const spawnTipGrassOrange = new ItemStack ("nicothekid:tip_grass_orange", 1);
          block.dimension.spawnItem(spawnTipGrassOrange, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_pink_block") {
          const spawnTipGrassPink = new ItemStack ("nicothekid:tip_grass_pink", 1);
          block.dimension.spawnItem(spawnTipGrassPink, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_red_block") {
          const spawnTipGrassRed = new ItemStack ("nicothekid:tip_grass_red", 1);
          block.dimension.spawnItem(spawnTipGrassRed, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_white_block") {
          const spawnTipGrassWhite = new ItemStack ("nicothekid:tip_grass_white", 1);
          block.dimension.spawnItem(spawnTipGrassWhite, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
        if (brokenBlock.type.id === "nicothekid:tip_grass_yellow_block") {
          const spawnTipGrassYellow = new ItemStack ("nicothekid:tip_grass_yellow", 1);
          block.dimension.spawnItem(spawnTipGrassYellow, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        }
      }
      else {
        const itemEnchantable = itemUsed.getComponent("enchantable");
        if (itemEnchantable) {
          function lushGrassTallDrop () {
            const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
            if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
              if (fortuneEnchanted.level == 1) {
                if (randomChance <= 13.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 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 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 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 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:lush_grass_tall_block") {
            lushGrassTallDrop ();
          }
          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:lush_grass_tall_block") {
            if (randomChance <= 10.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: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;
});
