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

const PLATFORM_ID = "mebesm:jump_platform";
const BULLET_ID = "mebesm:grabpack_purple_bullet";
const HORIZONTAL_BAR_ID = "mebesm:horizontal_bar";
const GRAB_BULLETS = [
    "mebesm:grabpack_blue_bullet",
    "mebesm:grabpack_gren_bullet",
    "mebesm:grabpack_gren_active_bullet",
    "mebesm:grabpack_omni_bullet",
    "mebesm:grabpack_orange_bullet",
    "mebesm:grabpack_purple_bullet",
    "mebesm:grabpack_red_bullet",
    "mebesm:grabpack_gren_and_blue_bullet",
    "mebesm:grabpack_omni_and_blue_bullet",
    "mebesm:grabpack_purple_and_blue_bullet",
    "mebesm:grabpack_red_and_blue_bullet"
];

// Чтобы не тянуть одного игрока несколько раз подряд
const pullCooldowns = new Map();

system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        const dimension = player.dimension;

        // ====== JUMP PLATFORM ======
        const platforms = dimension.getEntities({
            type: PLATFORM_ID,
            location: player.location,
            maxDistance: 1.5
        });

        if (platforms.length > 0) {
            const bullets = dimension.getEntities({
                type: BULLET_ID,
                location: platforms[0].location,
                maxDistance: 1.5
            });

            if (bullets.length > 0) {
                const viewDir = player.getViewDirection();

                player.applyImpulse({
                    x: viewDir.x * 1,
                    y: 1.25,
                    z: viewDir.z * 1
                });

                player.runCommandAsync(`effect @s resistance 2 255 true`);
                player.runCommandAsync(`playsound mob.slime.jump @s ~ ~ ~ 1 1.5`);
                dimension.spawnParticle("minecraft:ice_evaporation_emitter", platforms[0].location);

                for (const bullet of bullets) {
                    bullet.remove();
                }
            }
        }

        // ====== HORIZONTAL BAR ======
        const now = system.currentTick;
        const lastPull = pullCooldowns.get(player.id) ?? 0;
        if (now - lastPull < 10) continue;

        const bars = dimension.getEntities({
            type: HORIZONTAL_BAR_ID,
            location: player.location,
            maxDistance: 50
        });

        for (const bar of bars) {
            const barLoc = bar.location;

            // Ищем снаряды около бара
            for (const bulletId of GRAB_BULLETS) {
                const nearBullets = dimension.getEntities({
                    type: bulletId,
                    location: barLoc,
                    maxDistance: 1.5
                });

                for (const bullet of nearBullets) {
                    // Проверяем что снаряд принадлежит этому игроку
                    const projComp = bullet.getComponent("minecraft:projectile");
                    const owner = projComp?.owner;
                    if (!owner || owner.id !== player.id) continue;

                    // Удаляем снаряд
                    bullet.remove();

                    // Тянем игрока к бару
                    const playerLoc = player.location;
                    const dx = barLoc.x - playerLoc.x;
                    const dy = barLoc.y - playerLoc.y;
                    const dz = barLoc.z - playerLoc.z;
                    const dist = Math.sqrt(dx * dx + dy * dy + dz * dz);

                    if (dist > 0) {
                        const speed = 1.8;
                        player.applyImpulse({
                            x: (dx / dist) * speed,
                            y: (dy / dist) * speed,
                            z: (dz / dist) * speed
                        });
                    }

                    player.runCommandAsync(`effect @s resistance 3 255 true`);
                    player.runCommandAsync(`playsound random.click @s ~ ~ ~ 1 1.2`);
                    dimension.spawnParticle("minecraft:critical_hit_emitter", barLoc);

                    pullCooldowns.set(player.id, now);
                    break;
                }
            }
        }
    }
}, 2);