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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  const copperEvaporator = [
    "nicothekid:evaporator_copper_blue_ice",
    "nicothekid:evaporator_copper_cheese",
    "nicothekid:evaporator_copper_ice",
    "nicothekid:evaporator_copper_lava",
    "nicothekid:evaporator_copper_leather_scrap_1",
    "nicothekid:evaporator_copper_leather_scrap_2",
    "nicothekid:evaporator_copper_leather_scrap_3",
    "nicothekid:evaporator_copper_milk",
    "nicothekid:evaporator_copper_obsidian",
    "nicothekid:evaporator_copper_packed_ice",
    "nicothekid:evaporator_copper_rotten_flesh_1",
    "nicothekid:evaporator_copper_rotten_flesh_2",
    "nicothekid:evaporator_copper_rotten_flesh_3",
    "nicothekid:evaporator_copper_salt",
    "nicothekid:evaporator_copper_snow",
    "nicothekid:evaporator_copper_water",
    "nicothekid:evaporator_copper_empty"
  ]
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:copper_evaporator_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");
      function decreaseItem () {
        system.run(() => {
          if (!player.matches({ gameMode: GameMode.creative })) {
            const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
            playerEquippable.setEquipment("Mainhand", decrementItem)
          }
        });
      }
      function giveEmptyBucket () {
        const emptyBucket = new ItemStack ("minecraft:bucket", 1);
        playerEquippable.setEquipment("Mainhand", emptyBucket)
      }
      if (player.isSneaking) return;
      if (copperEvaporator.includes(block.typeId)) {
        if (block.typeId === "nicothekid:evaporator_copper_empty") {
          const validInputs = [
            "minecraft:blue_ice",
            "minecraft:ice",
            "minecraft:lava_bucket",
            "minecraft:milk_bucket",
            "minecraft:packed_ice",
            "minecraft:rotten_flesh",
            "minecraft:snow",
            "minecraft:water_bucket",
            "nicothekid:jar_water_full"
          ]
          if (itemUsed === undefined || !validInputs.includes(itemUsed.typeId)) return;
          if (itemUsed.typeId === "minecraft:blue_ice") {
            decreaseItem ();
            block.dimension.playSound("dig.stone", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_blue_ice"));
          }
          if (itemUsed.typeId === "minecraft:ice") {
            decreaseItem ();
            block.dimension.playSound("dig.stone", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_ice"));
          }
          if (itemUsed.typeId === "minecraft:lava_bucket") {
            giveEmptyBucket ();
            block.dimension.playSound("bucket.empty_lava", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_lava"));
          }
          if (itemUsed.typeId === "minecraft:milk_bucket") {
            giveEmptyBucket ();
            block.dimension.playSound("bucket.empty_water", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_milk"));
          }
          if (itemUsed.typeId === "minecraft:packed_ice") {
            decreaseItem ();
            block.dimension.playSound("dig.stone", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_packed_ice"));
          }
          if (itemUsed.typeId === "minecraft:rotten_flesh") {
            decreaseItem ();
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_rotten_flesh_1"));
            return;
          }
          if (itemUsed.typeId === "minecraft:snow") {
            decreaseItem ();
            block.dimension.playSound("use.snow", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_snow"));
          }
          if (itemUsed.typeId === "minecraft:water_bucket") {
            giveEmptyBucket ();
            block.dimension.playSound("bucket.empty_water", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_water"));
          }
          if (itemUsed.typeId === "nicothekid:jar_water_full") {
            decreaseItem ();
            block.dimension.playSound("bottle.empty", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:jar_empty 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_water"));
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_blue_ice") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:blue_ice") {
            const spawnBlueIce = new ItemStack ("minecraft:blue_ice", 1);
            block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnBlueIce, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:blue_ice") {
            block.dimension.playSound("block.click.fail", blockLocation, { pitch: 1.0, volume: 0.7 });
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_cheese") {
          const setCount = Math.random() * (4 - 3) + 3;
          const spawnCheese = new ItemStack ("nicothekid:cheese", setCount);
          block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnCheese, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_ice") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:ice") {
            const spawnIce = new ItemStack ("minecraft:ice", 1);
            block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnIce, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:blue_ice") {
            block.dimension.playSound("block.click.fail", blockLocation, { pitch: 1.0, volume: 0.7 });
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_lava") {
          if (itemUsed === undefined) return;
          if (itemUsed.typeId === "minecraft:bucket") {
            decreaseItem ();
            block.dimension.playSound("bucket.fill_lava", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s minecraft:lava_bucket 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_leather_scrap_1") {
          const setCount = Math.random() * (4 - 3) + 3;
          const spawnLeatherScrap = new ItemStack ("nicothekid:leather_scrap", setCount);
          block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnLeatherScrap, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_leather_scrap_2") {
          const setCount = Math.random() * (7 - 6) + 6;
          const spawnLeatherScrap = new ItemStack ("nicothekid:leather_scrap", setCount);
          block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnLeatherScrap, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_leather_scrap_3") {
          const setCount = Math.random() * (10 - 9) + 9;
          const spawnLeatherScrap = new ItemStack ("nicothekid:leather_scrap", setCount);
          block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnLeatherScrap, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_milk") {
          if (itemUsed === undefined) return;
          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:evaporator_copper_empty"));
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_obsidian") {
          const spawnObsidian = new ItemStack ("minecraft:obsidian", 1);
          block.dimension.playSound("dig.stone", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnObsidian, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_packed_ice") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:packed_ice") {
            const spawnPackedIce = new ItemStack ("minecraft:packed_ice", 1);
            block.dimension.playSound("block.itemframe.remove_item", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnPackedIce, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:packed_ice") {
            block.dimension.playSound("block.click.fail", blockLocation, { pitch: 1.0, volume: 0.7 });
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_rotten_flesh_1") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:rotten_flesh") {
            const spawnRottenFlesh = new ItemStack ("minecraft:rotten_flesh", 1);
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnRottenFlesh, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:rotten_flesh") {
            decreaseItem ();
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_rotten_flesh_2"));
            return;
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_rotten_flesh_2") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:rotten_flesh") {
            const spawnRottenFlesh = new ItemStack ("minecraft:rotten_flesh", 2);
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnRottenFlesh, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:rotten_flesh") {
            decreaseItem ();
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_rotten_flesh_3"));
            return;
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_rotten_flesh_3") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:rotten_flesh") {
            const spawnRottenFlesh = new ItemStack ("minecraft:rotten_flesh", 3);
            block.dimension.playSound("cake.add_candle", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnRottenFlesh, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:rotten_flesh") {
            block.dimension.playSound("block.click.fail", blockLocation, { pitch: 1.0, volume: 0.7 });
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_salt") {
          const setCount = Math.random() * (4 - 3) + 3;
          const spawnSalt = new ItemStack ("nicothekid:salt", setCount);
          block.dimension.playSound("dig.sand", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.dimension.spawnItem(spawnSalt, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
        }
        if (block.typeId === "nicothekid:evaporator_copper_snow") {
          if (itemUsed === undefined || itemUsed.typeId !== "minecraft:snow") {
            const spawnSnow = new ItemStack ("minecraft:snow", 1);
            block.dimension.playSound("dig.snow", blockLocation, { pitch: 1.0, volume: 0.7 });
            block.dimension.spawnItem(spawnSnow, { x: blockLocation.x + 0.5, y: blockLocation.y + 1.0, z: blockLocation.z + 0.5 });
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
          }
          else if (itemUsed.typeId === "minecraft:snow") {
            block.dimension.playSound("block.click.fail", blockLocation, { pitch: 1.0, volume: 0.7 });
          }
        }
        if (block.typeId === "nicothekid:evaporator_copper_water") {
          if (itemUsed === undefined) return;
          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:water_bucket 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
            return;
          }
          if (itemUsed.typeId === "nicothekid:jar_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:jar_water_full 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_empty"));
            return;
          }
          if (itemUsed.typeId === "nicothekid:bamboo_tumbler_empty") {
            decreaseItem ();
            block.dimension.playSound("bottle.fill", blockLocation, { pitch: 1.0, volume: 0.7 });
            player.runCommandAsync(`gamerule sendcommandfeedback false`);
            player.runCommandAsync(`give @s nicothekid:bamboo_tumbler_water 1`);
            player.runCommandAsync(`gamerule sendcommandfeedback true`);
            return;
          }
        }
      }
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:copper_evaporator_ticks", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (copperEvaporator.includes(block.typeId)) {
        if (block.permutation.matches("nicothekid:evaporator_copper_blue_ice")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_packed_ice"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_ice")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 0.7 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_water"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_lava")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_obsidian"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_milk")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_cheese"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_packed_ice")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_ice"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_rotten_flesh_1")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_leather_scrap_1"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_rotten_flesh_2")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_leather_scrap_2"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_rotten_flesh_3")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_leather_scrap_3"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_snow")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_water"));
          return
        }
        if (block.permutation.matches("nicothekid:evaporator_copper_water")) {
          block.dimension.playSound("step.powder_snow", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:evaporator_copper_salt"));
          return
        }
      }
    }
  });
});
