import { world, Player } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";

world.afterEvents.playerInteractWithEntity.subscribe(ev => {
    const player = ev.player;
    const pelican = ev.target;

    if (!(player instanceof Player)) return;
    if (pelican.typeId !== "halo:pelican_2") return;

    const form = new ModalFormData()
        .title("Pelican Navigation System")
        .textField("X Coordinate", "Example: 120")
        .textField("Z Coordinate", "Example: -90");

    form.show(player).then(res => {
        if (res.canceled) return;

        const [xStr, zStr] = res.formValues;
        const x = parseFloat(xStr);
        const z = parseFloat(zStr);

        if (isNaN(x) || isNaN(z)) {
            player.sendMessage("Invalid coordinates.");
            return;
        }

        const y = 1000;
        const dim = world.getDimension("overworld");

        player.teleport({ x, y, z }, {
            dimension: dim,
            keepVelocity: false
        });

        player.triggerEvent("droppod");
    });
});
