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

world.beforeEvents.itemUseOn.subscribe(eventData => {
  const player = eventData.source;
  const itemUsed = eventData.itemStack;
  const block = eventData.block;
  const blockLocation = block.location;
  const playerEquippable = player.getComponent("equippable");
  const stateAll = block.permutation.getAllStates();
  const customAxes = [
    "nicothekid:brass_axe",
    "nicothekid:bronze_axe",
    "nicothekid:olivine_axe",
    "nicothekid:tin_axe",
    "nicothekid:zinc_axe"
  ]
  const customHoes = [
    "nicothekid:brass_hoe",
    "nicothekid:bronze_hoe",
    "nicothekid:olivine_hoe",
    "nicothekid:tin_hoe",
    "nicothekid:zinc_hoe"
  ]
  const customShovels = [
    "nicothekid:brass_shovel",
    "nicothekid:bronze_shovel",
    "nicothekid:olivine_shovel",
    "nicothekid:tin_shovel",
    "nicothekid:zinc_shovel"
  ]
  function damageDurability () {
    if (!player.matches({ gameMode: GameMode.creative })) {
      const itemEnchantable = itemUsed.getComponent("minecraft:enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability;
      const itemDurabilityDamage = itemDurability.damage;
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 1;
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        player.playSound("random.break", { pitch: 0.9, location: player.location, volume: 1 });
        playerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        playerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
  }
  if (itemUsed === undefined) return;
  if (customAxes.includes(itemUsed.typeId) || customHoes.includes(itemUsed.typeId) || customShovels.includes(itemUsed.typeId)) {
    if (customAxes.includes(itemUsed.typeId)) {
      const vanillaWoods = [
        "minecraft:oak_log",
        "minecraft:oak_wood",
        "minecraft:spruce_log",
        "minecraft:spruce_wood",
        "minecraft:birch_log",
        "minecraft:birch_wood",
        "minecraft:jungle_log",
        "minecraft:jungle_wood",
        "minecraft:acacia_log",
        "minecraft:acacia_wood",
        "minecraft:dark_oak_log",
        "minecraft:dark_oak_wood",
        "minecraft:mangrove_log",
        "minecraft:mangrove_wood",
        "minecraft:cherry_log",
        "minecraft:cherry_wood",
        "minecraft:bamboo_block",
        "minecraft:crimson_stem",
        "minecraft:crimson_hyphae",
        "minecraft:warped_stem",
        "minecraft:warped_hyphae"
      ];
      const vanillaCopper = [
        "minecraft:exposed_chiseled_copper",
        "minecraft:exposed_copper",
        "minecraft:exposed_copper_bulb",
        "minecraft:exposed_copper_door",
        "minecraft:exposed_copper_grate",
        "minecraft:exposed_copper_trapdoor",
        "minecraft:exposed_cut_copper",
        "minecraft:exposed_cut_copper_stairs",
        "minecraft:exposed_cut_copper_slab",
        "minecraft:exposed_double_cut_copper_slab",
        "minecraft:weathered_chiseled_copper",
        "minecraft:weathered_copper",
        "minecraft:weathered_copper_bulb",
        "minecraft:weathered_copper_door",
        "minecraft:weathered_copper_grate",
        "minecraft:weathered_copper_trapdoor",
        "minecraft:weathered_cut_copper",
        "minecraft:weathered_cut_copper_stairs",
        "minecraft:weathered_cut_copper_slab",
        "minecraft:weathered_double_cut_copper_slab",
        "minecraft:oxidized_chiseled_copper",
        "minecraft:oxidized_copper",
        "minecraft:oxidized_copper_bulb",
        "minecraft:oxidized_copper_door",
        "minecraft:oxidized_copper_grate",
        "minecraft:oxidized_copper_trapdoor",
        "minecraft:oxidized_cut_copper",
        "minecraft:oxidized_cut_copper_stairs",
        "minecraft:oxidized_cut_copper_slab",
        "minecraft:oxidized_double_cut_copper_slab"
      ];
      const vanillaWaxedCopper = [
        "minecraft:waxed_copper",
        "minecraft:waxed_chiseled_copper",
        "minecraft:waxed_copper_bulb",
        "minecraft:waxed_copper_door",
        "minecraft:waxed_copper_grate",
        "minecraft:waxed_copper_trapdoor",
        "minecraft:waxed_cut_copper",
        "minecraft:waxed_cut_copper_stairs",
        "minecraft:waxed_cut_copper_slab",
        "minecraft:waxed_double_cut_copper_slab",
        
        "minecraft:waxed_exposed_copper",
        "minecraft:waxed_exposed_chiseled_copper",
        "minecraft:waxed_exposed_copper_bulb",
        "minecraft:waxed_exposed_copper_door",
        "minecraft:waxed_exposed_copper_grate",
        "minecraft:waxed_exposed_copper_trapdoor",
        "minecraft:waxed_exposed_cut_copper",
        "minecraft:waxed_exposed_cut_copper_stairs",
        "minecraft:waxed_exposed_cut_copper_slab",
        "minecraft:waxed_exposed_double_cut_copper_slab",
        
        "minecraft:waxed_weathered_copper",
        "minecraft:waxed_weathered_chiseled_copper",
        "minecraft:waxed_weathered_copper_bulb",
        "minecraft:waxed_weathered_copper_door",
        "minecraft:waxed_weathered_copper_grate",
        "minecraft:waxed_weathered_copper_trapdoor",
        "minecraft:waxed_weathered_cut_copper",
        "minecraft:waxed_weathered_cut_copper_stairs",
        "minecraft:waxed_weathered_cut_copper_slab",
        "minecraft:waxed_weathered_double_cut_copper_slab",
        
        "minecraft:waxed_oxidized_copper",
        "minecraft:waxed_oxidized_chiseled_copper",
        "minecraft:waxed_oxidized_copper_bulb",
        "minecraft:waxed_oxidized_copper_door",
        "minecraft:waxed_oxidized_copper_grate",
        "minecraft:waxed_oxidized_copper_trapdoor",
        "minecraft:waxed_oxidized_cut_copper",
        "minecraft:waxed_oxidized_cut_copper_stairs",
        "minecraft:waxed_oxidized_cut_copper_slab",
        "minecraft:waxed_oxidized_double_cut_copper_slab"
      ];
      if (vanillaWoods.includes(block.typeId)) {
        system.run(() => {
          const statePillarAxis = block.permutation.getState("pillar_axis");
          damageDurability ();
          if (block.permutation.matches("minecraft:oak_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_oak_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:oak_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_oak_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:spruce_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_spruce_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:spruce_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_spruce_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:birch_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_birch_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:birch_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_birch_wood", { "pillar_axis": statePillarAxis } ));            
          }
          if (block.permutation.matches("minecraft:jungle_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_jungle_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:jungle_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_jungle_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:acacia_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_acacia_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:acacia_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_acacia_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:dark_oak_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_dark_oak_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:dark_oak_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_dark_oak_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:mangrove_log")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_mangrove_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:mangrove_wood")) {
            block.dimension.playSound("use.wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_mangrove_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:cherry_log")) {
            block.dimension.playSound("step.cherry_wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_cherry_log", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:cherry_wood")) {
            block.dimension.playSound("step.cherry_wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_cherry_wood", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:crimson_stem")) {
            block.dimension.playSound("use.stem", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_crimson_stem", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:crimson_hyphae")) {
            block.dimension.playSound("use.stem", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_crimson_hyphae", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:warped_stem")) {
            block.dimension.playSound("use.stem", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_warped_stem", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:warped_hyphae")) {
            block.dimension.playSound("use.stem", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_warped_hyphae", { "pillar_axis": statePillarAxis } ));
          }
          if (block.permutation.matches("minecraft:bamboo_block")) {
            block.dimension.playSound("step.bamboo_wood", blockLocation, { pitch: 0.8, volume: 1.0 });
            //block.setPermutation(BlockPermutation.resolve("minecraft:stripped_bamboo_block", { "pillar_axis": statePillarAxis } ));
          }
        });
      }
      /*if (vanillaCopper.includes(block.typeId)) {
        const pitchRange = Math.random() * (1.2 - 0.8) + 0.8;
        const vanillaCopperDoors = [
          "minecraft:exposed_copper_door",
          "minecraft:exposed_copper_trapdoor",
          "minecraft:weathered_copper_door",
          "minecraft:weathered_copper_trapdoor",
          "minecraft:oxidized_copper_door",
          "minecraft:oxidized_copper_trapdoor"
        ];
        if (vanillaCopperDoors.includes(block.typeId)) {
          system.run(() => {
            if (!player.isSneaking) return;
            damageDurability ();
            block.dimension.playSound("scrape", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.spawnParticle("nicothekid:copper_scrape_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            if (block.permutation.matches("minecraft:exposed_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:exposed_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:exposed_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_trapdoor", stateAll ));
            }
            if (block.permutation.matches("minecraft:weathered_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:weathered_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:weathered_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_trapdoor", stateAll ));
            }
            if (block.permutation.matches("minecraft:oxidized_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:oxidized_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:oxidized_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_trapdoor", stateAll ));
            }
          });
        }
        else {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("scrape", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.spawnParticle("nicothekid:copper_scrape_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            if (block.permutation.matches("minecraft:exposed_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:exposed_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_block"));
            }
            if (block.permutation.matches("minecraft:exposed_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:exposed_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_grate"));
            }
            if (block.permutation.matches("minecraft:exposed_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper"));
            }
            if (block.permutation.matches("minecraft:exposed_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:exposed_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:exposed_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:double_cut_copper_slab"));
            }
            if (block.permutation.matches("minecraft:weathered_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:weathered_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper"));
            }
            if (block.permutation.matches("minecraft:weathered_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:weathered_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_grate"));
            }
            if (block.permutation.matches("minecraft:weathered_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper"));
            }
            if (block.permutation.matches("minecraft:weathered_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:weathered_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:weathered_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_double_cut_copper_slab"));
            }
            if (block.permutation.matches("minecraft:oxidized_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:oxidized_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper"));
            }
            if (block.permutation.matches("minecraft:oxidized_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:oxidized_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_grate"));
            }
            if (block.permutation.matches("minecraft:oxidized_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper"));
            }
            if (block.permutation.matches("minecraft:oxidized_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:oxidized_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:oxidized_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_double_cut_copper_slab"));
            }
          });
        }
      }
      if (vanillaWaxedCopper.includes(block.typeId)) {
        const pitchRange = Math.random() * (1.2 - 0.8) + 0.8;
        const vanillaWaxedCopperDoors = [
          "minecraft:waxed_copper_door",
          "minecraft:waxed_copper_trapdoor",
          "minecraft:waxed_exposed_copper_door",
          "minecraft:waxed_exposed_copper_trapdoor",
          "minecraft:waxed_weathered_copper_door",
          "minecraft:waxed_weathered_copper_trapdoor",
          "minecraft:waxed_oxidized_copper_door",
          "minecraft:waxed_oxidized_copper_trapdoor"
        ];
        if (vanillaWaxedCopperDoors.includes(block.typeId)) {
          system.run(() => {
            if (!player.isSneaking) return;
            damageDurability ();
            block.dimension.playSound("copper.wax.off", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.spawnParticle("nicothekid:copper_wax_off_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            if (block.permutation.matches("minecraft:waxed_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_trapdoor", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_trapdoor", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_trapdoor", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper_door", { "upper_block_bit": false })) {
              const blockAbove = block.above();
              const blockAboveAllState = blockAbove.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_door", stateAll ));
              blockAbove.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_door", blockAboveAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper_door", { "upper_block_bit": true })) {
              const blockBelow = block.below();
              const blockBelowAllState = blockBelow.permutation.getAllStates();
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_door", stateAll ));
              blockBelow.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_door", blockBelowAllState ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper_trapdoor")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_trapdoor", stateAll ));
            }
          });
        }
        else {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("copper.wax.off", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.spawnParticle("nicothekid:copper_wax_off_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            if (block.permutation.matches("minecraft:waxed_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_block"));
            }
            if (block.permutation.matches("minecraft:waxed_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:copper_grate"));
            }
            if (block.permutation.matches("minecraft:waxed_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:double_cut_copper_slab"));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_copper_grate"));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_exposed_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:exposed_double_cut_copper_slab"));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_copper_grate"));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_weathered_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:weathered_double_cut_copper_slab"));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_chiseled_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_chiseled_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper_bulb")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_bulb", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_copper_grate")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_copper_grate"));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_cut_copper")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_cut_copper"));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_cut_copper_stairs")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_cut_copper_stairs", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_cut_copper_slab", stateAll ));
            }
            if (block.permutation.matches("minecraft:waxed_oxidized_double_cut_copper_slab")) {
              block.setPermutation(BlockPermutation.resolve("minecraft:oxidized_double_cut_copper_slab"));
            }
          });
        }
      }*/
    }
    if (customHoes.includes(itemUsed.typeId)) {
      const vanillaFarmSoil = [
        "minecraft:coarse_dirt",
        "minecraft:dirt",
        "minecraft:dirt_with_roots",
        "minecraft:grass_block",
        "minecraft:grass_path"
      ];
      if (vanillaFarmSoil.includes(block.typeId)) {
        system.run(() => {
          const blockAbove = block.dimension.getBlock(blockLocation).above(1);
          if (!blockAbove.isAir) return;
          damageDurability ();
          block.dimension.playSound("use.gravel", blockLocation, { pitch: 0.8, volume: 1.0 });
          /*if (block.permutation.matches("minecraft:dirt", { dirt_type: "normal" })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:farmland"));
          }
          if (block.permutation.matches("minecraft:dirt", { dirt_type: "coarse" })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:dirt", { dirt_type: "normal" }));
          }
          if (block.permutation.matches("minecraft:dirt_with_roots")) {
            const spawnHangingRoots = new ItemStack("minecraft:hanging_roots", 1);
            block.dimension.spawnItem(spawnHangingRoots, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:dirt", { dirt_type: "normal" }));
          }
          if (block.permutation.matches("minecraft:grass_block")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:farmland"));
          }
          if (block.permutation.matches("minecraft:grass_path")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:farmland"));
          }*/
        });
      }
    }
    if (customShovels.includes(itemUsed.typeId)) {
      const vanillaCampfire = [
        "minecraft:campfire",
        "minecraft:soul_campfire"
      ];
      const vanillaPathSoil = [
        "minecraft:coarse_dirt",
        "minecraft:dirt",
        "minecraft:dirt_with_roots",
        "minecraft:grass_block",
        "minecraft:mycelium",
        "minecraft:podzol"
      ];
      if (vanillaCampfire.includes(block.typeId)) {
        system.run(() => {
          const blockState = block.permutation.getState("minecraft:cardinal_direction");
          const pitchRange = Math.random() * (2.4 - 1.8) + 1.8;
          damageDurability ();
          block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
          if (block.permutation.matches("minecraft:campfire", { "extinguished": false, "minecraft:cardinal_direction": blockState })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:campfire", { "extinguished": true, "minecraft:cardinal_direction": blockState }));
          }
          if (block.permutation.matches("minecraft:soul_campfire", { "extinguished": false, "minecraft:cardinal_direction": blockState })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:soul_campfire", { "extinguished": true, "minecraft:cardinal_direction": blockState }));
          }
        });
      }
      if (vanillaPathSoil.includes(block.typeId)) {
        system.run(() => {
          const blockAbove = block.dimension.getBlock(blockLocation).above(1);
          if (!blockAbove.isAir) return;
          damageDurability ();
          block.dimension.playSound("use.grass", blockLocation, { pitch: 0.8, volume: 1.0 });
          if (block.permutation.matches("minecraft:dirt", { dirt_type: "normal" })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }
          if (block.permutation.matches("minecraft:dirt", { dirt_type: "coarse" })) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }
          if (block.permutation.matches("minecraft:dirt_with_roots")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }
          /*if (block.permutation.matches("minecraft:grass_block")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }*/
          if (block.permutation.matches("minecraft:mycelium")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }
          if (block.permutation.matches("minecraft:podzol")) {
            block.setPermutation(BlockPermutation.resolve("minecraft:grass_path"));
          }
        });
      }
      if (block.typeId === "minecraft:snow_layer") {
        system.run(() => {
          const pitchRange = Math.random() * (1.0 - 0.8) + 0.8;
          damageDurability ();
          block.dimension.playSound("dig.snow", blockLocation, { pitch: pitchRange, volume: 1.0 });
          if (block.permutation.matches("minecraft:snow_layer", { height: 0 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 1);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 1 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 1);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 2 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 1);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 3 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 2);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 4 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 2);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 5 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 3);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 6 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 3);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
          if (block.permutation.matches("minecraft:snow_layer", { height: 7 })) {
            const spawnSnowball = new ItemStack("minecraft:snowball", 4);
            block.dimension.spawnItem(spawnSnowball, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          }
        });
      }
    }
  }
  else return;
});