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

world.beforeEvents.itemUseOn.subscribe(eventData => {
  const player = eventData.source;
  const itemUsed = eventData.itemStack;
  const block = eventData.block;
  const blockFacing = eventData.blockFace;
  const blockLocation = block.location;
  const customMobBuckets = [
    "nicothekid:bucket_of_jellyfish",
    "nicothekid:bucket_of_prismarine_clam"
  ]
  function giveEmptyBucket () {
    if (!player.matches({ gameMode: GameMode.creative })) {
      const emptyBucket = new ItemStack("minecraft:bucket", 1);
      const playerInventory = player?.getComponent("inventory").container;
      playerInventory.setItem(player.selectedSlot, emptyBucket)
    }
    else return;
  }
  if (customMobBuckets.includes(itemUsed.typeId)) {
    const fluidBlocks = [
      "minecraft:flowing_lava",
      "minecraft:flowing_water",
      "minecraft:lava",
      "minecraft:water"
    ]
    if (itemUsed.typeId === "nicothekid:bucket_of_jellyfish") {
      system.run(() => {
        if (fluidBlocks.includes(block.typeId)) {
          if (block.typeId === "minecraft:water" || block.typeId === "minecraft:flowing_water") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            }
          }
          if (block.typeId === "minecraft:lava" || block.typeId === "minecraft:flowing_lava") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.setType("minecraft:flowing_water");
              player.playSound("bucket.empty_water", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              player.playSound("bucket.empty_lava", { pitch: 1.0, location: blockLocation, volume: 1.0 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            }
          }
        }
        else {
          if (blockFacing === "Up") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.above(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y + 1.0,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "Down") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y - 1.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.below(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y - 1.0,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "North") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y + 0.0} ${blockLocation.z - 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.north(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y,
                z: blockLocation.z - 1.0
              })
            }
          }
          if (blockFacing === "South") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 0.5} ${blockLocation.y + 0.0} ${blockLocation.z + 1.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.south(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y,
                z: blockLocation.z + 1.0
              })
            }
          }
          if (blockFacing === "East") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x + 1.5} ${blockLocation.y + 0.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.east(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x + 1.0,
                y: blockLocation.y,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "West") {
            block.dimension.runCommandAsync(`summon nicothekid:jellyfish ${blockLocation.x - 0.5} ${blockLocation.y + 0.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.west(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x - 1.0,
                y: blockLocation.y,
                z: blockLocation.z
              })
            }
          }
        }
        giveEmptyBucket ();
      });
    }
    if (itemUsed.typeId === "nicothekid:bucket_of_prismarine_clam") {
      system.run(() => {
        if (fluidBlocks.includes(block.typeId)) {
          if (block.typeId === "minecraft:water" || block.typeId === "minecraft:flowing_water") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            }
          }
          if (block.typeId === "minecraft:lava" || block.typeId === "minecraft:flowing_lava") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.setType("minecraft:flowing_water");
              player.playSound("bucket.empty_water", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              player.playSound("bucket.empty_lava", { pitch: 1.0, location: blockLocation, volume: 1.0 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
            }
          }
        }
        else {
          if (blockFacing === "Up") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.above(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y + 1.0,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "Down") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y - 1.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.below(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y - 1.0,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "North") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y + 0.0} ${blockLocation.z - 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.north(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y,
                z: blockLocation.z - 1.0
              })
            }
          }
          if (blockFacing === "South") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 0.5} ${blockLocation.y + 0.0} ${blockLocation.z + 1.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.south(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x,
                y: blockLocation.y,
                z: blockLocation.z + 1.0
              })
            }
          }
          if (blockFacing === "East") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x + 1.5} ${blockLocation.y + 0.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.east(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x + 1.0,
                y: blockLocation.y,
                z: blockLocation.z
              })
            }
          }
          if (blockFacing === "West") {
            block.dimension.runCommandAsync(`summon nicothekid:prismarine_clam ${blockLocation.x - 0.5} ${blockLocation.y + 0.0} ${blockLocation.z + 0.5} facing @p`);
            if (player.dimension.id !== "minecraft:nether") {
              block.west(1).setType("minecraft:flowing_water");
              player.playSound("bucket.empty_fish", { pitch: 1.0, location: blockLocation, volume: 1.0 })
            }
            else {
              const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
              block.dimension.playSound("random.fizz", blockLocation, { pitch: pitchRange, volume: 0.5 });
              block.dimension.spawnParticle("minecraft:water_evaporation_bucket_emitter", {
                x: blockLocation.x - 1.0,
                y: blockLocation.y,
                z: blockLocation.z
              })
            }
          }
        }
        giveEmptyBucket ();
      });
    }
  }
  else return;
});