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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:jar_of_drinks_interaction", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockFacing = eventData.face;
      const blockLocation = block.location;
      const playerEquippable = player.getComponent("equippable");
      const itemUsed = playerEquippable.getEquipment("Mainhand");
      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 drinkJars = [
        "nicothekid:block_jar_empty",
        "nicothekid:block_jar_chocolate_full",
        "nicothekid:block_jar_chocolate_half",
        "nicothekid:block_jar_glow_berry_juice_full",
        "nicothekid:block_jar_glow_berry_juice_half",
        "nicothekid:block_jar_honey_full",
        "nicothekid:block_jar_honey_half",
        "nicothekid:block_jar_milk_full",
        "nicothekid:block_jar_milk_half",
        "nicothekid:block_jar_sweet_berry_juice_full",
        "nicothekid:block_jar_sweet_berry_juice_half",
        "nicothekid:block_jar_water_full",
        "nicothekid:block_jar_water_half"
      ];
      const validLiquids = [
        "minecraft:bucket",
        "minecraft:milk_bucket",
        "minecraft:glass_bottle",
        "minecraft:honey_bottle",
        "minecraft:potion",
        "nicothekid:bamboo_tumbler_empty",
        "nicothekid:bamboo_tumbler_chocolate",
        "nicothekid:bamboo_tumbler_glow_berry",
        "nicothekid:bamboo_tumbler_milk",
        "nicothekid:bamboo_tumbler_sweet_berry",
        "nicothekid:bamboo_tumbler_water"
      ];
      if (player.isSneaking || itemUsed === undefined || blockFacing === "Up") return;
      if (drinkJars.includes(block.typeId) && validLiquids.includes(itemUsed.typeId)) {
        if (block.permutation.matches("nicothekid:block_jar_empty")) {
          if (itemUsed.typeId === "minecraft:milk_bucket") {
            decreaseItem ();
            block.dimension.playSound("bucket.empty_water", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:bucket 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_milk_full", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "minecraft:honey_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:glass_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_honey_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "minecraft:potion") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:glass_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_chocolate") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_chocolate_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_glow_berry") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });        
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_juice_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_milk") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_milk_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_sweet_berry") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_juice_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_water") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_chocolate_half")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_chocolate 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_chocolate") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_chocolate_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_chocolate_full")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_chocolate 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_chocolate_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_glow_berry_juice_half")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_glow_berry 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_glow_berry") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_juice_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_glow_berry_juice_full")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_glow_berry 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_glow_berry_juice_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_honey_half")) {
          if (itemUsed.typeId === "minecraft:glass_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:honey_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "minecraft:honey_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:glass_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_honey_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_honey_full")) {
          if (itemUsed.typeId === "minecraft:glass_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:honey_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_honey_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_milk_half")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_milk 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_milk") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_milk_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_milk_full")) {
          if (itemUsed.typeId === "minecraft:bucket") {
            decreaseItem ();
            block.dimension.playSound("bucket.fill_water", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:milk_bucket 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_milk 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_milk_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_sweet_berry_juice_half")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_sweet_berry 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_sweet_berry") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_juice_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_sweet_berry_juice_full")) {
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_sweet_berry 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_sweet_berry_juice_half", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_water_half")) {
          if (itemUsed.typeId === "minecraft:glass_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:potion 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "minecraft:potion") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:glass_bottle 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_full", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_water 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_empty", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_water") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_full", blockAllState ));
            return;
          }
        }
        if (block.permutation.matches("nicothekid:block_jar_water_full")) {
          if (itemUsed.typeId === "minecraft:glass_bottle") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:potion 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_half", blockAllState ));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.5 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_water 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:block_jar_water_half", blockAllState ));
            return;
          }
        }
      }
      else return;
    }
  });
});
