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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:jar_of_cookies_interaction", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockLocation = block.location;
      const playerEquippable = player.getComponent("equippable");
      const itemUsed = playerEquippable.getEquipment("Mainhand");
      const blockState = block.permutation.getState("minecraft:cardinal_direction");
      const blockAllState = block.permutation.getAllStates();
      function damageDurability () {
        if (!player.matches({ gameMode: GameMode.creative })) {
          const itemEnchantable = itemUsed.getComponent("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)
          }
        }
      }
      function decreaseItem () {
        system.run(() => {
          if (!player.matches({ gameMode: GameMode.creative })) {
            const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
            playerEquippable.setEquipment("Mainhand", decrementItem)
          }
        });
      }
      const cookieJars = [
        "nicothekid:block_jar_empty",
        "nicothekid:block_jar_cookies_01",
        "nicothekid:block_jar_cookies_02",
        "nicothekid:block_jar_cookies_03",
        "nicothekid:block_jar_cookies_04",
        "nicothekid:block_jar_cookies_05",
        "nicothekid:block_jar_cookies_06",
        "nicothekid:block_jar_cookies_07",
        "nicothekid:block_jar_cookies_08",
        "nicothekid:block_jar_glow_berry_cookies_01",
        "nicothekid:block_jar_glow_berry_cookies_02",
        "nicothekid:block_jar_glow_berry_cookies_03",
        "nicothekid:block_jar_glow_berry_cookies_04",
        "nicothekid:block_jar_glow_berry_cookies_05",
        "nicothekid:block_jar_glow_berry_cookies_06",
        "nicothekid:block_jar_glow_berry_cookies_07",
        "nicothekid:block_jar_glow_berry_cookies_08",
        "nicothekid:block_jar_moss_berry_cookies_01",
        "nicothekid:block_jar_moss_berry_cookies_02",
        "nicothekid:block_jar_moss_berry_cookies_03",
        "nicothekid:block_jar_moss_berry_cookies_04",
        "nicothekid:block_jar_moss_berry_cookies_05",
        "nicothekid:block_jar_moss_berry_cookies_06",
        "nicothekid:block_jar_moss_berry_cookies_07",
        "nicothekid:block_jar_moss_berry_cookies_08",
        "nicothekid:block_jar_sweet_berry_cookies_01",
        "nicothekid:block_jar_sweet_berry_cookies_02",
        "nicothekid:block_jar_sweet_berry_cookies_03",
        "nicothekid:block_jar_sweet_berry_cookies_04",
        "nicothekid:block_jar_sweet_berry_cookies_05",
        "nicothekid:block_jar_sweet_berry_cookies_06",
        "nicothekid:block_jar_sweet_berry_cookies_07",
        "nicothekid:block_jar_sweet_berry_cookies_08"
      ]
      if (player.isSneaking) return;
      if (cookieJars.includes(block.typeId)) {
        if (itemUsed === undefined) {
          const fullCookieJars = [
            "nicothekid:block_jar_cookies_08",
            "nicothekid:block_jar_glow_berry_cookies_08",
            "nicothekid:block_jar_moss_berry_cookies_08",
            "nicothekid:block_jar_sweet_berry_cookies_08"
          ]
          if (fullCookieJars.includes(block.typeId)) {
            player.playSound("game.player.attack.nodamage", { pitch: 1.1, location: blockLocation, volume: 1.0 });
            player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse the Jar Opener to take out one of the cookies.§r\"}]}`);
          }
          else return;
        }
        else if (itemUsed !== undefined) {
          const validCookies = [
            "minecraft:cookie",
            "nicothekid:glow_berry_cookie",
            "nicothekid:moss_berry_cookie",
            "nicothekid:sweet_berry_cookie"
          ]
          if (itemUsed.typeId === "nicothekid:jar_opener") {
            if (block.hasTag("jar_cookies_block")) {
              damageDurability ();
              block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              const spawnCookie = new ItemStack ("minecraft:cookie", 1);
              block.dimension.spawnItem(spawnCookie, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_08")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_07", blockAllState ));
                return;
              }
            }
            if (block.hasTag("jar_glow_berry_cookies_block")) {
              damageDurability ();
              block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              const spawnGlowBerryCookie = new ItemStack ("nicothekid:glow_berry_cookie", 1);
              block.dimension.spawnItem(spawnGlowBerryCookie, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_08")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_07", blockAllState ));
                return;
              }
            }
            if (block.hasTag("jar_moss_berry_cookies_block")) {
              damageDurability ();
              block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              const spawnMossBerryCookie = new ItemStack ("nicothekid:moss_berry_cookie", 1);
              block.dimension.spawnItem(spawnMossBerryCookie, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_08")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_07", blockAllState ));
                return;
              }
            }
            if (block.hasTag("jar_sweet_berry_cookies_block")) {
              damageDurability ();
              block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              const spawnSweetBerryCookie = new ItemStack ("nicothekid:sweet_berry_cookie", 1);
              block.dimension.spawnItem(spawnSweetBerryCookie, { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_08")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_07", blockAllState ));
                return;
              }
            }
          }
          else if (validCookies.includes(itemUsed.typeId)) {
            if (itemUsed.typeId === "minecraft:cookie") {
              const inputableJars = [
                "nicothekid:block_jar_empty",
                "nicothekid:block_jar_cookies_01",
                "nicothekid:block_jar_cookies_02",
                "nicothekid:block_jar_cookies_03",
                "nicothekid:block_jar_cookies_04",
                "nicothekid:block_jar_cookies_05",
                "nicothekid:block_jar_cookies_06",
                "nicothekid:block_jar_cookies_07"
              ];
              if (!inputableJars.includes(block.typeId)) return;
              decreaseItem ();
              block.dimension.playSound("block.itemframe.add_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_empty")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_07", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_cookies_08", blockAllState ));
                return;
              }
            }
            if (itemUsed.typeId === "nicothekid:glow_berry_cookie") {
              const inputableJars = [
                "nicothekid:block_jar_empty",
                "nicothekid:block_jar_glow_berry_cookies_01",
                "nicothekid:block_jar_glow_berry_cookies_02",
                "nicothekid:block_jar_glow_berry_cookies_03",
                "nicothekid:block_jar_glow_berry_cookies_04",
                "nicothekid:block_jar_glow_berry_cookies_05",
                "nicothekid:block_jar_glow_berry_cookies_06",
                "nicothekid:block_jar_glow_berry_cookies_07"
              ];
              if (!inputableJars.includes(block.typeId)) return;
              decreaseItem ();
              block.dimension.playSound("block.itemframe.add_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_empty")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_07", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_glow_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_cookies_08", blockAllState ));
                return;
              }
            }
            if (itemUsed.typeId === "nicothekid:moss_berry_cookie") {
              const inputableJars = [
                "nicothekid:block_jar_empty",
                "nicothekid:block_jar_moss_berry_cookies_01",
                "nicothekid:block_jar_moss_berry_cookies_02",
                "nicothekid:block_jar_moss_berry_cookies_03",
                "nicothekid:block_jar_moss_berry_cookies_04",
                "nicothekid:block_jar_moss_berry_cookies_05",
                "nicothekid:block_jar_moss_berry_cookies_06",
                "nicothekid:block_jar_moss_berry_cookies_07"
              ];
              if (!inputableJars.includes(block.typeId)) return;
              decreaseItem ();
              block.dimension.playSound("block.itemframe.add_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_empty")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_07", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_moss_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_moss_berry_cookies_08", blockAllState ));
                return;
              }
            }
            if (itemUsed.typeId === "nicothekid:sweet_berry_cookie") {
              const inputableJars = [
                "nicothekid:block_jar_empty",
                "nicothekid:block_jar_sweet_berry_cookies_01",
                "nicothekid:block_jar_sweet_berry_cookies_02",
                "nicothekid:block_jar_sweet_berry_cookies_03",
                "nicothekid:block_jar_sweet_berry_cookies_04",
                "nicothekid:block_jar_sweet_berry_cookies_05",
                "nicothekid:block_jar_sweet_berry_cookies_06",
                "nicothekid:block_jar_sweet_berry_cookies_07"
              ];
              if (!inputableJars.includes(block.typeId)) return;
              decreaseItem ();
              block.dimension.playSound("block.itemframe.add_item", blockLocation, { pitch: 1.0, volume: 0.5 });
              if (block.permutation.matches("nicothekid:block_jar_empty")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_01", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_01")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_02", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_02")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_03", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_03")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_04", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_04")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_05", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_05")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_06", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_06")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_07", blockAllState ));
                return;
              }
              if (block.permutation.matches("nicothekid:block_jar_sweet_berry_cookies_07")) {
                block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_cookies_08", blockAllState ));
                return;
              }
            }
          }
          else return;
        }
      }
      else return;
    }
  });
});
