import { BlockLocation, world } from "@minecraft/server"
let ores = ["minecraft:iron_ore", "minecraft:lit_redstone_ore", "minecraft:redstone_ore", "minecraft:lit_deepslate_redstone_ore", "minecraft:deepslate_redstone_ore", "minecraft:deepslate_iron_ore", "minecraft:deepslate_diamond_ore", "minecraft:iron_ore", "minecraft:copper_ore", "minecraft:coal_ore", "minecraft:lapis_ore", "minecraft:diamond_ore", "minecraft:emerald_ore", "minecraft:ancient_debris", "minecraft:nether_gold_ore", "minecraft:deepslate_gold_ore", "minecraft:quartz_ore", "minecraft:deepslate_copper_ore", "minecraft:deepslate_lapis_ore", "minecraft:deepslate_coal_ore", "minecraft:deepslate_emerald_ore", "minecraft:gold_ore"]
let c = 0
world.events.blockBreak.subscribe(data => {
  if (data.player.isSneaking) return
  let id = data.brokenBlockPermutation.type.id
  if (!ores.includes(id)) return
  let item = data.player.getComponent("minecraft:inventory").container.getItem(data.player.selectedSlot)
  let vein_miner = item?.nameTag?.search("Vein Miner")
  if (vein_miner != undefined && vein_miner != -1) {
    let level = findLevel("Vein Miner", vein_miner, item)
    c = level * 3
    checkBlocksAroundPos(data.player.dimension, data.block.location, id)
  }
})
function checkBlocksAroundPos(dim, pos, id) {
  let offset = [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
    [-1, 0, 0],
    [0, -1, 0],
    [0, 0, -1]
  ]
  for (let i of offset) {
    let block = dim.getBlock(new BlockLocation(pos.x + i[0], pos.y + i[1], pos.z + i[2]))
    if (block.id == id && c > 0) {
      c -= 1
      setBlockAir(dim, pos.x + i[0], pos.y + i[1], pos.z + i[2])
      checkBlocksAroundPos(dim, { x: pos.x + i[0], y: pos.y + i[1], z: pos.z + i[2] }, id)
    }
  }
}