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 = block.location;
  const customMinerals = [
    "nicothekid:deepslate_fossil",
    "nicothekid:deepslate_prismarine_ore",
    "nicothekid:deepslate_tin_ore",
    "nicothekid:deepslate_zinc_ore",
    "nicothekid:heated_netherrack_fossil",
    "nicothekid:heated_netherrack_olivine_ore",
    "nicothekid:netherrack_diamond_ore",
    "nicothekid:netherrack_fossil",
    "nicothekid:netherrack_olivine_ore",
    "nicothekid:prismarine_ore",
    "nicothekid:soul_sand_echo_shard_ore",
    "nicothekid:soul_sand_fossil",
    "nicothekid:soul_soil_fossil",
    "nicothekid:stone_fossil",
    "nicothekid:tin_ore",
    "nicothekid:zinc_ore"
  ]
  const customPickaxes = [
    "nicothekid:brass_pickaxe",
    "nicothekid:bronze_pickaxe",
    "nicothekid:olivine_pickaxe",
    "nicothekid:tin_pickaxe",
    "nicothekid:zinc_pickaxe"
  ]
  const customShovels = [
    "nicothekid:brass_shovel",
    "nicothekid:bronze_shovel",
    "nicothekid:olivine_shovel",
    "nicothekid:tin_shovel",
    "nicothekid:zinc_shovel"
  ]
  function spawnXpOrb () {
    const silkTouchEnchanted = itemUsed.getComponent("enchantable").getEnchantment("silk_touch");
    if (!silkTouchEnchanted) {
      block.dimension.spawnEntity("minecraft:xp_orb", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnBone () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const itemBone = new ItemStack ("minecraft:bone", fortuneChance);
        block.dimension.spawnItem(itemBone, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (3 - 1) + 1;
        const itemBone = new ItemStack ("minecraft:bone", fortuneChance);
        block.dimension.spawnItem(itemBone, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (3 - 2) + 2;
        const itemBone = new ItemStack ("minecraft:bone", fortuneChance);
        block.dimension.spawnItem(itemBone, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const itemBone = new ItemStack ("minecraft:bone", 1);
      block.dimension.spawnItem(itemBone, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnDiamond () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:diamond", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:diamond", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (3 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:diamond", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const rawDiamond = new ItemStack ("minecraft:diamond", 1);
      block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnEchoShard () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:echo_shard", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:echo_shard", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (3 - 1) + 1;
        const rawDiamond = new ItemStack ("minecraft:echo_shard", fortuneChance);
        block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const rawDiamond = new ItemStack ("minecraft:echo_shard", 1);
      block.dimension.spawnItem(rawDiamond, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnOlivineFragment () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (3 - 2) + 2;
        const olivineFragment = new ItemStack ("nicothekid:olivine_fragment", fortuneChance);
        block.dimension.spawnItem(olivineFragment, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (4 - 2) + 2;
        const olivineFragment = new ItemStack ("nicothekid:olivine_fragment", fortuneChance);
        block.dimension.spawnItem(olivineFragment, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (4 - 3) + 3;
        const olivineFragment = new ItemStack ("nicothekid:olivine_fragment", fortuneChance);
        block.dimension.spawnItem(olivineFragment, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      let amountChance = Math.random() * (2 - 1) + 1;
      const olivineFragment = new ItemStack ("nicothekid:olivine_fragment", amountChance);
      block.dimension.spawnItem(olivineFragment, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnPrismarineShard () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (3 - 2) + 2;
        const prismarineShard = new ItemStack ("minecraft:prismarine_shard", fortuneChance);
        block.dimension.spawnItem(prismarineShard, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (4 - 3) + 3;
        const prismarineShard = new ItemStack ("minecraft:prismarine_shard", fortuneChance);
        block.dimension.spawnItem(prismarineShard, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (5 - 4) + 4;
        const prismarineShard = new ItemStack ("minecraft:prismarine_shard", fortuneChance);
        block.dimension.spawnItem(prismarineShard, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const prismarineShard = new ItemStack ("minecraft:prismarine_shard", 2);
      block.dimension.spawnItem(prismarineShard, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnRawTin () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawTin = new ItemStack ("nicothekid:raw_tin", fortuneChance);
        block.dimension.spawnItem(rawTin, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (3 - 1) + 1;
        const rawTin = new ItemStack ("nicothekid:raw_tin", fortuneChance);
        block.dimension.spawnItem(rawTin, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (3 - 2) + 2;
        const rawTin = new ItemStack ("nicothekid:raw_tin", fortuneChance);
        block.dimension.spawnItem(rawTin, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const rawTin = new ItemStack ("nicothekid:raw_tin", 1);
      block.dimension.spawnItem(rawTin, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  function spawnRawZinc () {
    const fortuneEnchanted = itemUsed.getComponent("enchantable").getEnchantment("fortune");
    if (itemUsed.getComponent("enchantable").hasEnchantment("fortune")) {
      if (fortuneEnchanted.level == 1) {
        let fortuneChance = Math.random() * (2 - 1) + 1;
        const rawZinc = new ItemStack ("nicothekid:raw_zinc", fortuneChance);
        block.dimension.spawnItem(rawZinc, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 2) {
        let fortuneChance = Math.random() * (3 - 1) + 1;
        const rawZinc = new ItemStack ("nicothekid:raw_zinc", fortuneChance);
        block.dimension.spawnItem(rawZinc, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
      if (fortuneEnchanted.level == 3) {
        let fortuneChance = Math.random() * (3 - 2) + 2;
        const rawZinc = new ItemStack ("nicothekid:raw_zinc", fortuneChance);
        block.dimension.spawnItem(rawZinc, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
      }
    }
    else {
      const rawZinc = new ItemStack ("nicothekid:raw_zinc", 1);
      block.dimension.spawnItem(rawZinc, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
    }
  }
  if (itemUsed === undefined || player.matches({ gameMode: GameMode.creative })) return;
  if (customMinerals.includes(brokenBlock.type.id)) {
    const itemEnchantable = itemUsed.getComponent("enchantable");
    if (!itemEnchantable) return;
    const silkTouchEnchanted = itemEnchantable.getEnchantment("silk_touch");
    const fortuneEnchanted = itemEnchantable.getEnchantment("fortune");
    if (silkTouchEnchanted) return;
    if (customPickaxes.includes(itemUsed.typeId) || itemUsed.hasTag("minecraft:is_pickaxe")) {
      if (brokenBlock.type.id === "nicothekid:deepslate_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:deepslate_prismarine_ore") {
        spawnPrismarineShard ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:deepslate_tin_ore") {
        spawnRawTin ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:deepslate_zinc_ore") {
        spawnRawZinc ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:heated_netherrack_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:heated_netherrack_olivine_ore") {
        spawnOlivineFragment ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:netherrack_diamond_ore") {
        spawnDiamond ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:netherrack_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:netherrack_olivine_ore") {
        spawnOlivineFragment ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:prismarine_ore") {
        spawnPrismarineShard ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:stone_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:tin_ore") {
        spawnRawTin ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:zinc_ore") {
        spawnRawZinc ();
        spawnXpOrb ();
      }
    }
    if (customShovels.includes(itemUsed.typeId) || itemUsed.hasTag("minecraft:is_shovel")) {
      if (brokenBlock.type.id === "nicothekid:soul_sand_echo_shard_ore") {
        spawnEchoShard ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:soul_sand_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
      if (brokenBlock.type.id === "nicothekid:soul_soil_fossil") {
        spawnBone ();
        spawnXpOrb ();
      }
    }
  }
  else return;
});
