import { Player, system } from "@minecraft/server";
import { faceOffset } from "../../util/faceOffset";
import { Vector3 } from "../../vector/vector3";

/**
 * @param {Player} player 
 */
export function teleporter(player) {
    const blockPos = player.getBlockFromViewDirection({ maxDistance: 100, includePassableBlocks: false, includeLiquidBlocks: false })

    if (blockPos != undefined) {
        const dim = player.dimension
        try {
            const loc = faceOffset(blockPos.face, blockPos.block.bottomCenter())
            player.teleport(loc)
            system.runTimeout(() => {
                dim.playSound("block_ha:teleport", loc)
                const particleLoc = loc.add(new Vector3(0, 1.5, 0))
                for (let i = 0; i < 10; i++) {
                    dim.spawnParticle("minecraft:mob_portal", particleLoc)
                }
            }, 1)
        }
        catch (e) { }
    }
    else player.onScreenDisplay.setActionBar("§cYou are too far away from a block to teleport.")
}

