import { world } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe((initEvent) => {
  initEvent.itemComponentRegistry.registerCustomComponent("drink:return_potion", {
    onConsume: ({ itemStack, source }) => {
      switch (itemStack.typeId) {
        case "drink:return_potion":
          const spawnPoint = source.getSpawnPoint();
          if (spawnPoint) {
            const { x, y, z } = spawnPoint;

            source.runCommandAsync(`playsound mob.shulker.teleport @s ~ ~ ~ 1 1.2`);
            source.teleport({ x, y, z }, {
              dimension: source.dimension,
              rotation: source.rotation,
              keepVelocity: false,
            });
          } else {
            source.runCommandAsync("title @s actionbar No spawnpoint set in this Dimension");
          }
          break;
      }
    }
  })
}
)