import {
    world,
    system
}
from "@minecraft/server";
import {
    ActionFormData,
    MessageFormData,
    ModalFormData,
    ActionFormResponse
}
from "@minecraft/server-ui";
system.runInterval(runInterval)

function score(player, objective) {
    try {
        return world.scoreboard.getObjective(objective).getScore(player.scoreboardIdentity)
    } catch (e) {
        return 0;
    }
}
system.runInterval(() => {
    for (let player of world.getPlayers()) {
        if (player.isSneaking) {player.addTag("isUsingSAPI")
        } else {player.addTag("isUsingSAPI")
        }
    }
})



function runInterval() {
    for (const player of world.getPlayers()) {
        "xyz".split("").map(xyz => {
            player.runCommand(`scoreboard objectives add ts:v${xyz} dummy`)
        })
        for (const entity of player.dimension.getEntities()) {
            if (entity.hasTag("tse_st")) {
                let tsv = Object.fromEntries("xyz".split("").map(xyz => {
                    return [ xyz, score(entity, "ts:v" + xyz) ]
                }))
                entity.runCommand("tp @s[tag=tse] @s")
                if (!entity.hasTag("tse_over")) {
                    if (!entity.hasTag("tse")) {
                        let velocity = entity.getVelocity()
                        if (entity.typeId !== "minecraft:player") {
                            entity.clearVelocity();
                        }
                        let scoreboardVelocity = Object.fromEntries("xyz".split("").map(xyz => {
                            return [ xyz, parseInt(velocity[xyz]) * Math.pow(10,3) ]
                        }))
                        "xyz".split("").map(xyz => {
                            entity.runCommand(`scoreboard players set @s ts:v${xyz} ${scoreboardVelocity[xyz]}`)
                        })
                        entity.addTag("tse")
                    }
                } else {
                    entity.removeTag("tse")
                    entity.removeTag("tse_st")
                    entity.removeTag("tse_over")
                    if (entity.typeId !== "minecraft:player") {
                        entity.applyImpulse(Object.fromEntries("xyz".split("").map(xyz => {
                            return [ xyz, tsv[xyz] / Math.pow(10,3) ]
                        })));
                    }
                }
                if (entity.typeId !== "minecraft:player") {
                    entity.applyImpulse(Object.fromEntries("xyz".split("").map(xyz => {
                        return [ xyz, tsv[xyz] / Math.pow(10,8) ]
                    })));
                } else {
                    entity.teleport(Object.fromEntries("xyz".split("").map(xyz => {
                        return [ xyz, entity.location[xyz] ]
                    })))
                    entity.runCommand(`effect @s slowness 1 255 true`)
                }
            }
        }
    }
    for (const player of world.getPlayers()) {
        if (player.isSneaking) {
            player.runCommand("function player_sneaking")
        } else {
            player.runCommand("function player_stop_sneaking")
        }
    }
    for (const player of world.getPlayers()) {
        for (const entity of player.dimension.getEntities()) {
            if (entity.hasTag("gd_m_ed")) {
                if (entity.isFalling) {
                    entity.runCommand("damage @s 1")
                    entity.runCommand("effect @s fatal_poison 2 1")
                    entity.runCommand("effect @s slowness 2 0")
                    entity.runCommand("playsound gd_m_e @a[r=4] ~~~")
                    entity.runCommand("particle jjba:gd_m_e ~~0.5~")
                }
            }
        }
    }
}