import { system, world, MolangVariableMap, } from "@minecraft/server";
let playerParticleLocations = {};
let playerIsUsingLocator = {};
export default async function nestLocator(log, ItemUseAfterEvent) {
    const player = ItemUseAfterEvent.source;
    const playerId = player.id;
    const playerLocation = player.location;
    const nests = player.dimension.getEntities({
        closest: 1,
        location: playerLocation,
        families: ["kicu_mmad_nest"],
        maxDistance: 100,
    });
    if (playerIsUsingLocator[playerId] !== true) {
        if (nests && nests.length > 0) {
            const identity = nests[0].scoreboardIdentity;
            try {
                const objective = world.scoreboard.getObjective("kicu_mmad_NestHasEgg");
                if (objective.getScore(nests[0]) === undefined) {
                    player.onScreenDisplay.setActionBar({
                        rawtext: [{ translate: "dialog.kicu_mmad.nestlocator.nonestfound", with: ["\n"] }],
                    });
                    return;
                }
            }
            catch (error) {
                player.onScreenDisplay.setActionBar({
                    rawtext: [{ translate: "dialog.kicu_mmad.nestlocator.nonestfound", with: ["\n"] }],
                });
                return;
            }
            const nestLocation = nests[0].location;
            player.onScreenDisplay.setActionBar({
                rawtext: [{ translate: "dialog.kicu_mmad.nestlocator.nestfound", with: ["\n"] }],
            });
            player.playSound("sound.kicu_mmad_nest_locator.sound");
            const travelVector = {
                x: nestLocation.x - playerLocation.x,
                y: nestLocation.y - playerLocation.y + 2,
                z: nestLocation.z - playerLocation.z,
            };
            const velocityMultiplier = 1 /
                Math.sqrt(travelVector.x * travelVector.x + travelVector.y * travelVector.y + travelVector.z * travelVector.z);
            const locatorTravelVector = {
                x: travelVector.x * velocityMultiplier,
                y: travelVector.y * velocityMultiplier,
                z: travelVector.z * velocityMultiplier,
            };
            playerParticleLocations[playerId] = {
                x: playerLocation.x,
                y: playerLocation.y + 2,
                z: playerLocation.z,
            };
            playerIsUsingLocator[playerId] = true;
            system.runTimeout(() => {
                playerIsUsingLocator[playerId] = false;
            }, 60);
            for (let x = 1; x <= 6; x++) {
                system.runTimeout(() => {
                    const playerLocation = playerParticleLocations[playerId];
                    const molang = new MolangVariableMap();
                    molang.setVector3("kicu_mmad_speed", {
                        x: locatorTravelVector.x,
                        y: locatorTravelVector.y,
                        z: locatorTravelVector.z,
                    });
                    player.spawnParticle("kicu_mmad:nest_locator_particle", {
                        x: playerLocation.x,
                        y: playerLocation.y,
                        z: playerLocation.z,
                    }, molang);
                    playerParticleLocations[playerId] = {
                        x: playerLocation.x + locatorTravelVector.x,
                        y: playerLocation.y + locatorTravelVector.y,
                        z: playerLocation.z + locatorTravelVector.z,
                    };
                }, 4 * x);
            }
        }
        else {
            player.onScreenDisplay.setActionBar({
                rawtext: [{ translate: "dialog.kicu_mmad.nestlocator.nonestfound", with: ["\n"] }],
            });
        }
    }
    else {
        player.onScreenDisplay.setActionBar({
            rawtext: [{ translate: "dialog.kicu_mmad.nestlocator.cooldown", with: ["\n"] }],
        });
    }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/NestLocator.js.map
