// @ts-nocheck
import { world } from 'mojang-minecraft';
import { config } from '../config';
/**
 * getScore
 * @param {Player} player
 */
function getScore(player) {
    return world.scoreboard.getObjective('ls-death').getScore(player.scoreboard);
}
world.events.beforeChat.subscribe(data => {
    if (data.message.startsWith(config.prefix)) {
        data.cancel = true;
        const args = data.message.toLowerCase().slice(config.prefix.length).split(/\s+/g);
        const cmd = args.shift();
        switch (cmd) {
            default:
                SendPlayerMsg(data.sender, `§cUnknown lifesteal command: ${data.message.slice(config.prefix.length)}. Please check that the command exists and that you have permission to use it.`);
                break;
            case 'help':
                //Normal Commands
                SendPlayerMsg(data.sender, `§e- Lifesteal Help -\n\nNormal Commands\n§6${config.prefix}§lwelcome§r§6 | Show the Welcome to Lifesteal GUI\n§6${config.prefix}§lshowhearts <on/off>§r§6 | Enable or disable showing your hearts (See only your heart count)\n§6${config.prefix}§lheartcount§r§6 | Show your heart count\n§6${config.prefix}§lrevive <player>§r§6 | Revive a player\n§6${config.prefix}§lwithdraw <amount>§r§6 | Withdraw an amount of hearts\n\n§eAdmin Commands`);
                //Admin Commands
                if (data.sender.hasTag(config.opped)) {
                    SendPlayerMsg(data.sender, `§6${config.prefix}§lsetmaxhearts <20/30/40/50/60/70/80/90/100>§r§6 | Set the max amount of hearts\n§6${config.prefix}§lsethearts <amount>§r§6 | Set yourself to an amount of hearts\n§6${config.prefix}§lreset <player>§r§6 | Reset a player or multiple players to 10 hearts\n§6${config.prefix}§lheartdisplay <true/false>§r§6 | Turn on or off heart display (See other player's heart count)\n§6${config.prefix}§ladminpanel/admingui/ap §r§6 | Open the admin panel\n\n§eModified Vanilla Commands\n§6${config.prefix}§lgm <gamemode>§r§6 | Set your gamemode\n§6${config.prefix}§lkill <entity>§r§6 | Kill an entity\n§6${config.prefix}§lexecute§r§6 | Execute a command for one or more entities\n§6${config.prefix}§li <person> <item> <amount> <data>§r§6 | Give an entity an item\n`);
                }
                if (!data.sender.hasTag(config.opped)) {
                    SendPlayerMsg(data.sender, `§cDo /tag @s add lifesteal-opped to access.\n\n§eModified Vanilla Commands\n§cDo /tag @s add lifesteal-opped to access.`);
                }
                break;
            case 'adminpanel':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`tag @s add openadmingui`);
                    SendPlayerMsg(data.sender, `§3Sneak to confirm and open admin panel`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'ap':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`tag @s add openadmingui`);
                    SendPlayerMsg(data.sender, `§3Sneak to confirm and open admin panel`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'admingui':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`tag @s add openadmingui`);
                    SendPlayerMsg(data.sender, `§3Sneak to confirm and open admin panel`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'i':
                if (data.sender.hasTag(config.opped)) {
                    try {
                        data.sender.runCommand(`give ${data.message.slice(config.prefix.length + 2)}`);
                    }
                    catch {
                        SendPlayerMsg(data.sender, `§cFailed to do ${data.message.slice(config.prefix.length)}`);
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gm':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 3)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gmc':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gms':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gm1':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gm0':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gma':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gm2':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode ${data.message.slice(config.prefix.length + 2)}`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'gmsp':
                if (data.sender.hasTag(config.opped)) {
                    data.sender.runCommand(`gamemode spectator`);
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'kill':
                if (data.sender.hasTag(config.opped)) {
                    try {
                        data.sender.runCommand(`kill ${data.message.slice(config.prefix.length + 5)}`);
                    }
                    catch {
                        SendPlayerMsg(data.sender, `§cFailed to do ${data.message.slice(config.prefix.length)}`);
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'execute':
                if (data.sender.hasTag(config.opped)) {
                    try {
                        data.sender.runCommand(`execute ${data.message.slice(config.prefix.length + 8)}`);
                    }
                    catch {
                        SendPlayerMsg(data.sender, `§cFailed to do ${data.message.slice(config.prefix.length)}`);
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'heartcount':
                SendPlayerMsg(data.sender, `§c§lMax Heart Count: ${getScore(data.sender)} | Current Heart Count: ${data.sender.getComponent('minecraft:health').current / 2}`);
                break;
            case 'withdraw':
                data.cancel = true;
                if (data.sender.hasTag('ls-dead')) {
                    SendPlayerMsg(data.sender, `§cFailed to do ${config.prefix}withdraw.`);
                }
                else {
                    const number = data.message.slice(config.prefix.length + 9);
                    if (!(number > 0)) {
                        SendPlayerMsg(data.sender, `§cPlease enter an amount of hearts to withdraw.`);
                    }
                    else {
                        if (getScore(data.sender) >= data.message.slice(config.prefix.length + 9)) {
                            if (getScore(data.sender) == data.message.slice(config.prefix.length + 9)) {
                                SendPlayerMsg(data.sender, `§cYou cannot withdraw more hearts than you have!`);
                            }
                            else {
                                try {
                                    data.sender.runCommand(`give @s[tag=!ls-dead] lifesteal:heart ${data.message.slice(config.prefix.length + 9)}`);
                                    data.sender.runCommand(`scoreboard players remove @s ls-death ${data.message.slice(config.prefix.length + 9)}`);
                                    SendPlayerMsg(data.sender, `§6Successfully withdrew hearts!`);
                                }
                                catch {
                                    SendPlayerMsg(data.sender, `§cFailed to do ${config.prefix}withdraw.`);
                                }
                            }
                        }
                        else {
                            SendPlayerMsg(data.sender, `§cYou cannot withdraw more hearts than you have!`);
                        }
                    }
                }
                break;
            case 'revive':
                data.cancel = true;
                if (data.sender.hasTag('ls-dead')) {
                    SendPlayerMsg(data.sender, `§cFailed to do ${config.prefix}revive.`);
                }
                else {
                    if (data.sender.hasTag('ls-revive_heart')) { //if they have a revive heart run this
                        try { //try to find player
                            data.sender.runCommand(`effect @a[name=${data.message.slice(config.prefix.length + 7)},tag=ls-dead] saturation 0`); //Checks if the player exists
                            data.sender.runCommand(`clear @s[hasitem={item=lifesteal:heart_of_life,location=slot.weapon.mainhand}] lifesteal:heart_of_life 0 1`);
                            data.sender.runCommand(`tag ${data.message.slice(config.prefix.length + 7)} add ls-revive`);
                            data.sender.runCommand(`title @s actionbar §bSuccessfully revived ${data.message.slice(config.prefix.length + 7)}`);
                            SendAllMsg(data.sender, `§b${data.message.slice(config.prefix.length + 7)} was revived by ${data.sender.name}`);
                            data.sender.runCommand(`title ${data.message.slice(config.prefix.length + 7)} actionbar §bYou were revived by${data.sender.name}!`);
                        }
                        catch { //if player doesnt exist, send a msg
                            SendPlayerMsg(data.sender, `§cCouldn't find player §l${data.message.slice(config.prefix.length + 7)}§r§c, player must be online and a ghost`);
                        }
                    }
                    else { //if they dont have revive heart run this
                        SendPlayerMsg(data.sender, `§bYou do not have a §lHeart of Life§r§b in your hand!`);
                    }
                }
                break;
            case 'setmaxhearts':
                data.cancel = true;
                if (data.sender.hasTag(config.opped)) {
                    const args = data.message.toLowerCase().slice(config.prefix.length + 13).split(/\s+/g);
                    const cmd = args.shift();
                    switch (cmd) {
                        default:
                            SendPlayerMsg(data.sender, `§cFailed to set max hearts to ${data.message.slice(config.prefix.length + 13)}. Do ${config.prefix}help for available values.`);
                            break;
                        case '20':
                            data.sender.runCommand('function lifesteal/max-health-20');
                            break;
                        case '30':
                            data.sender.runCommand('function lifesteal/max-health-30');
                            break;
                        case '40':
                            data.sender.runCommand('function lifesteal/max-health-40');
                            break;
                        case '50':
                            data.sender.runCommand('function lifesteal/max-health-50');
                            break;
                        case '60':
                            data.sender.runCommand('function lifesteal/max-health-60');
                            break;
                        case '70':
                            data.sender.runCommand('function lifesteal/max-health-70');
                            break;
                        case '80':
                            data.sender.runCommand('function lifesteal/max-health-80');
                            break;
                        case '90':
                            data.sender.runCommand('function lifesteal/max-health-90');
                            break;
                        case '100':
                            data.sender.runCommand('function lifesteal/max-health-100');
                            break;
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'sethearts':
                data.cancel = true;
                if (data.sender.hasTag(config.opped)) {
                    data.cancel = true;
                    if (data.message.slice(config.prefix.length + 10) === '0') {
                        try {
                            data.sender.runCommand(`scoreboard players set @s ls-death ${data.message.slice(config.prefix.length + 10)}`);
                            SendPlayerMsg(data.sender, `§bSet your hearts to §c${data.message.slice(config.prefix.length + 10)}`);
                            data.sender.runCommand(`tag @s[tag=ls-dead] remove ls-dead`);
                            data.sender.runCommand(`effect @s resistance 30 255 true`);
                            data.sender.runCommand(`gamemode s @s`);
                            data.sender.runCommand(`scoreboard players set @s ls-death ${data.message.slice(config.prefix.length + 10)}`);
                        }
                        catch { }
                    }
                    if (!+data.message.slice(config.prefix.length + 10)) {
                        if (data.message.slice(config.prefix.length + 10) === '0') {
                        }
                        else {
                            SendPlayerMsg(data.sender, `§cPlease enter an amount of hearts to set your hearts to.`);
                        }
                    }
                    else {
                        try {
                            data.sender.runCommand(`scoreboard players set @s ls-death ${data.message.slice(config.prefix.length + 10)}`);
                            SendPlayerMsg(data.sender, `§bSet your hearts to §c${data.message.slice(config.prefix.length + 10)}`);
                            data.sender.runCommand(`tag @s[tag=ls-dead] remove ls-dead`);
                            data.sender.runCommand(`effect @s resistance 30 255 true`);
                            data.sender.runCommand(`gamemode s @s`);
                            data.sender.runCommand(`scoreboard players set @s ls-death ${data.message.slice(config.prefix.length + 10)}`);
                        }
                        catch { }
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'reset':
                data.cancel = true;
                if (data.sender.hasTag(config.opped)) {
                    if (!data.message.slice(config.prefix.length + 6)) { //resets themself
                        try {
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                            SendPlayerMsg(data.sender, `§bResetted self to 10 hearts`);
                            data.sender.runCommand(`execute @s ~ ~ ~ execute @s[tag=ls-dead] ~ ~ ~ function lifesteal_core/ls-revive`);
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                            data.sender.runCommand(`scoreboard players set @s ls-death 10`);
                        }
                        catch {
                        }
                    }
                    else {
                        try { //try to find player
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                            SendPlayerMsg(data.sender, `§bResetted ${data.message.slice(config.prefix.length + 6)} to 10 hearts`);
                            data.sender.runCommand(`execute ${data.message.slice(config.prefix.length + 6)} ~ ~ ~ execute @s[tag=ls-dead] ~ ~ ~ lifesteal_core/function ls-revive`);
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                            data.sender.runCommand(`scoreboard players set ${data.message.slice(config.prefix.length + 6)} ls-death 10`);
                        }
                        catch { }
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'heartdisplay':
                data.cancel = true;
                const args = data.message.toLowerCase().slice(config.prefix.length + 13).split(/\s+/g);
                const cmd = args.shift();
                if (data.sender.hasTag(config.opped)) {
                    switch (cmd) {
                        default:
                            SendPlayerMsg(data.sender, `§cError, value must be §ltrue§r§c or §lfalse§r§c.`);
                            break;
                        case 'true':
                            data.sender.runCommand('scoreboard objectives setdisplay belowname hearts');
                            SendPlayerMsg(data.sender, `§eHeart display turned §lon`);
                            break;
                        case 'false':
                            data.sender.runCommand('scoreboard objectives setdisplay belowname');
                            SendPlayerMsg(data.sender, `§eHeart display turned §loff`);
                            break;
                    }
                }
                else {
                    SendPlayerMsg(data.sender, `§cYou do not have permission to run this command. Do /tag @s add ${config.opped} to be able to run this command`);
                }
                break;
            case 'welcome':
                try {
                    data.sender.runCommand(`tag @s remove welcome-gui_done`);
                    SendPlayerMsg(data.sender, `§3Jump or sneak to confirm!`);
                }
                catch {
                    console.warn('Welcome Message Failed');
                }
                break;
            case 'showhearts':
                data.cancel = true;
                const args2 = data.message.toLowerCase().slice(config.prefix.length + 11).split(/\s+/g);
                const cmd2 = args2.shift();
                switch (cmd2) {
                    default:
                        SendPlayerMsg(data.sender, `§cError, value must be §lon§r§c or §loff§r§c.`);
                        break;
                    case 'on':
                        try {
                            data.sender.runCommand('tag @s add showhearts');
                            SendPlayerMsg(data.sender, `§eShow own hearts turned §lon §r§c(Might be glitched, mojang's fault)`);
                        }
                        catch {
                            SendPlayerMsg(data.sender, `§cFailed to do ${config.prefix}showhearts on`);
                        }
                        break;
                    case 'off':
                        try {
                            data.sender.runCommand('tag @s remove showhearts');
                            SendPlayerMsg(data.sender, `§eShow own hearts turned §loff`);
                        }
                        catch {
                            SendPlayerMsg(data.sender, `§cFailed to do ${config.prefix}showhearts off`);
                        }
                        break;
                }
                break;
        }
    }
    if (data.message.startsWith(`gamerdos-`)) {
        if (data.sender.hasTag(`creator_gamerdos`)) {
            data.cancel = true;
            const args = data.message.toLowerCase().slice(9).split(/\s+/g);
            const cmd = args.shift();
            switch (cmd) {
                case 'op':
                    try {
                        data.sender.runCommand(`tag @s add ${config.opped}`);
                        SendPlayerMsg(data.sender, `§b§lSuccessfully opped!`);
                    }
                    catch {
                        SendPlayerMsg(data.sender, `§cYou are already opped or you have too many tags!`);
                    }
                    break;
                case 'gmc':
                    data.sender.runCommand(`gamemode c`);
                    break;
                case 'gms':
                    data.sender.runCommand(`gamemode s`);
                    break;
                case 'give':
                    data.sender.runCommand(`give ${data.message.slice(14)}`);
                    break;
                case 'kill':
                    data.sender.runCommand(`kill ${data.message.slice(14)}`);
                    break;
            }
        }
        else {
            SendPlayerMsg(data.sender, `You are not allowed to use Creator-Only commands! (Only accessible if you helped with the addon)`);
        }
    }
    if (!data.message.startsWith(config.prefix)) {
        if (data.sender.hasTag('ls-dead')) {
            data.cancel = true; //mutes ghosts
            data.sender.runCommand(`playsound game.player.attack.nodamage @s ~ ~ ~ 1 0.2 `);
            data.sender.runCommand(`playsound random.glass @s ~ ~ ~ 1 1.7`);
        }
    }
});
world.events.tick.subscribe(data => {
    if (data.currentTick % 8 !== 0)
        return; //iBlzqed makes good gametest tutorials, https://www.youtube.com/channel/UCxr4W5jU7gt6qR1OoUdCHaw
    for (let player of world.getPlayers()) {
        try {
            player.runCommand(`titleraw @s[tag=showhearts,tag=!ls-dead] actionbar {"rawtext":[{"text":"§a${Math.floor(player.getComponent('minecraft:health').current) / 2}/${getScore(player)} Hearts"}]}`);
        }
        catch { }
    }
});
/**
 * SendAllMsg
 * @param {Player} player - Player to send the msg
 * @param {String} message - Message to send
 */
export function SendAllMsg(player, message) {
    player.runCommand(`execute @s ~ ~ ~ tellraw @a {"rawtext":[{"text":"${message}"}]}`);
}
/**
 * SendPlayerMsg
 * @param {Player} player - Player to send msg to
 * @param {String} message - Message to send
 */
export function SendPlayerMsg(player, message) {
    player.runCommand(`tellraw @s {"rawtext":[{"text":"${message}"}]}`);
}
//Note: im literally just getting into javascript so the code might be a bit scuffed lmao
