import { world, system, BlockPermutation, 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 statePillarAxis = block.permutation.getState("pillar_axis");
  const stateAll = block.permutation.getAllStates();
  const strippedWoods = [
    "minecraft:stripped_acacia_log",
    "minecraft:stripped_acacia_wood",
    "minecraft:stripped_bamboo_block",
    "minecraft:stripped_birch_log",
    "minecraft:stripped_birch_wood",
    "minecraft:stripped_cherry_log",
    "minecraft:stripped_cherry_wood",
    "minecraft:stripped_crimson_hyphae",
    "minecraft:stripped_crimson_stem",
    "minecraft:stripped_dark_oak_log",
    "minecraft:stripped_dark_oak_wood",
    "minecraft:stripped_jungle_log",
    "minecraft:stripped_jungle_wood",
    "minecraft:stripped_mangrove_log",
    "minecraft:stripped_mangrove_wood",
    "minecraft:stripped_oak_log",
    "minecraft:stripped_oak_wood",
    "minecraft:stripped_spruce_log",
    "minecraft:stripped_spruce_wood",
    "minecraft:stripped_warped_hyphae",
    "minecraft:stripped_warped_stem",
    "nicothekid:corrupted_log_stripped",
    "nicothekid:corrupted_wood_stripped",
    "nicothekid:mosshroom_hyphae_stripped",
    "nicothekid:mosshroom_stem_stripped",
    "nicothekid:mossy_log_stripped",
    "nicothekid:mossy_wood_stripped",
    "nicothekid:overgrown_jungle_log_stripped",
    "nicothekid:overgrown_jungle_wood_stripped"
  ]
  function decreaseItem () {
    if (!player.matches({ gameMode: GameMode.creative })) {
      const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
      playerEquippable.setEquipment("Mainhand", decrementItem)
    }
  }
  if (itemUsed === undefined) return;
  if (strippedWoods.includes(block.typeId)) {
    if (itemUsed.typeId === "minecraft:bone_meal") {
      system.run(() => {
        decreaseItem ();
        block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
        block.dimension.spawnParticle("nicothekid:barked_2_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
        if (block.permutation.matches("minecraft:stripped_acacia_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_acacia_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_bamboo_block")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:bamboo_block", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("nicothekid:corrupted_log_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:corrupted_log", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:corrupted_wood_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:corrupted_wood", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:mosshroom_hyphae_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:mosshroom_hyphae", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:mosshroom_stem_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:mosshroom_stem", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:mossy_log_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:mossy_log", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:mossy_wood_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:mossy_wood", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:overgrown_jungle_log_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:overgrown_jungle_log", stateAll ));
          return;
        }
        if (block.permutation.matches("nicothekid:overgrown_jungle_wood_stripped")) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:overgrown_jungle_wood", stateAll ));
          return;
        }
      });
    }
    else return;
  }
  else return;
});
