import * as mc from '@minecraft/server';
import * as mcui from "@minecraft/server-ui";

// Hub command
mc.world.beforeEvents.chatSend.subscribe((eventData) => {
	const player = eventData.sender;
	switch (eventData.message) {
		case '!hub':
			eventData.cancel = true;
            goHub(player);
			break;
        case '!settings':
			eventData.cancel = true;
            player.runCommandAsync("effect @s instant_damage 1 1 true")
            mc.system.runTimeout(() => {
                player.runCommandAsync("effect @s instant_health 1 255 true")
                if (player.hasTag("extension")) {
                    clutchSettings(player)
                } else if (player.hasTag("pearl")) {
                    pearlSettings(player)
                } else if (player.hasTag("pvp")) {
                    pvpSettings(player)
                } else if (player.hasTag("bridge")) {
                    bridgeSettings(player)
                } else if (player.hasTag("parkourCourse")) {
                    parkourSettings(player)
                } else if (player.hasTag("breaking")) {
                    breakingSettings(player)
                } else if (player.hasTag("bow")) {
                    bowSettings(player)
                } else if (player.hasTag("hotbar")) {
                    hotbarSettings(player)
                } else if (player.hasTag("defense")) {
                    defenseSettings(player)
                } else if (player.hasTag("ore")) {
                    oreSettings(player)
                } else {
                    showSettingsMenu(player)
                }
            }, 10)
            break
        case '!test':
            eventData.cancel = true;

            mc.system.run(() => {
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 0, z: 2002 });
                console.error(block.typeId)
            })
            break;
		default: break;
	}
});

// NPC Interactions
var clicks = new Array()

mc.world.afterEvents.entityHitEntity.subscribe(data => {
    if (data.damagingEntity.typeId == "minecraft:player") {
        clicks.push(mc.system.currentTick)
    }
    if (data.hitEntity == undefined) {
        return
    } else if (data.hitEntity.typeId == "pada:npc1") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showPearlMenu(data.damagingEntity);
    } else if (data.hitEntity.typeId == "pada:npc2") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showPvpMenu(data.damagingEntity);
    } else if (data.hitEntity.typeId == "pada:npc3") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showExtensionMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc4") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showBridgeMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc5") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showBParkourMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc6") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showBreakingMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc7") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showBowMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc8") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showHotbarMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc9") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showDefenseMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc10") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showOreMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "pada:npc11") {
        data.damagingEntity.runCommandAsync('playsound random.pop @s');
        showChestMenu(data.damagingEntity)
    } else if (data.hitEntity.typeId == "minecraft:player" && data.damagingEntity.typeId == "pada:pvpbot") {
        for (let score of mc.world.scoreboard.getObjective("pvp").getScores()) if (score.participant.displayName == "Combo") data.hitEntity.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score)}`)
        data.damagingEntity.runCommandAsync("scoreboard players set Combo pvp 0")
    } else if (data.hitEntity.typeId == "pada:treasure") {
        if (data.damagingEntity.hasTag("breaking")) {
            data.hitEntity.runCommandAsync("particle minecraft:huge_explosion_emitter")
            data.hitEntity.runCommandAsync("playsound random.explode @a")
            data.hitEntity.runCommandAsync("effect @s invisibility 999999 255 true")
            data.hitEntity.runCommandAsync("tp ~ ~300 ~ ")
            data.hitEntity.runCommandAsync("setblock 1022 2 2009 air")
            data.damagingEntity.runCommandAsync("tag @s remove breakingStarted")
            data.damagingEntity.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §ems" } ] }')
            data.damagingEntity.runCommandAsync("kill @e[type=pada:pvpbot]")
            let highscores = mc.world.scoreboard.getObjective("highscore")
            var highscoreFinal = 0
            for (let highscore of highscores.getScores()) {
                if (highscore.participant.displayName == "breaking") {
                    highscoreFinal = highscore.score
                    break
                }
            }
            var time = 0
            let scores = mc.world.scoreboard.getObjective("breaking")
            for (let score of scores.getScores()) {
                if (score.participant.displayName == "Time") {
                    time = score.score
                    break
                }
            }
            if (highscoreFinal == 0) {
                data.damagingEntity.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou broke the treasure for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §ams!" } ] }`)
                for (let score of mc.world.scoreboard.getObjective("breaking").getScores()) if (score.participant.displayName == "Time") data.damagingEntity.runCommandAsync(`scoreboard players add @s money ${Math.floor((30000/score.score)*1.5)}`)
                data.damagingEntity.runCommandAsync(`scoreboard players set breaking highscore ${time}`)
            } else if (highscoreFinal == time) {
                data.damagingEntity.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                for (let score of mc.world.scoreboard.getObjective("breaking").getScores()) if (score.participant.displayName == "Time") data.damagingEntity.runCommandAsync(`scoreboard players add @s money ${Math.floor(30000/score.score)}`)
            } else if (highscoreFinal < time) {
                let difference = time - highscoreFinal
                data.damagingEntity.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "breaking", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                for (let score of mc.world.scoreboard.getObjective("breaking").getScores()) if (score.participant.displayName == "Time") data.damagingEntity.runCommandAsync(`scoreboard players add @s money ${Math.floor(30000/score.score)}`)
            } else {
                let difference = highscoreFinal - time
                data.damagingEntity.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "breaking", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                for (let score of mc.world.scoreboard.getObjective("breaking").getScores()) if (score.participant.displayName == "Time") data.damagingEntity.runCommandAsync(`scoreboard players add @s money ${Math.floor((30000/score.score)*1.5)}`)
                data.damagingEntity.runCommandAsync(`scoreboard players set breaking highscore ${time}`)
            }
            mc.system.runTimeout(() => {
                data.hitEntity.runCommandAsync("kill")
            }, 1)
            mc.system.runTimeout(() => {
                data.damagingEntity.runCommandAsync("function breaking/reset")
                mc.system.runTimeout(() => {
                    data.damagingEntity.runCommandAsync("summon pada:treasure 1013 4 2000")
                    data.damagingEntity.runCommandAsync("scoreboard players set Time breaking 0")
                }, 5)
            }, 60)
        }
    }
})

mc.world.afterEvents.entityHurt.subscribe(data => {
    if (data.damageSource.damagingEntity) {
        if (data.hurtEntity.typeId == "pada:pvpbot" && data.damageSource.damagingEntity.hasTag("pvp")) {
            data.damageSource.damagingEntity.runCommandAsync("scoreboard players add Combo pvp 1")
        }
    }
})

const parkourCoords = [[-1011, 1], [-1000, 2], [-989, 1], [-978, 1], [-967, 2], [-956, 2], [-945, 1], [-934, 3], [-923, 2], [-912, 2]]
var newItem = ["minecraft:iron_sword", "Sword"]
const hotbarItems = [["minecraft:iron_sword", "Sword"], ["minecraft:iron_pickaxe", "Pickaxe"], ["minecraft:iron_axe", "Axe"], ["minecraft:stone", "Blocks"], ["minecraft:snowball", "Snowballs"], ["minecraft:ender_pearl", "Pearl"], ["minecraft:bow", "Bow"], ["minecraft:tnt", "TNT"], ["minecraft:golden_apple", "Golden Apple"]]

var oldItem = ""

mc.system.run(
    function tick () {
        for (let player of mc.world.getPlayers()) {
            // Projectile Trails
            if (player.hasTag("projectiles")) {
                for (let tag of player.getTags()) {
                    for (let particle in projectiles) {
                        if (tag == particle) {
                            mc.world.getDimension("overworld").runCommandAsync(`execute @e[type=arrow] ~ ~ ~ particle ${projectiles[particle][1]} ~ ~ ~`)
                            mc.world.getDimension("overworld").runCommandAsync(`execute @e[type=snowball] ~ ~ ~ particle ${projectiles[particle][1]} ~ ~ ~`)
                            mc.world.getDimension("overworld").runCommandAsync(`execute @e[type=fishing_hook] ~ ~ ~ particle ${projectiles[particle][1]} ~ ~ ~`)
                        }
                    }
                }
            }

            if (player.hasTag("hub")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eMoney: §f" }, { "score": {"name": "@s", "objective": "money" } } ] }')
            }

            // CPS Counter
            // if (player.hasTag("cps")) {
            //     player.runCommandAsync("title @s actionbar §f" + getCPS() + " §eCPS")
            // }

            // Pearl Fix
            if (player.hasTag("baroque") && player.hasTag("pearl")) {
                player.runCommandAsync("function pearlclutch/baroque/pearlfix")
            } if (player.hasTag("wildwood") && player.hasTag("pearl")) {
                player.runCommandAsync("function pearlclutch/wildwood/pearlfix")
            } if (player.hasTag("village") && player.hasTag("pearl")) {
                player.runCommandAsync("function pearlclutch/village/pearlfix")
            } if (player.hasTag("flat") && player.hasTag("pearl")) {
                player.runCommandAsync("function pearlclutch/flat/pearlfix")
            }

            // Pearl clutch items
            if (player.hasTag("pearl")) {
                player.runCommandAsync("effect @e[type=pada:clutchbot,name=pearl] invisibility 999999 255 true")
                if (!mc.world.scoreboard.getObjective("inventoryPearl")) {
                    player.runCommandAsync("scoreboard objectives add inventoryPearl dummy inventory")
                    player.runCommandAsync("scoreboard players set pearl inventoryPearl 0")
                    player.runCommandAsync("scoreboard players set blocks inventoryPearl 1")
                    player.runCommandAsync("scoreboard players set settings inventoryPearl 7")
                    player.runCommandAsync("scoreboard players set hub inventoryPearl 8")
                } else {
                    let scoreboard = mc.world.scoreboard.getObjective("inventoryPearl")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == "pearl") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} ender_pearl 16`)
                        }
                        if (score.participant.displayName == "blocks") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} ${pearlBlock} 64`)
                        }
                        if (score.participant.displayName == "settings") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:settings`)
                        }
                        if (score.participant.displayName == "hub") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:hub`)
                        }
                    }
                }
            }

            // PvP items
            if (player.hasTag("pvp")) {
                if (!mc.world.scoreboard.getObjective("inventoryPvp")) {
                    player.runCommandAsync("scoreboard objectives add inventoryPvp dummy inventory")
                    player.runCommandAsync("scoreboard players set sword inventoryPvp 0")
                    player.runCommandAsync("scoreboard players set rod inventoryPvp 1")
                    player.runCommandAsync("scoreboard players set snowball inventoryPvp 2")
                    player.runCommandAsync("scoreboard players set bow inventoryPvp 3")
                    player.runCommandAsync("scoreboard players set pearl inventoryPvp 4")
                    player.runCommandAsync("scoreboard players set blocks inventoryPvp 5")
                    player.runCommandAsync("scoreboard players set settings inventoryPvp 7")
                    player.runCommandAsync("scoreboard players set hub inventoryPvp 8")
                } else {
                    let scoreboard = mc.world.scoreboard.getObjective("inventoryPvp")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == "sword") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} diamond_sword`)
                        }
                        if (score.participant.displayName == "rod") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} fishing_rod`)
                        }
                        if (score.participant.displayName == "snowball") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} snowball 16`)
                        }
                        if (score.participant.displayName == "bow") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} bow`)
                        }
                        if (score.participant.displayName == "pearl") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} ender_pearl 16`)
                        }
                        if (score.participant.displayName == "blocks") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:crumbling 64`)
                        }
                        if (score.participant.displayName == "settings") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:settings`)
                        }
                        if (score.participant.displayName == "hub") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:hub`)
                        }
                        player.runCommandAsync("replaceitem entity @s slot.inventory 0 arrow")
                    }
                }
            }
            
            // PvP Bots
            if (player.hasTag("pvp")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eCombo: §f" }, { "score": {"name": "Combo", "objective": "pvp" } } ] }')
                player.runCommandAsync("execute @e[type=pada:pvpbot] ~ ~ ~ detect ~1 ~ ~ barrier 0 tp ~-1 ~ ~")
                player.runCommandAsync("execute @e[type=pada:pvpbot] ~ ~ ~ detect ~-1 ~ ~ barrier 0 tp ~1 ~ ~")
                player.runCommandAsync("execute @e[type=pada:pvpbot] ~ ~ ~ detect ~ ~ ~1 barrier 0 tp ~ ~ ~-1")
                player.runCommandAsync("execute @e[type=pada:pvpbot] ~ ~ ~ detect ~ ~ ~-1 barrier 0 tp ~ ~ ~1")
                player.runCommandAsync("effect @s resistance 1 255 true")
            }

            // Extension items
            if (player.hasTag("extension")) {
                // player.runCommandAsync("replaceitem entity @s slot.hotbar 0 pada:start")
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eBlocks: §f" }, { "score": {"name": "Blocks", "objective": "extension" } } ] }')
                player.runCommandAsync("replaceitem entity @s slot.hotbar 7 pada:settings")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 8 pada:hub")
            }
            if (player.hasTag("extensionStarted")) {
                player.runCommandAsync(`replaceitem entity @s slot.hotbar 4 ${extensionBlock} 64`)
            }
            
            // Extension death
            if (player.hasTag("extension") && player.location.y <= -16) {
                player.runCommandAsync("tp @s -1000 0 0")
                player.runCommandAsync("tag @s remove extensionStarted")
                player.runCommandAsync(`clear @s ${extensionBlock}`)
                player.runCommandAsync("function extension/clear")
            }
            
            // Block Parkour
            if (player.hasTag("parkour")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eStreak: §f" }, { "score": {"name": "Streak", "objective": "parkour" } } ] }')
                if (player.location.y <= -4) {
                    var random = Math.floor(Math.random() * 10)
                    player.runCommandAsync(`tp @s 0 0 ${parkourCoords[random][0]}`)
                    player.runCommandAsync(`replaceitem entity @s slot.hotbar 4 pada:crumbling ${parkourCoords[random][1]} 0 {"can_place_on":{"blocks":["concrete","pada:crumbling"]}}`)
                    player.runCommandAsync('title @s subtitle §cYou died!§r')
                    player.runCommandAsync('title @s title §r')
                    player.runCommandAsync('playsound note.bass')
                    player.runCommandAsync('scoreboard players set Streak parkour 0')
                    for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Streak") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score*1.5)}`)
                    player.runCommandAsync("function bParkour/random/reset")
                }
                if (player.hasTag("plusOne")) {
                    var random = Math.floor(Math.random() * 10)
                    player.runCommandAsync(`tp @s 0 0 ${parkourCoords[random][0]}`)
                    player.runCommandAsync(`replaceitem entity @s slot.hotbar 4 pada:crumbling ${parkourCoords[random][1]} 0 {"can_place_on":{"blocks":["concrete","pada:crumbling"]}}`)
                    player.runCommandAsync("tag @s remove plusOne")
                    player.runCommandAsync("scoreboard players add Streak parkour 1")
                    player.runCommandAsync("playsound random.orb")
                    player.runCommandAsync("function bParkour/random/reset")
                }
                player.runCommandAsync("execute @s ~ ~ ~ detect ~ ~-0.1 ~ diamond_block 0 tag @s add plusOne")
                // player.runCommandAsync(`execute @s ~ ~ ~ detect ~ ~-0.1 ~ diamond_block 0 tp @s 0 0 ${parkourCoords[random]}`)
            }
            
            // Block Parkour items
            if (player.hasTag("courseStarted")) {
                player.runCommandAsync(`replaceitem entity @s slot.hotbar 4 ${parkourBlock} 64`)
            }

            if (player.hasTag("parkourCourse")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§bTime: §f" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §bms" } ] }')
                if (player.hasTag("course1") && player.location.z >= 1078 && player.location.y >= 2 && player.location.y < 4) {
                    player.runCommandAsync("setblock -998 201 1039 air")
                    player.runCommandAsync("tag @s remove course1")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("playsound respawn_anchor.charge")
                    player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ems" } ] }')
                    let highscores = mc.world.scoreboard.getObjective("highscore")
                    var highscoreFinal = 0
                    for (let highscore of highscores.getScores()) {
                        if (highscore.participant.displayName == "course1") {
                            highscoreFinal = highscore.score
                            break
                        }
                    }
                    var time = 0
                    let scores = mc.world.scoreboard.getObjective("parkour")
                    for (let score of scores.getScores()) {
                        if (score.participant.displayName == "Time") {
                            time = score.score
                            break
                        }
                    }
                    if (highscoreFinal == 0) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this course for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams!" } ] }`)
                        player.runCommandAsync(`scoreboard players set course1 highscore ${time}`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((300000/score.score)*1.5)}`)
                    } else if (highscoreFinal == time) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(300000/score.score)}`)
                    } else if (highscoreFinal < time) {
                        let difference = time - highscoreFinal
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "course1", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(300000/score.score)}`)
                    } else {
                        let difference = highscoreFinal - time
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "course1", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((300000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set course1 highscore ${time}`)
                    }
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("tp -1000 0 1000")
                        player.runCommandAsync("tag @s add course1")
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                        player.runCommandAsync("function bParkour/course/clear1")
                    }, 60)
                }
                if (player.hasTag("course1") && player.location.y <= -4) {
                    player.runCommandAsync("tp -1000 0 1000")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("setblock -998 201 1039 air")
                    player.runCommandAsync(`clear @s ${parkourBlock}`)
                    player.runCommandAsync("title @s subtitle §cYou died!§r")
                    player.runCommandAsync("title @s title §r")
                    player.runCommandAsync("playsound note.bass")
                    player.runCommandAsync("function bParkour/course/clear1")
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                    }, 4)
                }
                if (player.hasTag("course2") && player.location.x > -951 && player.location.y >= 1 && player.location.y < 3) {
                    player.runCommandAsync("setblock -975 200 -1000 air")
                    player.runCommandAsync("tag @s remove course2")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("playsound respawn_anchor.charge")
                    player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ems" } ] }')
                    let highscores = mc.world.scoreboard.getObjective("highscore")
                    var highscoreFinal = 0
                    for (let highscore of highscores.getScores()) {
                        if (highscore.participant.displayName == "course2") {
                            highscoreFinal = highscore.score
                            break
                        }
                    }
                    var time = 0
                    let scores = mc.world.scoreboard.getObjective("parkour")
                    for (let score of scores.getScores()) {
                        if (score.participant.displayName == "Time") {
                            time = score.score
                            break
                        }
                    }
                    if (highscoreFinal == 0) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this course for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((150000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set course2 highscore ${time}`)
                    } else if (highscoreFinal == time) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(150000/score.score)}`)
                    } else if (highscoreFinal < time) {
                        let difference = time - highscoreFinal
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "course2", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(150000/score.score)}`)
                    } else {
                        let difference = highscoreFinal - time
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "course2", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((150000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set course2 highscore ${time}`)
                    }
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("tp -1000 0 -1000")
                        player.runCommandAsync("tag @s add course2")
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                        player.runCommandAsync("function bParkour/course/clear2")
                    }, 60)
                }
                if (player.hasTag("course2") && player.location.y <= -4) {
                    player.runCommandAsync("tp -1000 0 -1000")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("setblock -975 200 -1000 air")
                    player.runCommandAsync(`clear @s ${parkourBlock}`)
                    player.runCommandAsync("title @s subtitle §cYou died!§r")
                    player.runCommandAsync("title @s title §r")
                    player.runCommandAsync("playsound note.bass")
                    player.runCommandAsync("function bParkour/course/clear2")
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                    }, 4)
                }
                if (player.hasTag("course3") && player.location.x >= -1936 && player.location.y >= 14 && player.location.y < 16) {
                    player.runCommandAsync("setblock -1968 200 0 air")
                    player.runCommandAsync("tag @s remove course3")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("playsound respawn_anchor.charge")
                    player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ems" } ] }')
                    let highscores = mc.world.scoreboard.getObjective("highscore")
                    var highscoreFinal = 0
                    for (let highscore of highscores.getScores()) {
                        if (highscore.participant.displayName == "course3") {
                            highscoreFinal = highscore.score
                            break
                        }
                    }
                    var time = 0
                    let scores = mc.world.scoreboard.getObjective("parkour")
                    for (let score of scores.getScores()) {
                        if (score.participant.displayName == "Time") {
                            time = score.score
                            break
                        }
                    }
                    if (highscoreFinal == 0) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this course for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((300000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set course3 highscore ${time}`)
                    } else if (highscoreFinal == time) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(300000/score.score)}`)
                    } else if (highscoreFinal < time) {
                        let difference = time - highscoreFinal
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "course3", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(300000/score.score)}`)
                    } else {
                        let difference = highscoreFinal - time
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "course3", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((300000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set course3 highscore ${time}`)
                    }
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("tp -2000 0 0")
                        player.runCommandAsync("tag @s add course3")
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                        player.runCommandAsync("function bParkour/course/clear3")
                    }, 60)
                }
                if (player.hasTag("course3") && player.location.y <= -4) {
                    player.runCommandAsync("tp -2000 0 0")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("setblock -1968 200 0 air")
                    player.runCommandAsync(`clear @s ${parkourBlock}`)
                    player.runCommandAsync("title @s subtitle §cYou died!§r")
                    player.runCommandAsync("title @s title §r")
                    player.runCommandAsync("playsound note.bass")
                    player.runCommandAsync("function bParkour/course/clear3")
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                    }, 4)
                }
                if (player.hasTag("customCourse") && player.location.x >= -1950 && player.location.y >= -1 && player.location.y < 1) {
                    player.runCommandAsync("setblock -1975 200 1000 air")
                    player.runCommandAsync("tag @s remove customCourse")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("playsound respawn_anchor.charge")
                    player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ems" } ] }')
                    let highscores = mc.world.scoreboard.getObjective("highscore")
                    var highscoreFinal = 0
                    for (let highscore of highscores.getScores()) {
                        if (highscore.participant.displayName == "customCourse") {
                            highscoreFinal = highscore.score
                            break
                        }
                    }
                    var time = 0
                    let scores = mc.world.scoreboard.getObjective("parkour")
                    for (let score of scores.getScores()) {
                        if (score.participant.displayName == "Time") {
                            time = score.score
                            break
                        }
                    }
                    if (highscoreFinal == 0) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this course for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((50000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set customCourse highscore ${time}`)
                    } else if (highscoreFinal == time) {
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(50000/score.score)}`)
                    } else if (highscoreFinal < time) {
                        let difference = time - highscoreFinal
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "customCourse", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(50000/score.score)}`)
                    } else {
                        let difference = highscoreFinal - time
                        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "parkour" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "customCourse", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                        for (let score of mc.world.scoreboard.getObjective("parkour").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((50000/score.score)*1.5)}`)
                        player.runCommandAsync(`scoreboard players set customCourse highscore ${time}`)
                    }
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("tp -2000 0 1000")
                        player.runCommandAsync("tag @s add customCourse")
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                        player.runCommandAsync("function bParkour/course/clearCustom")
                        player.runCommandAsync("replaceitem entity @s slot.hotbar 4 pada:start")
                    }, 60)
                }
                if (player.hasTag("customCourse") && player.location.y <= -4) {
                    player.runCommandAsync("tp -2000 0 1000")
                    player.runCommandAsync("tag @s remove courseStarted")
                    player.runCommandAsync("setblock -1975 200 1000 air")
                    player.runCommandAsync(`clear @s ${parkourBlock}`)
                    player.runCommandAsync("title @s subtitle §cYou died!§r")
                    player.runCommandAsync("title @s title §r")
                    player.runCommandAsync("playsound note.bass")
                    player.runCommandAsync("function bParkour/course/clearCustom")
                    mc.system.runTimeout(() => {
                        player.runCommandAsync("scoreboard players set Time parkour 0")
                    }, 4)
                }
                if (!player.hasTag("courseStarted") && player.hasTag("customCourse")) {
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 0 quartz_block 64")
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 1 netherite_pickaxe")
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 4 pada:start")
                }
            }
            
            // Course Start

            if ((player.getVelocity().x > 0 || player.getVelocity().y > 0 || player.getVelocity().z > 0) && player.hasTag("parkourCourse") && !player.hasTag("courseStarted") && !player.hasTag("customCourse")) {
                parkour(player)
            }
            
            // Bridging

            if (player.hasTag("bridge")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§bTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §bms" } ] }')
                if (player.location.y <= -10) {
                    if (player.hasTag("normal")) {
                        player.runCommandAsync("function bridge/normal/death")
                        mc.system.runTimeout(() => {
                            player.runCommandAsync("scoreboard players set Time bridge 0")
                        }, 5)
                    }
                    if (player.hasTag("short")) {
                        player.runCommandAsync("function bridge/short/death")
                        mc.system.runTimeout(() => {
                            player.runCommandAsync("scoreboard players set Time bridge 0")
                        }, 5)
                    }
                    if (player.hasTag("diagonal")) {
                        player.runCommandAsync("function bridge/diagonal/death")
                        mc.system.runTimeout(() => {
                            player.runCommandAsync("scoreboard players set Time bridge 0")
                        }, 5)
                    }
                    if (player.hasTag("onestack")) {
                        player.runCommandAsync("function bridge/onestack/death")
                        mc.system.runTimeout(() => {
                            player.runCommandAsync("scoreboard players set Time bridge 0")
                        }, 5)
                    }
                    if (player.hasTag("downstack")) {
                        player.runCommandAsync("function bridge/downstack/death")
                        mc.system.runTimeout(() => {
                            player.runCommandAsync("scoreboard players set Time bridge 0")
                        }, 5)
                    }
                }

                // Bridging items
                player.runCommandAsync(`replaceitem entity @s slot.hotbar 0 ${bridgeBlock} 64`)
                player.runCommandAsync("replaceitem entity @s slot.hotbar 7 pada:settings")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 8 pada:hub")

                // Finish Line
                if (player.hasTag("bridgeStarted")) {
                    if (player.hasTag("normal")) {
                        if (player.location.x >= 38 && player.location.y >= 3 && player.location.y < 5) {
                            player.runCommandAsync("tag @s remove bridgeStarted")
                            player.runCommandAsync("setblock 25 200 2000 air")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ems" } ] }')
                            player.runCommandAsync("playsound random.levelup")
                            let highscores = mc.world.scoreboard.getObjective("highscore")
                            var highscoreFinal = 0
                            for (let highscore of highscores.getScores()) {
                                if (highscore.participant.displayName == "normal") {
                                    highscoreFinal = highscore.score
                                    break
                                }
                            }
                            var time = 0
                            let scores = mc.world.scoreboard.getObjective("bridge")
                            for (let score of scores.getScores()) {
                                if (score.participant.displayName == "Time") {
                                    time = score.score
                                    break
                                }
                            }
                            if (highscoreFinal == 0) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this bridge for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((80000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set normal highscore ${time}`)
                            } else if (highscoreFinal == time) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(80000/score.score)}`)
                            } else if (highscoreFinal < time) {
                                let difference = time - highscoreFinal
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "normal", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(80000/score.score)}`)
                            } else {
                                let difference = highscoreFinal - time
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "normal", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((80000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set normal highscore ${time}`)
                            }
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("function bridge/clear")
                                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000")
                                player.runCommandAsync("scoreboard players set Time bridge 0")
                                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                            }, 60)
                        }
                    }
                    if (player.hasTag("short")) {
                        if (player.location.x >= 18 && player.location.y >= -1 && player.location.y < 1) {
                            player.runCommandAsync("tag @s remove bridgeStarted")
                            player.runCommandAsync("setblock 25 200 2000 air")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ems" } ] }')
                            player.runCommandAsync("playsound random.levelup")
                            let highscores = mc.world.scoreboard.getObjective("highscore")
                            var highscoreFinal = 0
                            for (let highscore of highscores.getScores()) {
                                if (highscore.participant.displayName == "short") {
                                    highscoreFinal = highscore.score
                                    break
                                }
                            }
                            var time = 0
                            let scores = mc.world.scoreboard.getObjective("bridge")
                            for (let score of scores.getScores()) {
                                if (score.participant.displayName == "Time") {
                                    time = score.score
                                    break
                                }
                            }
                            if (highscoreFinal == 0) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this bridge for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((12000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set short highscore ${time}`)
                            } else if (highscoreFinal == time) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(12000/score.score)}`)
                            } else if (highscoreFinal < time) {
                                let difference = time - highscoreFinal
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "short", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(12000/score.score)}`)
                            } else {
                                let difference = highscoreFinal - time
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "short", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((12000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set short highscore ${time}`)
                            }
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("function bridge/clear")
                                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000")
                                player.runCommandAsync("scoreboard players set Time bridge 0")
                                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                            }, 60)
                        }
                    }
                    if (player.hasTag("diagonal")) {
                        if (player.location.x - player.location.z > -1957 && player.location.x > 19 && player.location.z < 1981 && player.location.y >= -1 && player.location.y < 1) {
                            player.runCommandAsync("tag @s remove bridgeStarted")
                            player.runCommandAsync("setblock 25 200 2000 air")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ems" } ] }')
                            player.runCommandAsync("playsound random.levelup")
                            let highscores = mc.world.scoreboard.getObjective("highscore")
                            var highscoreFinal = 0
                            for (let highscore of highscores.getScores()) {
                                if (highscore.participant.displayName == "diagonal") {
                                    highscoreFinal = highscore.score
                                    break
                                }
                            }
                            var time = 0
                            let scores = mc.world.scoreboard.getObjective("bridge")
                            for (let score of scores.getScores()) {
                                if (score.participant.displayName == "Time") {
                                    time = score.score
                                    break
                                }
                            }
                            if (highscoreFinal == 0) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this bridge for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((100000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set diagonal highscore ${time}`)
                            } else if (highscoreFinal == time) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(100000/score.score)}`)
                            } else if (highscoreFinal < time) {
                                let difference = time - highscoreFinal
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "diagonal", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(100000/score.score)}`)
                            } else {
                                let difference = highscoreFinal - time
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "diagonal", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((100000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set diagonal highscore ${time}`)
                            }
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("function bridge/clear")
                                player.runCommandAsync("tp 0 0 2000 facing 1 0 1999")
                                player.runCommandAsync("scoreboard players set Time bridge 0")
                                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                            }, 60)
                        }
                    }
                    if (player.hasTag("onestack")) {
                        if (player.location.x >= 22 && player.location.y >= 15 && player.location.y < 17) {
                            player.runCommandAsync("tag @s remove bridgeStarted")
                            player.runCommandAsync("setblock 25 200 2000 air")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ems" } ] }')
                            player.runCommandAsync("playsound random.levelup")
                            let highscores = mc.world.scoreboard.getObjective("highscore")
                            var highscoreFinal = 0
                            for (let highscore of highscores.getScores()) {
                                if (highscore.participant.displayName == "onestack") {
                                    highscoreFinal = highscore.score
                                    break
                                }
                            }
                            var time = 0
                            let scores = mc.world.scoreboard.getObjective("bridge")
                            for (let score of scores.getScores()) {
                                if (score.participant.displayName == "Time") {
                                    time = score.score
                                    break
                                }
                            } if (highscoreFinal == 0) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this bridge for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((50000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set onestack highscore ${time}`)
                            } else if (highscoreFinal == time) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(50000/score.score)}`)
                            } else if (highscoreFinal < time) {
                                let difference = time - highscoreFinal
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "onestack", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(50000/score.score)}`)
                            } else {
                                let difference = highscoreFinal - time
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "onestack", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((50000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set onestack highscore ${time}`)
                            }
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("function bridge/clear")
                                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000")
                                player.runCommandAsync("scoreboard players set Time bridge 0")
                                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                            }, 60)
                        }
                    }
                    if (player.hasTag("downstack")) {
                        if (player.location.x >= 22 && player.location.y >= -1 && player.location.y < 1) {
                            player.runCommandAsync("tag @s remove bridgeStarted")
                            player.runCommandAsync("setblock 25 200 2000 air")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ems" } ] }')
                            player.runCommandAsync("playsound random.levelup")
                            let highscores = mc.world.scoreboard.getObjective("highscore")
                            var highscoreFinal = 0
                            for (let highscore of highscores.getScores()) {
                                if (highscore.participant.displayName == "downstack") {
                                    highscoreFinal = highscore.score
                                    break
                                }
                            }
                            var time = 0
                            let scores = mc.world.scoreboard.getObjective("bridge")
                            for (let score of scores.getScores()) {
                                if (score.participant.displayName == "Time") {
                                    time = score.score
                                    break
                                }
                            }
                            if (highscoreFinal == 0) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this bridge for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((20000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set downstack highscore ${time}`)
                            } else if (highscoreFinal == time) {
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(20000/score.score)}`)
                            } else if (highscoreFinal < time) {
                                let difference = time - highscoreFinal
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "downstack", "objective": "highscore" } }, { "text": " §ams." } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(20000/score.score)}`)
                            } else {
                                let difference = highscoreFinal - time
                                player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "bridge" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "downstack", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
                                for (let score of mc.world.scoreboard.getObjective("bridge").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((20000/score.score)*1.5)}`)
                                player.runCommandAsync(`scoreboard players set downstack highscore ${time}`)
                            }
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("function bridge/clear")
                                player.runCommandAsync("tp 0 16 2000 facing 1 16 2000")
                                player.runCommandAsync("scoreboard players set Time bridge 0")
                                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                            }, 60)
                        }
                    }
                }
            }

            if (player.hasTag("breaking")) {
                // Treasure Breaking items
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§bTime: §f" }, { "score": {"name": "Time", "objective": "breaking" } }, { "text": " §bms" } ] }')
                if (!mc.world.scoreboard.getObjective("inventoryBreaking")) {
                    player.runCommandAsync("scoreboard objectives add inventoryBreaking dummy inventory")
                    player.runCommandAsync("scoreboard players set sword inventoryBreaking 0")
                    player.runCommandAsync("scoreboard players set pickaxe inventoryBreaking 1")
                    player.runCommandAsync("scoreboard players set axe inventoryBreaking 2")
                    player.runCommandAsync("scoreboard players set shears inventoryBreaking 3")
                    player.runCommandAsync("scoreboard players set blocks inventoryBreaking 4")
                    player.runCommandAsync("scoreboard players set settings inventoryBreaking 7")
                    player.runCommandAsync("scoreboard players set hub inventoryBreaking 8")
                } else {
                    let scoreboard = mc.world.scoreboard.getObjective("inventoryBreaking")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == "sword") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} stone_sword`)
                        }
                        if (score.participant.displayName == "pickaxe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} stone_pickaxe`)
                        }
                        if (score.participant.displayName == "axe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} stone_axe`)
                        }
                        if (score.participant.displayName == "shears") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} shears`)
                        }
                        if (score.participant.displayName == "blocks") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} ${breakingBlock} 64`)
                        }
                        if (score.participant.displayName == "settings") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:settings`)
                        }
                        if (score.participant.displayName == "hub") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:hub`)
                        }
                    }
                }

            }

            // Treasure Breaking Settings Items
            
            if (player.hasTag("breakingSettings")) {
                player.runCommandAsync("replaceitem entity @s slot.hotbar 1 iron_axe")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 0 iron_pickaxe")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 2 wool 64 14")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 3 planks 64")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 4 end_stone 64")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 5 concrete 64 14")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 6 stained_glass 64 14")
            }
            
            // Bow items
            if (player.hasTag("bow")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eStreak: §f" }, { "score": {"name": "Streak", "objective": "bow" } } ] }')
                player.runCommandAsync("replaceitem entity @s slot.hotbar 0 bow")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 7 pada:settings")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 8 pada:hub")
                player.runCommandAsync("replaceitem entity @s slot.inventory 0 arrow")

                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 11 kill @e[type=villager,c=1]")
                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 11 summon villager 8 3 -1980 minecraft:entity_born")
                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 11 tp -7 ~ -1981")
                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 14 kill @e[type=villager,c=1]")
                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 14 summon villager -8 3 -1980 minecraft:entity_born")
                player.runCommandAsync("execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ wool 14 tp 7 ~ -1981")

                player.runCommandAsync("execute @e[type=pada:pvpbot] ~ ~ ~ detect ~ ~-1 ~ barrier 0 tag @a add botDied")
                if (player.hasTag("botDied")) {
                    player.runCommandAsync("kill @e[type=pada:pvpbot,c=1]")
                    player.runCommandAsync("tag @s remove botDied")
                    let random = Math.floor(Math.random() * 19)
                    let newX = 9 - random
                    player.runCommandAsync(`summon pada:pvpbot ${newX} 6 -1980 `)
                    player.runCommandAsync("scoreboard players set Streak bow 0")
                }
            }

            // Hotbar Switching
            if (player.hasTag("hotbar")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§eStreak: §f" }, { "score": {"name": "Streak", "objective": "hotbar" } } ] }')
                player.runCommandAsync("kill @e[type=ender_pearl]")
                if (player.getComponent("equipment_inventory").getEquipment("mainhand")) {
                    if (player.getComponent("equipment_inventory").getEquipment("mainhand").typeId == newItem[0]) {
                        player.runCommandAsync("playsound random.orb")
                        player.runCommandAsync("scoreboard players add Streak hotbar 1")
                        oldItem = newItem
                        newItem = hotbarItems[ Math.floor(Math.random() * 9) ]
                        player.runCommandAsync(`title @s title ${newItem[1]}`)
                        player.runCommandAsync("tag @s remove wrong")
                        player.runCommandAsync(`scoreboard players add @s money 1`)
                    } else if ((player.getComponent("equipment_inventory").getEquipment("mainhand").typeId != oldItem[0] && newItem[0]) && !player.hasTag("wrong")) {
                        player.runCommandAsync("tag @s add wrong")
                        player.runCommandAsync("playsound note.bass")
                        player.runCommandAsync("scoreboard players set Streak hotbar 0")
                    }
                }
            }
            
            if (player.hasTag("hotbar")) {
                // Hotbar Inventory
                if (!mc.world.scoreboard.getObjective("inventoryHotbar")) {
                    player.runCommandAsync("scoreboard objectives add inventoryHotbar dummy inventory")
                    player.runCommandAsync("scoreboard players set sword inventoryHotbar 0")
                    player.runCommandAsync("scoreboard players set pickaxe inventoryHotbar 1")
                    player.runCommandAsync("scoreboard players set axe inventoryHotbar 2")
                    player.runCommandAsync("scoreboard players set blocks inventoryHotbar 3")
                    player.runCommandAsync("scoreboard players set snowball inventoryHotbar 4")
                    player.runCommandAsync("scoreboard players set bow inventoryHotbar 5")
                    player.runCommandAsync("scoreboard players set pearl inventoryHotbar 6")
                    player.runCommandAsync("scoreboard players set tnt inventoryHotbar 7")
                    player.runCommandAsync("scoreboard players set gapple inventoryHotbar 8")
                } else {
                    let scoreboard = mc.world.scoreboard.getObjective("inventoryHotbar")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == "sword") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} iron_sword`)
                        }
                        if (score.participant.displayName == "pickaxe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} iron_pickaxe`)
                        }
                        if (score.participant.displayName == "axe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} iron_axe`)
                        }
                        if (score.participant.displayName == "blocks") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} stone 64`)
                        }
                        if (score.participant.displayName == "snowball") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} snowball 16`)
                        }
                        if (score.participant.displayName == "bow") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} bow`)
                        }
                        if (score.participant.displayName == "pearl") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} ender_pearl 16`)
                        }
                        if (score.participant.displayName == "tnt") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} tnt`)
                        }
                        if (score.participant.displayName == "gapple") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} golden_apple 64`)
                        }
                    }
                }
            }

            // Ore Mining
            if (player.hasTag("ore")) {
                if (player.hasTag("iron")) {
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 0 iron_pickaxe")
                } else if (player.hasTag("diamond")) {
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 0 diamond_pickaxe")
                } else if (player.hasTag("netherite")) {
                    player.runCommandAsync("replaceitem entity @s slot.hotbar 0 netherite_pickaxe")
                }

                player.runCommandAsync("replaceitem entity @s slot.hotbar 7 pada:settings")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 8 pada:hub")

                if (player.hasTag("oreStarted")) {
                    if (Math.floor(player.getVelocity().x * 10) == 0 && Math.floor(player.getVelocity().z * 10) == 0) {
                        player.runCommandAsync("function ore/reset")
                        player.runCommandAsync("playsound note.bass")
                        var random = Math.floor(Math.random() * 8 + 1);
                        if (player.hasTag("easy")) {
                            player.runCommandAsync(`structure load "ore/easy${random}" 2000 -1 -2005`)
                        } else {
                            player.runCommandAsync(`structure load "ore/${random}" 2000 -1 -2005`)
                        }
                    }
                } else {
                    if (player.getVelocity().x < 0 || player.getVelocity().x > 0 || player.getVelocity().z < 0 || player.getVelocity().z > 0) {
                        player.runCommandAsync("tag @s add oreStarted")
                    }
                }
                if (player.location.y <= -5) {
                    player.runCommandAsync("function ore/reset")
                    player.runCommandAsync("playsound note.bass")
                    var random = Math.floor(Math.random() * 8 + 1);
                    if (player.hasTag("easy")) {
                            player.runCommandAsync(`structure load "ore/easy${random}" 2000 -1 -2005`)
                        } else {
                            player.runCommandAsync(`structure load "ore/${random}" 2000 -1 -2005`)
                        }
                }
            }

            if (player.hasTag("defense")) {
                // Treasure Defense items
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§bTime: §f" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §bms" } ] }')
                if (!mc.world.scoreboard.getObjective("inventoryDefense")) {
                    player.runCommandAsync("scoreboard objectives add inventoryDefense dummy inventory")
                    player.runCommandAsync("scoreboard players set pickaxe inventoryDefense 0")
                    player.runCommandAsync("scoreboard players set axe inventoryDefense 1")
                    player.runCommandAsync("scoreboard players set wool inventoryDefense 2")
                    player.runCommandAsync("scoreboard players set planks inventoryDefense 3")
                    player.runCommandAsync("scoreboard players set concrete inventoryDefense 4")
                    player.runCommandAsync("scoreboard players set endstone inventoryDefense 5")
                    player.runCommandAsync("scoreboard players set glass inventoryDefense 6")
                    player.runCommandAsync("scoreboard players set settings inventoryDefense 7")
                    player.runCommandAsync("scoreboard players set hub inventoryDefense 8")
                } else {
                    let scoreboard = mc.world.scoreboard.getObjective("inventoryDefense")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == "pickaxe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} iron_pickaxe`)
                        }
                        if (score.participant.displayName == "axe") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} iron_axe`)
                        }
                        if (score.participant.displayName == "wool") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} wool 64`)
                        }
                        if (score.participant.displayName == "planks") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} planks 64`)
                        }
                        if (score.participant.displayName == "concrete") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} concrete 64`)
                        }
                        if (score.participant.displayName == "endstone") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} end_stone 64`)
                        }
                        if (score.participant.displayName == "glass") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} glass 64`)
                        }
                        if (score.participant.displayName == "settings") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:settings`)
                        }
                        if (score.participant.displayName == "hub") {
                            player.runCommandAsync(`replaceitem entity @s slot.hotbar ${score.score} pada:hub`)
                        }
                    }
                }

            }

            if (player.hasTag("defenseStarted")) {
                var dimension = mc.world.getDimension("overworld")
                if (!layers[0]) {
                } else {
                    if (dimension.getBlock({ x: -1981, y: 5, z: 1997 }).typeId == layers[0] && dimension.getBlock({ x: -1980, y: 5, z: 1998 }).typeId == layers[0] && dimension.getBlock({ x: -1982, y: 5, z: 1998 }).typeId == layers[0] && dimension.getBlock({ x: -1981, y: 5, z: 1999 }).typeId == layers[0] && dimension.getBlock({ x: -1981, y: 6, z: 1998 }).typeId == layers[0]) {
                        if (layers[1] == undefined) {
                            defenseFinish(player)
                        } else {
                            if (dimension.getBlock({ x: -1981+2, y: 5, z: 1998 }).typeId == layers[1] && dimension.getBlock({ x: -1981-2, y: 5, z: 1998 }).typeId == layers[1] && dimension.getBlock({ x: -1981, y: 5, z: 1998+2 }).typeId == layers[1] && dimension.getBlock({ x: -1981, y: 5, z: 1998-2 }).typeId == layers[1] && dimension.getBlock({ x: -1981, y: 5+2, z: 1998 }).typeId == layers[1] && dimension.getBlock({ x: -1981+1, y: 5, z: 1998+1 }).typeId == layers[1] && dimension.getBlock({ x: -1981+1, y: 5, z: 1998-1 }).typeId == layers[1] && dimension.getBlock({ x: -1981-1, y: 5, z: 1998+1 }).typeId == layers[1] && dimension.getBlock({ x: -1981-1, y: 5, z: 1998-1 }).typeId == layers[1] && dimension.getBlock({ x: -1981+1, y: 5+1, z: 1998 }).typeId == layers[1] && dimension.getBlock({ x: -1981-1, y: 5+1, z: 1998 }).typeId == layers[1] && dimension.getBlock({ x: -1981, y: 5+1, z: 1998+1 }).typeId == layers[1] && dimension.getBlock({ x: -1981, y: 5+1, z: 1998-1 }).typeId == layers[1]) {
                                defenseFinish(player)
                            }
                        }
                    }
                }
            }

            // Chest Looting
            if (player.hasTag("chest")) {
                player.runCommandAsync('titleraw @s actionbar { "rawtext": [ { "text": "§bTime: §f" }, { "score": {"name": "Time", "objective": "chest" } }, { "text": " §bms" } ] }')
                player.runCommandAsync("replaceitem entity @s slot.hotbar 8 pada:hub")
                player.runCommandAsync("replaceitem entity @s slot.hotbar 7 pada:settings")
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                var empty = 0
                if (player.hasTag("chestStarted")) {
                    for (let slot = 0; slot < 27; slot++) {
                        if (block.getComponent("inventory").container.getItem(slot) == undefined) {
                            empty++
                        } else if (block.getComponent("inventory").container.getItem(slot).typeId == "minecraft:web") {
                            empty++
                        }
                    }
                    if (empty == 27) {
                        player.runCommandAsync("clear")
                        // player.runCommandAsync("effect @s instant_damage 1 0 true")
                        mc.system.runTimeout(() => {
                            chestRefill()
                            // player.runCommandAsync("effect @s instant_health 1 255 true")
                            player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "chest" } }, { "text": " §ems" } ] }')
                            for (let score of mc.world.scoreboard.getObjective("chest").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(1000/score.score)}`)
                            player.runCommandAsync("clear")
                            player.runCommandAsync("setblock 2000 200 2000 air")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync("scoreboard players set Time chest 0")
                                player.runCommandAsync("tag @s remove chestStarted")
                            }, 10)
                        }, 3)
                    }
                    for (let pslot = 0; pslot < 36; pslot++) {
                        if (player.getComponent("inventory").container.getItem(pslot) == undefined) {
                            continue
                        } else if (player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:web") {
                            player.runCommandAsync("title @s title §cFailed")
                            mc.system.runTimeout(() => {
                                chestRefill()
                                player.runCommandAsync("clear")
                                player.runCommandAsync("setblock 2000 200 2000 air")
                                mc.system.runTimeout(() => {
                                    player.runCommandAsync("scoreboard players set Time chest 0")
                                    player.runCommandAsync("tag @s remove chestStarted")
                                }, 3)
                            }, 5)
                            break
                        }
                    }
                } else if (!player.hasTag("chestStarted")) {
                    for (let pslot = 0; pslot < 36; pslot++) {
                        if (player.getComponent("inventory").container.getItem(pslot) == undefined) {
                            continue
                        } else if (player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:diamond_sword" || player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:diamond_helmet" || player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:diamond_chestplate" || player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:diamond_leggings" || player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:diamond_boots" || player.getComponent("inventory").container.getItem(pslot).typeId == "minecraft:web") {
                            player.runCommandAsync("setblock 2000 200 2000 redstone_block")
                            player.runCommandAsync("tag @s add chestStarted")
                        }
                    }
                }
            }

            // Effects
            player.runCommandAsync("effect @s saturation 999999 255 true")
            if (player.hasTag("slowFalling")) {
                player.runCommandAsync("effect @s slow_falling 1 1 true")
            }
            if (player.hasTag("speed")) {
                player.runCommandAsync("effect @s speed 1 2 true")
            }
            if (player.hasTag("superEasy")) {
                player.runCommandAsync("effect @e[type=pada:pvpbot] slowness 999999 255 true")
            }
        }
        mc.system.run(tick)
    }
)

mc.system.runInterval(() => {
    for (let player of mc.world.getPlayers()) {
        if (player.hasTag("pvpSlowFalling")) {
            player.runCommandAsync("effect @e[type=pada:pvpbot] slow_falling 1 1 true")
            mc.system.runTimeout(() => {
                player.runCommandAsync("effect @e[type=pada:pvpbot] clear")
            }, 4)
        }
    }
}, 5)

// Clutchbot in pearl clutching
mc.system.runInterval(
    () => {
        // mc.world.getDimension("overworld").runCommandAsync("say yo")
        for (let player of mc.world.getPlayers()) {
            if (player.hasTag("baroque") && player.location.y >= 3) {
                player.runCommandAsync("event entity @e[type=pada:clutchbot,name=pearl] pada:hit1")
            }
            if (player.hasTag("wildwood") && player.location.y >= 8) {
                player.runCommandAsync("event entity @e[type=pada:clutchbot,name=pearl] pada:hit1")
            }
            if (player.hasTag("village") && player.location.y >= -3) {
                player.runCommandAsync("event entity @e[type=pada:clutchbot,name=pearl] pada:hit1")
      		}
            if (player.hasTag("flat") && player.location.y >= -1) {
                player.runCommandAsync("event entity @e[type=pada:clutchbot,name=pearl] pada:hit1")
      		}
        }
    }, 30
)

mc.world.afterEvents.blockPlace.subscribe(data => {
    // Build limits
    if (data.player.hasTag("pearl")) {
        if (data.block.y >= 16 || data.block.y <= -30) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou reached max height!§r')
        }
        if (data.player.hasTag("baroque")) {
            if (data.block.x <= 985 || data.block.x >= 1019 || data.block.z <= -40 || data.block.z >= 32) {
                data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
                data.player.sendMessage('§cYou cannot build further than this!§r')
            }
        }
        if (data.player.hasTag("wildwood")) {
            if (data.block.x <= 988 || data.block.x >= 1043 || data.block.z <= 985 || data.block.z >= 1014) {
                data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
                data.player.sendMessage('§cYou cannot build further than this!§r')
            }
        }
        if (data.player.hasTag("village")) {
            if (data.block.x <= 989 || data.block.x >= 1064 || data.block.z <= -1022 || data.block.z >= -980) {
                data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
                data.player.sendMessage('§cYou cannot build further than this!§r')
            }
            if (data.block.y >= 8 || data.block.y <= -20) {
                data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
                data.player.sendMessage('§cYou reached max height!§r')
            }
        }
        if (data.player.hasTag("flat")) {
            if (data.block.x <= 1994 || data.block.x >= 2036 || data.block.z <= -9 || data.block.z >= 9) {
                data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
                data.player.sendMessage('§cYou cannot build further than this!§r')
            }
        }
    }
    if (data.player.hasTag("pvp")) {
        if (data.block.y > 10) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou reached max height!§r')
        }
    }
    if (data.player.hasTag("course1")) {
        if (data.block.z < 1000 || data.block.x >= -990 || data.block.x <= -1004 || data.block.z >= 1081 || data.block.y >= 9) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
    if (data.player.hasTag("course2")) {
        if (data.block.x < -1000 || data.block.z >= -990 || data.block.z <= -1010 || data.block.x >= -948 || data.block.y >= 10) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
    if (data.player.hasTag("course3")) {
        if (data.block.x < -2000 || data.block.z >= 10 || data.block.z <= -10 || data.block.x >= -1932 || data.block.y >= 16) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
    if (data.player.hasTag("customCourse")) {
        if (data.block.x < -2000 || data.block.z > 1010 || data.block.z < 990 || data.block.x > -1948 || data.block.y >= 15) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }

    if (data.player.hasTag("bridge")) {
        if (data.block.x <= -5 || data.block.x >= 55 || data.block.z >= 2012 || data.block.z <= 1972 || data.block.y <= -10 || data.block >= 20) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
    
    if (data.player.hasTag("breaking")) {
        if (data.block.x > 1019 || data.block.x < 1007 || data.block.y > 10 || data.block.z > 2014 || data.block.z < 1986) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }

    if (data.player.hasTag("breakingSettings")) {
        if (data.block.x > 1016 || data.block.x < 1010 || data.block.y > 204 || data.block.z > 2003 || data.block.z < 1997) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
    
    if (data.player.hasTag("defense")) {
        if (data.block.x > -1975 || data.block.x < -1987 || data.block.y > 10 || data.block.z > 2012 || data.block.z < 1984) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }

    if (data.player.hasTag("defenseSettings")) {
        if (data.block.x > -1978 || data.block.x < -1984 || data.block.y > 205 || data.block.z > 2001 || data.block.z < 1995) {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
            data.player.sendMessage('§cYou cannot build further than this!§r')
        }
    }
})


mc.world.afterEvents.blockPlace.subscribe(data => {
    // Blocks counter
    if (data.player.hasTag("extension")) {
        data.player.runCommandAsync(`scoreboard players add Blocks extension 1`)
    }
    if (data.player.hasTag("bridge")) {
        data.player.runCommandAsync("scoreboard players add Blocks bridge 1")
    }

    // Bridge Start
    if (data.player.hasTag("bridge") && !data.player.hasTag("bridgeStarted")) {
        data.player.runCommandAsync("setblock 25 200 2000 redstone_block")
        data.player.runCommandAsync("tag @s add bridgeStarted")
    }

    // Treasure Breaking Start
    if (data.player.hasTag("breaking") && !data.player.hasTag("breakingStarted")) {
        data.player.runCommandAsync("setblock 1022 2 2009 redstone_block")
        data.player.runCommandAsync("tag @s add breakingStarted")
        data.player.runCommandAsync("summon pada:pvpbot 1009 4 1996")
        data.player.runCommandAsync("summon pada:pvpbot 1017 4 1996")
        data.player.runCommandAsync("summon pada:pvpbot 1017 4 2004")
        data.player.runCommandAsync("summon pada:pvpbot 1009 4 2004")
        data.player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hard")
        data.player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
    }

    // Treasure Defense Start
    if (data.player.hasTag("defense") && !data.player.hasTag("defenseStarted")) {
        data.player.runCommandAsync("setblock -1972 3 2005 redstone_block")
        data.player.runCommandAsync("tag @s add defenseStarted")
    }
})

mc.world.afterEvents.blockBreak.subscribe(data => {
    // Treasure Breaking Start
    if (data.player.hasTag("breaking") && !data.player.hasTag("breakingStarted")) {
        data.player.runCommandAsync("setblock 1022 2 2009 redstone_block")
        data.player.runCommandAsync("tag @s add breakingStarted")
        data.player.runCommandAsync("summon pada:pvpbot 1009 4 1996")
        data.player.runCommandAsync("summon pada:pvpbot 1017 4 1996")
        data.player.runCommandAsync("summon pada:pvpbot 1017 4 2004")
        data.player.runCommandAsync("summon pada:pvpbot 1009 4 2004")
        data.player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hard")
        data.player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
    }

    // Ore Mining success
    if (data.player.hasTag("ore")) {
        if (!data.player.hasTag("oreStarted")) {
            data.player.sendMessage("§aYou must mine the ore while walking§r")
            data.player.runCommandAsync("function ore/reset")
            data.player.runCommandAsync("playsound note.bass")
            var random = Math.floor(Math.random() * 8 + 1);
            if (data.player.hasTag("easy")) {
                data.player.runCommandAsync(`structure load "ore/easy${random}" 2000 -1 -2005`)
            } else {
                data.player.runCommandAsync(`structure load "ore/${random}" 2000 -1 -2005`)
            }
        } else {
            data.player.runCommandAsync("function ore/reset")
            data.player.runCommandAsync("playsound random.orb")
            var random = Math.floor(Math.random() * 8 + 1);
            if (data.player.hasTag("easy")) {
                data.player.runCommandAsync(`structure load "ore/easy${random}" 2000 -1 -2005`)
                data.player.runCommandAsync(`scoreboard players add @s money 1`)
            } else {
                data.player.runCommandAsync(`structure load "ore/${random}" 2000 -1 -2005`)
                data.player.runCommandAsync(`scoreboard players add @s money 3`)
            }
        }
    }
})

mc.world.afterEvents.entityDie.subscribe(data => {
    if (data.deadEntity.typeId == "minecraft:player" && data.deadEntity.hasTag("breaking")) {
        data.deadEntity.runCommandAsync("title @s subtitle §cYou died!")
        data.deadEntity.runCommandAsync("title @s title §r")
        data.deadEntity.runCommandAsync("function breaking/reset")
        mc.system.runTimeout(() => {
            data.deadEntity.runCommandAsync("summon pada:treasure 1013 4 2000")
            data.deadEntity.runCommandAsync("scoreboard players set Time breaking 0")
        }, 5)
    }
})

mc.world.afterEvents.projectileHit.subscribe(data => {
    if (data.source.hasTag("bow")) {
        if (data.getBlockHit()) {
            if (data.getBlockHit().block.typeId == "minecraft:target") {
                data.source.runCommandAsync("playsound random.orb")
                let random1 = Math.floor(Math.random() * 19)
                let random2 = Math.floor(Math.random() * 9)
                let newX = 9 - random1
                let newY = -1 + random2
                data.source.runCommandAsync(`setblock ${data.getBlockHit().block.location.x} ${data.getBlockHit().block.location.y} ${data.getBlockHit().block.location.z} planks`)
                data.source.runCommandAsync(`setblock ${newX} ${newY} -1981 target`)
                data.source.runCommandAsync("scoreboard players add Streak bow 1")
                data.source.runCommandAsync(`scoreboard players add @s money 1`)
            } else {
                data.source.runCommandAsync("scoreboard players set Streak bow 0")
            }
        }
        if (data.getEntityHit()) {
            if (data.getEntityHit().entity.typeId == "minecraft:zombie") {
                data.source.runCommandAsync("kill @e[type=zombie,c=1]")
                let random1 = Math.floor(Math.random() * 17)
                let random2 = Math.floor(Math.random() * 4) * 2
                let newX = 8 - random1
                let newY = 0 + random2
                data.source.runCommandAsync(`summon minecraft:zombie ${newX} ${newY} -1981 minecraft:as_baby`)
                data.source.runCommandAsync("scoreboard players add Streak bow 1")
                data.source.runCommandAsync("scoreboard players add @s money 2")
            } else if (data.getEntityHit().entity.typeId == "pada:pvpbot") {
                data.source.runCommandAsync("kill @e[type=pada:pvpbot,c=1]")
                let random = Math.floor(Math.random() * 19)
                let newX = 9 - random
                data.source.runCommandAsync(`summon pada:pvpbot ${newX} 6 -1980 `)
                data.source.runCommandAsync("scoreboard players add Streak bow 1")
                data.source.runCommandAsync("scoreboard players add @s money 3")
            } else {
                data.source.runCommandAsync("scoreboard players set Streak bow 0")
            }
        }
    }
    if (data.getBlockHit()) {
        data.projectile.runCommandAsync("kill")
    }
    if (data.getEntityHit()) {
        data.source.runCommandAsync("playsound random.orb @s")
        if (data.projectile.typeId == "minecraft:fishing_hook") {
            data.projectile.runCommandAsync("kill")
        }
    }
})


function getCPS() {
    var time = mc.system.currentTick
    for (let click of clicks) {
        if (click + 20 <= time) {
            var index = clicks.indexOf(click);
            if (index > -1) {
                clicks.splice(index, 1);
            }
        }
    }
    return clicks.length
}

function defenseFinish (player) {
    player.runCommandAsync("setblock -1972 3 2005 air")
    player.runCommandAsync("tag @s remove defenseStarted")
    player.runCommandAsync("playsound random.levelup")
    player.runCommandAsync('titleraw @s title { "rawtext": [ { "text": "§eTime: §f" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §ems" } ] }')
    let highscores = mc.world.scoreboard.getObjective("highscore")
    var highscoreFinal = 0
    for (let highscore of highscores.getScores()) {
        if (highscore.participant.displayName == "defense") {
            highscoreFinal = highscore.score
            break
        }
    }
    var time = 0
    let scores = mc.world.scoreboard.getObjective("defense")
    for (let score of scores.getScores()) {
        if (score.participant.displayName == "Time") {
            time = score.score
            break
        }
    }
    if (highscoreFinal == 0) {
        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou beat this course for the §eFIRST TIME§a with a time of §e" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §ams!" } ] }`)
        player.runCommandAsync(`scoreboard players set defense highscore ${time}`)
        for (let score of mc.world.scoreboard.getObjective("defense").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((30000/score.score)*1.5)}`)
    } else if (highscoreFinal == time) {
        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §ams which was the §eSAME SPEED§a as your highscore!" } ] }`)
        for (let score of mc.world.scoreboard.getObjective("defense").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(30000/score.score)}`)
    } else if (highscoreFinal < time) {
        let difference = time - highscoreFinal
        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §ams which was§c ${difference}§a ms §cSLOWER§a than your highscore of §e" }, { "score": {"name": "defense", "objective": "highscore" } }, { "text": " §ams." } ] }`)
        for (let score of mc.world.scoreboard.getObjective("defense").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(30000/score.score)}`)
    } else {
        let difference = highscoreFinal - time
        player.runCommandAsync(`tellraw @s { "rawtext": [ { "text": "§l§dCONGRATULATIONS!§r\n§aYou had a time of §e" }, { "score": {"name": "Time", "objective": "defense" } }, { "text": " §ams which was§b ${difference}§a ms §bFASTER§a than your last highscore of §e" }, { "score": {"name": "defense", "objective": "highscore" } }, { "text": " §ams!" } ] }`)
        for (let score of mc.world.scoreboard.getObjective("defense").getScores()) if (score.participant.displayName == "Time") player.runCommandAsync(`scoreboard players add @s money ${Math.floor((30000/score.score)*1.5)}`)
        player.runCommandAsync(`scoreboard players set defense highscore ${time}`)
    }
    mc.system.runTimeout(() => {
        player.runCommandAsync("function defense/reset")
    }, 60)
}

// Items

mc.world.afterEvents.itemUse.subscribe((data) => {
    if(data.itemStack.typeId == "pada:hub") goHub(data.source)
    if(data.itemStack.typeId == "pada:teleporter") showTpMenu(data.source)
    if(data.itemStack.typeId == "pada:settings") {
        if (data.source.hasTag("extension")) {
            clutchSettings(data.source)
        } else if (data.source.hasTag("pearl")) {
            pearlSettings(data.source)
        } else if (data.source.hasTag("pvp")) {
            pvpSettings(data.source)
        } else if (data.source.hasTag("bridge")) {
            bridgeSettings(data.source)
        } else if (data.source.hasTag("parkourCourse")) {
            parkourSettings(data.source)
        } else if (data.source.hasTag("breaking")) {
            breakingSettings(data.source)
        } else if (data.source.hasTag("bow")) {
            bowSettings(data.source)
        } else if (data.source.hasTag("hotbar")) {
            hotbarSettings(data.source)
        } else if (data.source.hasTag("defense")) {
            defenseSettings(data.source)
        } else if (data.source.hasTag("ore")) {
            oreSettings(data.source)
        } else {
            showSettingsMenu(data.source)
        }
    }
    if(data.itemStack.typeId == "pada:shop") showShopMenu(data.source)
    if(data.itemStack.typeId == "pada:start" && data.source.hasTag("extension")) extension(data.source)
    if(data.itemStack.typeId == "pada:start" && data.source.hasTag("customCourse")) parkour(data.source)
})

function chestRefill () {
    var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
    block.setType(mc.MinecraftBlockTypes.air)
    mc.world.getDimension("overworld").runCommandAsync("kill @e[type=item]")
    block.setType(mc.MinecraftBlockTypes.chest)
    let itemAmt = Math.floor(Math.random() * 2.9 + 3);
    let junkAmt = Math.floor(Math.random() * 2.9 + 1)
    var usedSlots = []
    for (let i = 0; i < itemAmt; i++) {
        let item = Math.floor(Math.random() * 5.9);
        switch (item) {
            case 0:
                var slot = chestSelectSlot(usedSlots)
                usedSlots.push(slot)
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:diamond_sword"), 1));
                break
            case 1:
                var slot = chestSelectSlot(usedSlots)
                usedSlots.push(slot)
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:diamond_helmet"), 1));
                break
            case 2:
                var slot = chestSelectSlot(usedSlots)
                usedSlots.push(slot)
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:diamond_chestplate"), 1));
                break
            case 3:
                var slot = chestSelectSlot(usedSlots)
                usedSlots.push(slot)
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:diamond_leggings"), 1));
                break
            case 4:
                var slot = chestSelectSlot(usedSlots)
                usedSlots.push(slot)
                var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
                block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:diamond_boots"), 1));
                break
        }
    }
    for (let i = 0; i < junkAmt; i++) {
        var slot = chestSelectSlot(usedSlots)
        usedSlots.push(slot)
        var block = mc.world.getDimension("overworld").getBlock({ x: 2000, y: 1, z: 2002 });
        block.getComponent("inventory").container.setItem(slot, new mc.ItemStack(mc.ItemTypes.get("minecraft:web"), 1));
    }
}

function chestSelectSlot (usedSlots) {
    let slot = Math.floor(Math.random() * 26)
    if (usedSlots.includes(slot)) {
        return chestSelectSlot(usedSlots)
    } else {
        return slot
    }
}

function goHub(player) {
    if (player.hasTag("pearl")) {
        player.runCommandAsync('function pearlclutch/hub');
    } else if (player.hasTag("pvp")) {
        player.runCommandAsync('function pvp/hub');
    } else if (player.hasTag("extension")) {
        player.runCommandAsync('function extension/hub');
    } else if (player.hasTag("bridge")) {
        player.runCommandAsync("function bridge/hub")
    } else if (player.hasTag("parkour")) {
        player.runCommandAsync("function bParkour/random/hub")
    } else if (player.hasTag("parkourCourse")) {
        player.runCommandAsync("function bParkour/course/hub")
    } else if (player.hasTag("breaking")) {
        player.runCommandAsync("function breaking/hub")
    } else if (player.hasTag("bow")) {
        player.runCommandAsync("function bow/hub")
    } else if (player.hasTag("hotbar")) {
        player.runCommandAsync("function hotbar/hub")
    } else if (player.hasTag("defense")) {
        player.runCommandAsync("function defense/hub")
    } else if (player.hasTag("chest")) {
        player.runCommandAsync("function chest/hub")
    } else if (player.hasTag("ore")) {
        player.runCommandAsync("function ore/hub")
    } else {
        player.runCommandAsync('function spawn')
    }
}

function showTpMenu(player){
    let tpForm = new mcui.ActionFormData()
    .title("§l§bTeleporter§r")
    .body("Choose the gamemode you want to play!")

    tpForm
    .button("§5Pearl Clutching§r", "textures/items/ender_pearl")
    .button("§3PvP Practice§r", "textures/items/diamond_sword")
    .button("§eBlock Extension§r", "textures/blocks/sandstone_normal")
    .button("§6Bridging§r", "textures/blocks/wool_colored_white")
    .button("§cBlock Parkour§r", "textures/blocks/red_sandstone_normal")
    .button("§dTreasure Breaking§r", "textures/items/shears")
    .button("§bBow Aim§r", "textures/items/bow_pulling_2")
    .button("§3Hotbar Switching§r", "textures/ui/selected_hotbar_slot")
    .button("§2Treasure Protecting§r", "textures/blocks/end_stone")
    .button("§4Ore Mining§r", "textures/blocks/redstone_ore")
    .button("§0Chest Looting§r", "textures/blocks/chest_front")
    .button("§8Spawn§r", "textures/items/dragons_breath")
    .show(player).then(res => {
        let response = res.selection;
        switch (response) {
            case 0:
                showPearlMenu(player);
                break;

            case 1:
                showPvpMenu(player);
                break;
    
            case 2:
                showExtensionMenu(player);
                break;

            case 3:
                showBridgeMenu(player);
                break;

            case 4:
                showBParkourMenu(player);
                break;

            case 5:
                showBreakingMenu(player);
                break;

            case 6:
                showBowMenu(player);
                break;

            case 7:
                showHotbarMenu(player);
                break;

            case 8:
                showDefenseMenu(player);
                break;

            case 9:
                showOreMenu(player);
                break;

            case 10:
                showChestMenu(player);
                break;

            case 11:
                player.runCommandAsync('function pearlclutch/hub');
                break;
            default: break
        }
    })
}

function showPearlMenu(player) {
    let pearlForm = new mcui.ActionFormData()
    .title("§l§5Pearl Clutching§r")
    .body("Select the map you want to play")

    pearlForm
    .button("§fBaroque Style§r")
    .button("§6Wild Wood Style§r")
    .button("§cVillage Style§r")
    .button("§aFlat§r")
    .show(player).then(res => {
        let response = res.selection;
        switch (response) {
            case 0:
                player.runCommandAsync("say Baroque");
                player.runCommandAsync("function pearlclutch/baroque/tp")
                mc.system.runTimeout(() => {
                    player.runCommandAsync("summon pada:clutchbot pearl 1042 4 -4")
                    player.runCommandAsync("effect @e[type=pada:clutchbot,name=pearl] invisibility 999999 255 true")
                }, 10)
                break
            case 1:
                player.runCommandAsync("say Granite");
                player.runCommandAsync("function pearlclutch/wildwood/tp")
                mc.system.runTimeout(() => {
                    player.runCommandAsync("summon pada:clutchbot pearl 1016 9 976")
                    player.runCommandAsync("effect @e[type=pada:clutchbot,name=pearl] invisibility 999999 255 true")
                }, 10)
                break
            case 2:
                player.runCommandAsync("say Village");
                player.runCommandAsync("function pearlclutch/village/tp")
                mc.system.runTimeout(() => {
                    player.runCommandAsync("summon pada:clutchbot pearl 1027 -2 -1030")
                    player.runCommandAsync("effect @e[type=pada:clutchbot,name=pearl] invisibility 999999 255 true")
                    player.runCommandAsync("spawnpoint @s 1009 -2 -1001");
                }, 10)
                break
            case 3:
                player.runCommandAsync("say Flat");
                player.runCommandAsync("function pearlclutch/flat/tp")
                mc.system.runTimeout(() => {
                    player.runCommandAsync("summon pada:clutchbot pearl 2014 0 -20")
                    player.runCommandAsync("effect @e[type=pada:clutchbot,name=pearl] invisibility 999999 255 true")
                }, 10)
                break
        }
    })
}

function showPvpMenu (player) {
    let pvpForm = new mcui.ActionFormData()
    .title("§l§fPvP Training§r")
    .body("Select your Bot's size")

    pvpForm
    .button("§aBig Target§r")
    .button("§6Player Target§r")
    .button("§cSmall Target§r")
    .show(player).then(res => {
        let response = res.selection;
        switch (response) {
            case 0:
                player.runCommandAsync("say big");
                let bigForm = new mcui.ActionFormData()
                .title("§l§fPvP Training§r")
                .body("Select the difficulty")
                .button("§2Super Easy§r\n§7(Not Agressive)")
                .button("§aEasy§r\n§7(1 Block Reach)")
                .button("§eMedium§r\n§7(2 Block Reach)")
                .button("§6Hard§r\n§7(2.5 Block Reach)")
                .button("§cHardcore§r\n§7(3 Block Reach)")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§9PvPBot§r §2Super Easy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:supereasy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:big")
                            }, 10)
                            break
                        case 1:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§9PvPBot§r §aEasy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:easy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:big")
                            }, 10)
                            break
                        case 2:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§9PvPBot§r §eMedium" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:medium")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:big")
                            }, 10)
                            break
                        case 3:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§9PvPBot§r §6Hard" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hard")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:big")
                            }, 10)
                            break
                        case 4:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§9PvPBot§r §cHardcore" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hardcore")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:big")
                            }, 10)
                            break
                    }
                })
                break
            case 1:
                let playerForm = new mcui.ActionFormData()
                .title("§l§fPvP Training§r")
                .body("Select the difficulty")
                .button("§2Super Easy§r\n§7(Not Agressive)")
                .button("§aEasy§r\n§7(1 Block Reach)")
                .button("§eMedium§r\n§7(2 Block Reach)")
                .button("§6Hard§r\n§7(2.5 Block Reach)")
                .button("§cHardcore§r\n§7(3 Block Reach)")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§dPvPBot§r §2Super Easy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:supereasy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
                                player.runCommandAsync("tag @s add superEasy")
                            }, 10)
                            break
                        case 1:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§dPvPBot§r §aEasy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:easy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
                            }, 10)
                            break
                        case 2:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§dPvPBot§r §eMedium" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:medium")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
                            }, 10)
                            break
                        case 3:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§dPvPBot§r §6Hard" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hard")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
                            }, 10)
                            break
                        case 4:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§dPvPBot§r §cHardcore" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hardcore")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:player")
                            }, 10)
                            break
                    }
                })
                break
            case 2:
                player.runCommandAsync("say small");
                let smallForm = new mcui.ActionFormData()
                .title("§l§fPvP Training§r")
                .body("Select the difficulty")
                .button("§2Super Easy§r\n§7(Not Agressive)")
                .button("§aEasy§r\n§7(1 Block Reach)")
                .button("§eMedium§r\n§7(2 Block Reach)")
                .button("§6Hard§r\n§7(2.5 Block Reach)")
                .button("§cHardcore§r\n§7(3 Block Reach)")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§3PvPBot§r §2Super Easy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:superEasy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:small")
                            }, 10)
                            break
                        case 1:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§3PvPBot§r §aEasy" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:easy")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:small")
                            }, 10)
                            break
                        case 2:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§3PvPBot§r §eMedium" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:medium")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:small")
                            }, 10)
                            break
                        case 3:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§3PvPBot§r §6Hard" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hard")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:small")
                            }, 10)
                            break
                        case 4:
                            player.runCommandAsync("function pvp/tp")
                            mc.system.runTimeout(() => {
                                player.runCommandAsync('summon pada:pvpbot "§3PvPBot§r §cHardcore" 0 0 1000')
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:hardcore")
                                player.runCommandAsync("event entity @e[type=pada:pvpbot] pada:small")
                            }, 10)
                            break
                        }
                    })
                break
        }
    })
}

function showExtensionMenu (player) {
    let extensionForm = new mcui.ActionFormData()
    .title("§l§eBlock Extension")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch(res.selection) {
            case 0:
                player.runCommandAsync("function extension/tp")
                mc.system.runTimeout(() => {
                    player.runCommandAsync("summon pada:clutchbot -1000 -5 0")
                }, 10)
        }
    })
}

function showBridgeMenu (player) {
    let bridgeForm = new mcui.ActionFormData()
    .title("§l§6Bridging")
    .button("Normal")
    .button("Short")
    .button("Diagonal")
    .button("One Stack")
    .button("Down Stack")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000");
                player.runCommandAsync("scoreboard objectives add bridge dummy bridge")
                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                player.runCommandAsync("scoreboard players set Time bridge 0")
                player.runCommandAsync("function bridge/normal/load");
                player.runCommandAsync("tag @s remove hub")
                break;
            case 1:
                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000");
                player.runCommandAsync("scoreboard objectives add bridge dummy bridge")
                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                player.runCommandAsync("scoreboard players set Time bridge 0")
                player.runCommandAsync("function bridge/short/load");
                player.runCommandAsync("tag @s remove hub")
                break;
            case 2:
                player.runCommandAsync("tp 0 0 2000 facing 1 0 1999");
                player.runCommandAsync("scoreboard objectives add bridge dummy bridge")
                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                player.runCommandAsync("scoreboard players set Time bridge 0")
                player.runCommandAsync("function bridge/diagonal/load");
                player.runCommandAsync("tag @s remove hub")
                break;
            case 3:
                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000");
                player.runCommandAsync("scoreboard objectives add bridge dummy bridge")
                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                player.runCommandAsync("scoreboard players set Time bridge 0")
                player.runCommandAsync("function bridge/onestack/load");
                player.runCommandAsync("tag @s remove hub")
                break;
            case 4:
                player.runCommandAsync("tp 0 0 2000 facing 1 0 2000");
                player.runCommandAsync("scoreboard objectives add bridge dummy bridge")
                player.runCommandAsync("scoreboard players set Blocks bridge 0")
                player.runCommandAsync("scoreboard players set Time bridge 0")
                player.runCommandAsync("function bridge/downstack/load");
                player.runCommandAsync("tag @s remove hub")
                break;
        }
    })
}

function showBParkourMenu (player) {
    let parkourForm = new mcui.ActionFormData()
    .title("§l§cBlock Parkour§r")
    .body("Select")
    .button("Randomized", "textures/ui/random_dice")
    .button("Prebuilt Course", "textures/items/map_filled")
    .show(player).then(res => {
        switch (res.selection) {
            case 0: 
                player.runCommandAsync("function bParkour/random/tp");
                break;
            case 1:
                let courseForm = new mcui.ActionFormData()
                .title("§l§cBlock Parkour§r")
                .body("Select Map")
                .button("Course 1")
                .button("Course 2")
                .button("Course 3")
                .button("Custom")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("function bParkour/course/tp1");
                            break;
                        case 1:
                            player.runCommandAsync("function bParkour/course/tp2");
                            break;
                        case 2:
                            player.runCommandAsync("function bParkour/course/tp3")
                            break;
                        case 3:
                            player.runCommandAsync("function bParkour/course/tpCustom")
                            break;
                    }
                })
        }
    })
}

function showBreakingMenu (player) {
    let bowForm = new mcui.ActionFormData()
    .title("§l§dTreasure Breaking§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function breaking/tp")
                break
        }
    })
}

function showBowMenu (player) {
    let bowForm = new mcui.ActionFormData()
    .title("§l§bBow Aim§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function bow/tp")
                break
        }
    })
}

function showHotbarMenu (player) {
    let hotbarForm = new mcui.ActionFormData()
    .title("§l§3Hotbar Switching§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function hotbar/tp")
                break
        }
    })
}
function showDefenseMenu (player) {
    let bowForm = new mcui.ActionFormData()
    .title("§l§2Treasure Protecting§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function defense/tp")
                break
        }
    })
}

function showOreMenu (player) {
    let oreForm = new mcui.ActionFormData()
    .title("§l§4Ore Mining§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function ore/tp")
                break
        }
    })
}

function showChestMenu (player) {
    let chestForm = new mcui.ActionFormData()
    .title("§l§0Chest Looting§r")
    .button("Join", "textures/ui/check")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function chest/tp")
                break
        }
    })
}

function showSettingsMenu(player) {
    let settingsForm = new mcui.ModalFormData()
    .title("§l§8Settings§r")
    if (player.hasTag("cps")) {
        settingsForm.toggle("§mCPS Counter§r §o§7(Work in Progress)§r", true)
    } else {
        settingsForm.toggle("§mCPS Counter§r §o§7(Work in Progress)§r", false)
    }

    settingsForm.show(player).then(res => {
        if(res.canceled) {
            player.sendMessage("§cChanges canceled§r")
            return
        }
        let [ cpscounter ] = res.formValues;
        if (cpscounter) {
            player.runCommandAsync("tag @s add cps")
        } else {
            player.runCommandAsync("tag @s remove cps")
        }
        player.sendMessage("§aSettings saved!§r")
    })
}

var projectiles = {
    "none": ["None", "", 0],
    "smoke": ["Smoke Particles", "minecraft:basic_smoke_particle", 100],
    "flame": ["Flame Particles", "minecraft:basic_flame_particle", 100],
    "white": ["White Particles", "minecraft:magnesium_salts_emitter", 100],
    "red": ["Red Dust Particles", "minecraft:falling_border_dust_particle", 100],
    "water": ["Water Particles", "minecraft:water_splash_particle", 100],
    "heart": ["Heart Particles", "minecraft:heart_particle", 300],
    "emerald": ["Emerald Particles", "minecraft:villager_happy", 300],
    "sparkle": ["Sparkle Particles", "minecraft:sparkler_emitter", 500],
    "music": ["Music Particles", "minecraft:note_particle", 500],
    "enchanter": ["Enchanter Particles", "minecraft:lab_table_misc_mystical_particle", 800],
    "ender": ["Ender Particles", "minecraft:basic_portal_particle", 800],
    "shriek": ["Shriek Particles", "minecraft:shriek_particle", 1000],
    "soul": ["Soul Particles", "minecraft:soul_particle", 1000]
}

var blockSkins = {
    "wool": ["Wool", "textures/blocks/wool_colored_white", 0],
    "planks": ["Planks", "textures/blocks/planks_oak", 100],
    "sandstone": ["Sandstone", "textures/blocks/sandstone_normal", 100],
    "pada:crumbling": ["Crumbling Cobblestone", "textures/blocks/coral_pink_dead", 100],
    "andesite": ["Andesite", "textures/blocks/stone_andesite", 100],
    "cobblestone": ["Cobblestone", "textures/blocks/cobblestone", 100],
    "cobblestone_wall": ["Cobblestone Wall", "textures/blocks/cobblestone", 300],
    "oak_fence": ["Fences", "textures/blocks/planks_oak", 300],
    "glass_pane": ["Glass Pane", "textures/blocks/glass", 300]
}

// for (let particle in projectiles) {
//     mc.world.getDimension("overworld").runCommandAsync(`scoreboard players set ${particle} unlockable_projectiles 0`)
// }

// for (let skin in blockSkins) {
//     mc.world.getDimension("overworld").runCommandAsync(`scoreboard players set ${skin} unlockable_blocks 0`)
// }

function showShopMenu (player) {
    let shopForm = new mcui.ActionFormData()
    .title("§l§aShop§r")
    var moneysb = mc.world.scoreboard.getObjective("money")
    for (let score of moneysb.getScores()) {
        if (score.participant.displayName == player.name) {
            shopForm.body(`§eBalance: ${score.score}`)
        }
    }

    shopForm
    .button("§aProjectile Trail§r", "textures/items/snowball")
    .button("§cBlocks§r", "textures/blocks/wool_colored_white")
    .show(player).then(res => {
        let response = res.selection;
        switch (response) {
            case 0:
                let projectileForm = new mcui.ActionFormData()
                .title("§l§aShop§r")
                for (let score of moneysb.getScores()) {
                    if (score.participant.displayName == player.name) {
                        projectileForm.body(`§eBalance: ${score.score}`)
                    }
                }
                for (let particle in projectiles) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_projectiles")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == particle) {
                            if (score.participant.displayName == "none") {
                                if (player.hasTag("projectiles")) {
                                    projectiles["none"].push(true)
                                    projectileForm.button(`${projectiles["none"][0]}\n§eUnlocked`)
                                } else {
                                    projectiles["none"].push(true)
                                    projectileForm.button(`${projectiles["none"][0]}\n§aSelected`)
                                }
                            } else if (score.score == 1) {
                                if (player.hasTag(particle)) {
                                    projectiles[particle].push(true)
                                    projectileForm.button(`${projectiles[particle][0]}\n§aSelected`)
                                } else {
                                    projectiles[particle].push(true)
                                    projectileForm.button(`${projectiles[particle][0]}\n§eUnlocked`)
                                }
                            } else {
                                projectiles[particle].push(false)
                                projectileForm.button(`${projectiles[particle][0]}\n§8${projectiles[particle][2]}`)
                            }
                            break
                        }
                    }
                }
                projectileForm.show(player).then(res => {
                    if (projectiles[Object.keys(projectiles)[res.selection]][3] == false) {
                        var moneysb = mc.world.scoreboard.getObjective("money")
                        for (let score of moneysb.getScores()) {
                            if (score.participant.displayName == player.name) {
                                if (score.score >= projectiles[Object.keys(projectiles)[res.selection]][2]) {
                                    let confirmForm = new mcui.MessageFormData()
                                    .title("§l§aShop§r")
                                    .body(`Are you sure you want to buy §e${projectiles[Object.keys(projectiles)[res.selection]][0]}?`)
                                    .button2("Yes!")
                                    .button1("Nevermind.")
                                    .show(player).then(ress => {
                                        if (ress.selection == 1) {
                                            player.runCommandAsync(`scoreboard players remove @s money ${projectiles[Object.keys(projectiles)[res.selection]][2]}`)
                                            player.runCommandAsync(`scoreboard players set ${Object.keys(projectiles)[res.selection]} unlockable_projectiles 1`)
                                            projectiles[Object.keys(projectiles)[res.selection]][3] = true
                                            for (let particle in projectiles) {
                                                player.runCommandAsync(`tag @s remove ${particle}`)
                                            }
                                            player.runCommandAsync(`tag @s add ${Object.keys(projectiles)[res.selection]}`)
                                            player.runCommandAsync("tag @s add projectiles")
                                            player.sendMessage(`§aYou just unlocked ${projectiles[Object.keys(projectiles)[res.selection]][0]}!`)
                                            player.runCommandAsync("playsound random.levelup @s")
                                        } else {
                                            return
                                        }
                                    })
                                } else {
                                    player.sendMessage("§cYou do not own enough money to buy this.§r")
                                }
                            }
                        }
                    } else if (projectiles[Object.keys(projectiles)[res.selection]] == projectiles[Object.keys(projectiles)[0]]) {
                        for (let particle in projectiles) {
                            player.runCommandAsync(`tag @s remove ${particle}`)
                        }
                        player.runCommandAsync("tag @s remove projectiles")
                    } else {
                        for (let particle in projectiles) {
                            player.runCommandAsync(`tag @s remove ${particle}`)
                        }
                        player.runCommandAsync(`tag @s add ${Object.keys(projectiles)[res.selection]}`) 
                        player.runCommandAsync("tag @s add projectiles")
                    }
                })
                break
            case 1:
                let blocksForm = new mcui.ActionFormData()
                .title("§l§aShop§r")
                for (let score of moneysb.getScores()) {
                    if (score.participant.displayName == player.name) {
                        blocksForm.body(`§eBalance: ${score.score}`)
                    }
                }
                for (let blockskin in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == blockskin) {
                            if (score.participant.displayName == "wool") {
                                blockSkins["wool"].push(true)
                                blocksForm.button(`${blockSkins["wool"][0]}\n§aUnlocked`, blockSkins[blockskin][1])
                            } else if (score.score == 1) {
                                blockSkins[blockskin].push(true)
                                blocksForm.button(`${blockSkins[blockskin][0]}\n§aUnlocked`, blockSkins[blockskin][1])
                            } else {
                                blockSkins[blockskin].push(false)
                                blocksForm.button(`${blockSkins[blockskin][0]}\n§8${blockSkins[blockskin][2]}`, blockSkins[blockskin][1])
                            }
                            break
                        }
                    }
                }
                blocksForm.show(player).then(res => {
                    if (blockSkins[Object.keys(blockSkins)[res.selection]][3] == false) {
                        var moneysb = mc.world.scoreboard.getObjective("money")
                        for (let score of moneysb.getScores()) {
                            if (score.participant.displayName == player.name) {
                                if (score.score >= blockSkins[Object.keys(blockSkins)[res.selection]][2]) {
                                    let confirmForm = new mcui.MessageFormData()
                                    .title("§l§aShop§r")
                                    .body(`Are you sure you want to buy §e${blockSkins[Object.keys(blockSkins)[res.selection]][0]}?`)
                                    .button2("Yes!")
                                    .button1("Nevermind.")
                                    .show(player).then(ress => {
                                        if (ress.selection == 1) {
                                            player.runCommandAsync(`scoreboard players remove @s money ${blockSkins[Object.keys(blockSkins)[res.selection]][2]}`)
                                            player.runCommandAsync(`scoreboard players set ${Object.keys(blockSkins)[res.selection]} unlockable_blocks 1`)
                                            blockSkins[Object.keys(blockSkins)[res.selection]][3] = true
                                            player.sendMessage(`§aYou just unlocked ${blockSkins[Object.keys(blockSkins)[res.selection]][0]}!`)
                                            player.runCommandAsync("playsound random.levelup @s")
                                        } else {
                                            return
                                        }
                                    })
                                } else {
                                    player.sendMessage("§cYou do not own enough money to buy this.§r")
                                }
                            }
                        }
                    } else {
                        player.sendMessage("§aYou already own this block!")
                    }
                })
                break
        }
    })
}


// Crumbling Cobblestone

mc.world.afterEvents.blockPlace.subscribe(data => {
    if (data.block.typeId == "pada:crumbling") {
        // blocks.push({x: data.block.x, y: data.block.y, z: data.block.z, timer: 0})
        mc.system.runTimeout(() => {
            data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air destroy`)
        }, 35)
    }
})

// ClutchBot

var hits = [1, 1, 1]

function extension(player) {
    player.runCommandAsync("tag @s add extensionStarted")
    player.runCommandAsync('title @s[tag=extensionStarted] title §a3§r')
    player.runCommandAsync('playsound random.click @s[tag=extensionStarted]')
    mc.system.runTimeout(() => {
        player.runCommandAsync('title @s[tag=extensionStarted] title §e2§r')
        player.runCommandAsync('playsound random.click @s[tag=extensionStarted]')
        mc.system.runTimeout(() => {
            player.runCommandAsync('title @s[tag=extensionStarted] title §c1§r')
            player.runCommandAsync('playsound random.click @s[tag=extensionStarted]')
            mc.system.runTimeout(() => {
                player.runCommandAsync('title @s[tag=extensionStarted] title §bClutch!§r')
                player.runCommandAsync('playsound random.orb @s[tag=extensionStarted]')
                player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[0]}`)
                if (hits.length >= 2) {
                    mc.system.runTimeout(() => {
                        player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[1]}`)
                        if (hits.length >= 3) {
                            mc.system.runTimeout(() => {
                                player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[2]}`)
                                if (hits.length >= 4) {
                                    mc.system.runTimeout(() => {
                                        player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[3]}`)
                                        if (hits.length >= 5) {
                                            mc.system.runTimeout(() => {
                                                player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[4]}`)
                                                if (hits.length >= 6) {
                                                    mc.system.runTimeout(() => {
                                                        player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[5]}`)
                                                        if (hits.length >= 7) {
                                                            mc.system.runTimeout(() => {
                                                                player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[6]}`)
                                                                if (hits.length >= 8) {
                                                                    mc.system.runTimeout(() => {
                                                                        player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[7]}`)
                                                                        if (hits.length >= 9) {
                                                                            mc.system.runTimeout(() => {
                                                                                player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[8]}`)
                                                                                if (hits.length >= 10) {
                                                                                    mc.system.runTimeout(() => {
                                                                                        player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[9]}`)
                                                                                        mc.system.runTimeout(() => {
                                                                                                for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                                                                player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                                                                player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                                                                player.runCommandAsync('function extension/clear')
                                                                                        }, 5)}, 60)
                                                                                    }, 10)
                                                                                } else mc.system.runTimeout(() => {
                                                                                    for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                                                    player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                                                    player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                                                    player.runCommandAsync('function extension/clear')
                                                                                }, 5)}, 60)
                                                                            }, 10)
                                                                        }else mc.system.runTimeout(() => {
                                                                            for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                                            player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                                            player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                                            player.runCommandAsync('function extension/clear')
                                                                        }, 5)}, 60)
                                                                    }, 10)
                                                                }else mc.system.runTimeout(() => {
                                                                    for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                                    player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                                    player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                                    player.runCommandAsync('function extension/clear')
                                                                }, 5)}, 60)
                                                            }, 10)
                                                        }else mc.system.runTimeout(() => {
                                                            for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                            player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                            player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                            player.runCommandAsync('function extension/clear')
                                                        }, 5)}, 60)
                                                    }, 10)
                                                }else mc.system.runTimeout(() => {
                                                    for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                                    player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                                    player.runCommandAsync(`clear @s ${extensionBlock}`)
                                                    player.runCommandAsync('function extension/clear')
                                                }, 5)}, 60)
                                            }, 10)
                                        }else mc.system.runTimeout(() => {
                                            for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                            player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                            player.runCommandAsync(`clear @s ${extensionBlock}`)
                                            player.runCommandAsync('function extension/clear')
                                        }, 5)}, 60)
                                    }, 10)
                                }else mc.system.runTimeout(() => {
                                    for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                                    player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                                    player.runCommandAsync(`clear @s ${extensionBlock}`)
                                    player.runCommandAsync('function extension/clear')
                                }, 5)}, 60)
                            }, 10)
                        }else mc.system.runTimeout(() => {
                            for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                            player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                            player.runCommandAsync(`clear @s ${extensionBlock}`)
                            player.runCommandAsync('function extension/clear')
                        }, 5)}, 60)
                    }, 10)
                } else mc.system.runTimeout(() => {
                    for (let score of mc.world.scoreboard.getObjective("extension").getScores()) if (score.participant.displayName == "Blocks") player.runCommandAsync(`scoreboard players add @s money ${Math.floor(score.score/2)}`)
                    player.runCommandAsync("give @s[tag=extension] pada:start"); player.runCommandAsync("tag @s remove extensionStarted"); player.runCommandAsync("scoreboard players set Blocks extension 0"); mc.system.runTimeout(()=>{
                    player.runCommandAsync(`clear @s ${extensionBlock}`)
                    player.runCommandAsync('function extension/clear')
                }, 5)}, 60)
                // mc.system.run(hit(0, player, hits), 10)
            }, 20)
        }, 20)
    }, 20)
}


// function hit (currentHit, player, hits) {
//     player.runCommandAsync(`event entity @e[type=pada:clutchbot,c=1] pada:hit${hits[currentHit]}`)
//     if (hits.length >= currentHit+2) {
//         let nextHit = currentHit + 1
//         console.error(nextHit)
//         mc.system.runTimeout(hit(nextHit, player, hits), 10)
//     }
// }

var extensionBlock = "wool"

function clutchSettings(player) {
    let clutchsets = new mcui.ActionFormData()
    .title("§l§eExtension Settings§r")
    .button("Hits", "textures/items/iron_sword")
    .button("Blocks", "textures/blocks/wool_colored_white")
    .button("Slow Falling", "textures/ui/slow_falling_effect")
    .show(player).then(res => {
        if (res.canceled) return
        switch (res.selection) {
            case 0:
                let clutchForm = new mcui.ModalFormData()
                .title("§l§eExtension Settings§r")
                .slider("Number of Hits", 1, 10, 1)
                
                clutchForm.show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cSettings canceled.§r")
                    let clutchForm2 = new mcui.ModalFormData()
                    .title("§l§eExtension Settings§r")
                    
                    for (let i = 1; i <= res.formValues[0]; i++) {
                        clutchForm2.slider(`Hit ${i} Knockback Strength`, 1, 3, 1)
                    }
                    clutchForm2.show(player).then(res => {
                        if (res.canceled) return player.sendMessage("§cSettings canceled.§r")
                        hits = []
                        for (let value of res.formValues) {
                            hits.push(value)
                        }
                        player.sendMessage("§aSettings saved!§r")
                    })
                })
                break
            case 1:
                let editBlock = new mcui.ActionFormData()
                .title("§l§eExtension Settings§r")
                var unlockedBlocks = {}
                for (let block in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == block) {
                            if (score.participant.displayName == "wool") {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            } else if (score.score == 1) {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            }
                            break
                        }
                    }
                }
                editBlock.show(player).then(res => {
                    if (res.canceled) return
                    extensionBlock = Object.keys(unlockedBlocks)[res.selection]
                })
                break
            case 2:
                let slowFalling = new mcui.ModalFormData()
                .title("§l§eExtension Settings§r")
                if (player.hasTag("slowFalling")) {
                    slowFalling.toggle("Enabled", true)
                } else {
                    slowFalling.toggle("Enabled", false)
                }
                slowFalling.show(player).then(res => {
                    if (res.canceled) return
                    if (res.formValues[0]) {
                        player.runCommandAsync("tag @s add slowFalling")
                    } else {
                        player.runCommandAsync("tag @s remove slowFalling")
                    }
                })
                break
        }
    })

}

var pearlBlock = "wool"

function pearlSettings(player) {
    let pearlForm = new mcui.ActionFormData()
    .title("§l§5Pearl Settings§r")
    .button("Edit Inventory", "textures/ui/pencil_edit_icon")
    .button("Change Blocks", "textures/blocks/wool_colored_white")
    .button("Slow Falling", "textures/ui/slow_falling_effect")
    .button("§cReset Map§r", "textures/ui/redX1")
    .show(player).then(res => {
        if (res.canceled) return 
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function pearlclutch/editinv")
                let editInvForm = new mcui.ModalFormData()
                .title("§l§5Pearl Settings§r")
                .slider("Pearl Slot", 1, 9, 1)
                .slider("Blocks Slot", 1, 9, 1)
                .slider("Settings Slot", 1, 9, 1)
                .slider("Hub slot", 1, 9, 1)
                .show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
                    if (hasDuplicates(res.formValues)) {
                        player.sendMessage("§cYou cannot place to items on the same slot!§r")
                    } else {
                        player.runCommandAsync(`scoreboard players set pearl inventoryPearl ${res.formValues[0] - 1}`)
                        player.runCommandAsync(`scoreboard players set blocks inventoryPearl ${res.formValues[1] - 1}`)
                        player.runCommandAsync(`scoreboard players set settings inventoryPearl ${res.formValues[2] - 1}`)
                        player.runCommandAsync(`scoreboard players set hub inventoryPearl ${res.formValues[3] - 1}`)
                        mc.system.runTimeout(() => {
                            player.runCommandAsync('clear')
                        }, 5)
                    }
                })
                break
            case 1:
                let editBlock = new mcui.ActionFormData()
                .title("§l§5Pearl Settings§r")
                var unlockedBlocks = {}
                for (let block in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == block) {
                            if (score.participant.displayName == "andesite" || score.participant.displayName == "cobblestone") {
                                continue
                            } else if (score.participant.displayName == "wool") {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            } else if (score.score == 1) {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            }
                            break
                        }
                    }
                }
                editBlock.show(player).then(res => {
                    if (res.canceled) return
                    pearlBlock = Object.keys(unlockedBlocks)[res.selection]
                })
                break
            case 2:
                let slowFalling = new mcui.ModalFormData()
                .title("§l§5Pearl Settings§r")
                if (player.hasTag("slowFalling")) {
                    slowFalling.toggle("Enabled", true)
                } else {
                    slowFalling.toggle("Enabled", false)
                }
                slowFalling.show(player).then(res => {
                    if (res.canceled) return
                    if (res.formValues[0]) {
                        player.runCommandAsync("tag @s add slowFalling")
                    } else {
                        player.runCommandAsync("tag @s remove slowFalling")
                    }
                })
            case 3:
                if (player.hasTag("baroque")) {
                    player.runCommandAsync("function pearlclutch/baroque/clear")
                }
                if (player.hasTag("wildwood")) {
                    player.runCommandAsync("function pearlclutch/wildwood/clear")
                }
                if (player.hasTag("village")) {
                    player.runCommandAsync("function pearlclutch/village/clear")
                }
                if (player.hasTag("flat")) {
                    player.runCommandAsync("function pearlclutch/flat/clear")
                }
        }
    })
}

function hasDuplicates(array) {
    return new Set(array).size !== array.length;
}

function pvpSettings (player) {
    let pvpForm = new mcui.ActionFormData()
    .title("§l§fPvP Settings§r")
    .button("Edit Inventory", "textures/ui/pencil_edit_icon")
    .button("Effects", "textures/ui/speed_effect")
    .button("Map", "textures/items/map_filled")
    .show(player).then(res => {
         if (res.canceled) return player.sendMessage("§cChanges canceled.")
         switch(res.selection) {
             case 0:
                player.runCommandAsync("function pvp/editinv")
                let editInvForm = new mcui.ModalFormData()
                .title("§l§fPvP Settings§r")
                .slider("Sword Slot", 1, 9, 1)
                .slider("Fishing Rod Slot", 1, 9, 1)
                .slider("Snowball Slot", 1, 9, 1)
                .slider("Bow Slot", 1, 9, 1)
                .slider("Pearl Slot", 1, 9, 1)
                .slider("Blocks Slot", 1, 9, 1)
                .slider("Settings Slot", 1, 9, 1)
                .slider("Hub slot", 1, 9, 1)
                .show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
                    if (hasDuplicates(res.formValues)) {
                        player.sendMessage("§cYou cannot place to items on the same slot!§r")
                    } else {
                        player.runCommandAsync(`scoreboard players set sword inventoryPvp ${res.formValues[0] - 1}`)
                        player.runCommandAsync(`scoreboard players set rod inventoryPvp ${res.formValues[1] - 1}`)
                        player.runCommandAsync(`scoreboard players set snowball inventoryPvp ${res.formValues[2] - 1}`)
                        player.runCommandAsync(`scoreboard players set bow inventoryPvp ${res.formValues[3] - 1}`)
                        player.runCommandAsync(`scoreboard players set pearl inventoryPvp ${res.formValues[4] - 1}`)
                        player.runCommandAsync(`scoreboard players set blocks inventoryPvp ${res.formValues[5] - 1}`)
                        player.runCommandAsync(`scoreboard players set settings inventoryPvp ${res.formValues[6] - 1}`)
                        player.runCommandAsync(`scoreboard players set hub inventoryPvp ${res.formValues[7] - 1}`)

                        mc.system.runTimeout(() => {
                            player.runCommandAsync('clear')
                        }, 5)
                    }
                })
                break
             case 1:
                let effectsForm = new mcui.ModalFormData()
                .title("§l§fPvP Settings")
                if (player.hasTag("slowFalling")) {
                    effectsForm.toggle("Slow Falling §7(self)§r", true)
                } else {
                    effectsForm.toggle("Slow Falling §7(self)§r", false)
                }
                if (player.hasTag("pvpSlowFalling")) {
                    effectsForm.toggle("Slow Falling §7(bot)§r", true)
                } else {
                    effectsForm.toggle("Slow Falling §7(bot)§r", false)
                }
                if (player.hasTag("speed")) {
                    effectsForm.toggle("Speed §7(self)§r", true)
                } else {
                    effectsForm.toggle("Speed §7(self)§r", false)
                }
                effectsForm.show(player).then(res => {
                    if (res.canceled) return
                    if (res.formValues[0]) {
                        player.runCommandAsync("tag @s add slowFalling")
                    } else {
                        player.runCommandAsync("tag @s remove slowFalling")
                    }
                    if (res.formValues[1]) {
                        player.runCommandAsync("tag @s add pvpSlowFalling")
                    } else {
                        player.runCommandAsync("tag @s remove pvpSlowFalling")
                    }
                    if (res.formValues[2]) {
                        player.runCommandAsync("tag @s add speed")
                    } else {
                        player.runCommandAsync("tag @s remove speed")
                    }
                })
                break
             case 2:
                 let mapForm = new mcui.ActionFormData()
                 .title("§l§fPvP Settings")
                 .button("Default")
                 .button("Locked")
                 .show(player).then(res => {
                     if (res.canceled) return
                     switch (res.selection) {
                         case 0:
                             player.sendMessage("§aChanged to map 1.§r")
                             player.runCommandAsync("structure load pvp/map/1 coordinates")
                             break
                         case 1:
                             player.sendMessage("§cYou didn't unlock this map!§r")
                             player.runCommandAsync("playsound note.bass")
                             break
                      }
                 })
         }
    })
}

var bridgeBlock = "wool"

function bridgeSettings (player) {
    let bridgeForm = new mcui.ActionFormData()
    .title("§l§6Bridging Settings§r")
    .body("Customize your settings")
    .button("Block")
    .button("Mode")
    .button("§cReset Highscores")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                let editBlock = new mcui.ActionFormData()
                .title("§l§6Bridging Settings§r")
                var unlockedBlocks = {}
                for (let block in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == block) {
                            if (score.participant.displayName == "wool") {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            } else if (score.score == 1) {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            }
                            break
                        }
                    }
                }
                editBlock.show(player).then(res => {
                    if (res.canceled) return
                    bridgeBlock = Object.keys(unlockedBlocks)[res.selection]
                })
                break
            case 1:
                let changeMode = new mcui.ActionFormData()
                .title("§l§6Bridging Settings§r")
                .body("Select the mode you want to play.")
                if (player.hasTag("normal")) {
                    changeMode.button("Normal\n§e- Selected -§r")
                    changeMode.button("Short")
                    changeMode.button("Diagonal")
                    changeMode.button("One Stack")
                    changeMode.button("Down Stack")
                } else if (player.hasTag("short")) {
                    changeMode.button("Normal")
                    changeMode.button("Short\n§e- Selected -§r")
                    changeMode.button("Diagonal")
                    changeMode.button("One Stack")
                    changeMode.button("Down Stack")
                } else if (player.hasTag("diagonal")) {
                    changeMode.button("Normal")
                    changeMode.button("Short")
                    changeMode.button("Diagonal\n§e- Selected -§r")
                    changeMode.button("One Stack")
                    changeMode.button("Down Stack")
                } else if (player.hasTag("onestack")) {
                    changeMode.button("Normal")
                    changeMode.button("Short")
                    changeMode.button("Diagonal")
                    changeMode.button("One Stack\n§e- Selected -§r")
                    changeMode.button("Down Stack")
                } else if (player.hasTag("downstack")) {
                    changeMode.button("Normal")
                    changeMode.button("Short")
                    changeMode.button("Diagonal")
                    changeMode.button("One Stack")
                    changeMode.button("Down Stack\n§e- Selected -§r")
                }
                changeMode.show(player).then(res => {
                    switch (res.selection) {
                        case 0: 
                            player.runCommandAsync("function bridge/normal/load")
                            break
                        case 1: 
                            player.runCommandAsync("function bridge/short/load")
                            break
                        case 2: 
                            player.runCommandAsync("function bridge/diagonal/load")
                            break
                        case 3: 
                            player.runCommandAsync("function bridge/onestack/load")
                            break
                        case 4: 
                            player.runCommandAsync("function bridge/downstack/load")
                            break
                    }
                })
                break
            case 2:
                let resetForm = new mcui.MessageFormData()
                .title("§l§6Bridging Settings§r")
                .body("Are you sure you want to erase your Bridging highscores? §cThis action is irreversible!")
                .button2("Yes, do it!")
                .button1("No, keep my highscores")
                .show(player).then(res => {
                    if (res.selection == 1) {
                        player.runCommandAsync("scoreboard players set normal highscore 0")
                        player.runCommandAsync("scoreboard players set short highscore 0")
                        player.runCommandAsync("scoreboard players set diagonal highscore 0")
                        player.runCommandAsync("scoreboard players set onestack highscore 0")
                        player.runCommandAsync("scoreboard players set downstack highscore 0")
                        player.sendMessage("§cSuccessfully erased highscores.")
                    } else {
                        return
                    }
                })
                break
        }
    })
}

function parkour (player) {
    if (player.hasTag("course1")) {
        player.runCommandAsync("tag @s add courseStarted")
        player.runCommandAsync("setblock -998 201 1039 redstone_block")
    }
    if (player.hasTag("course2")) {
        player.runCommandAsync("tag @s add courseStarted")
        player.runCommandAsync("setblock -975 200 -1000 redstone_block")
    }
    if (player.hasTag("course3")) {
        player.runCommandAsync("tag @s add courseStarted")
        player.runCommandAsync("setblock -1968 200 0 redstone_block")
    }
    if (player.hasTag("customCourse")) {
        player.runCommandAsync("tag @s add courseStarted")
        player.runCommandAsync("setblock -1975 200 1000 redstone_block")
        player.runCommandAsync("tp @s -2000 0 1000")
        player.runCommandAsync("clear @s quartz_block")
        player.runCommandAsync("clear @s netherite_pickaxe")
    }
}


var parkourBlock = "wool"

function parkourSettings (player) {
    let parkourForm = new mcui.ActionFormData()
    .title("§l§cParkour Settings§r")
    .body("Customize your settings")
    .button("Block", "textures/blocks/wool_colored_white")
    .button("Reset Highscores", "textures/ui/cancel")
    .show(player).then(res => {
        if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
        switch (res.selection) {
            case 0:
                let editBlock = new mcui.ActionFormData()
                .title("§l§cParkour Settings§r")
                var unlockedBlocks = {}
                for (let block in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == block) {
                            if (score.participant.displayName == "wool") {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            } else if (score.score == 1) {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            }
                            break
                        }
                    }
                }
                editBlock.show(player).then(res => {
                    if (res.canceled) return
                    parkourBlock = Object.keys(unlockedBlocks)[res.selection]
                })
                break
            case 1:
                let resetForm = new mcui.MessageFormData()
                .title("§l§cParkour Settings§r")
                .body("Are you sure you want to erase your Parkour highscores? §cThis action is irreversible!")
                .button2("Yes, do it!")
                .button1("No, keep my highscores")
                .show(player).then(res => {
                    if (res.selection == 1) {
                        player.runCommandAsync("scoreboard players set course1 highscore 0")
                        player.runCommandAsync("scoreboard players set course2 highscore 0")
                        player.runCommandAsync("scoreboard players set course3 highscore 0")
                        player.runCommandAsync("scoreboard players set customCourse highscore 0")
                        player.sendMessage("§cSuccessfully erased highscores.")
                    } else {
                        return
                    }
                })
                break
        }
    })
}


var breakingBlock = "wool"

function breakingSettings (player) {
    let breakingForm = new mcui.ActionFormData()
    .title("§l§dBreaking Settings§r")
    .button("Edit Inventory", "textures/ui/pencil_edit_icon")
    .button("Block", "textures/blocks/wool_colored_white")
    .button("Treasure Defense", "textures/items/map_filled")
    .button("§cReset Highscores§r", "textures/ui/cancel")
    .show(player).then(res => {
         if (res.canceled) return player.sendMessage("§cChanges canceled.")
         switch(res.selection) {
             case 0:
                player.runCommandAsync("function pvp/editinv")
                let editInvForm = new mcui.ModalFormData()
                .title("§l§dBreaking Settings§r")
                .slider("Sword Slot", 1, 9, 1)
                .slider("Pickaxe Slot", 1, 9, 1)
                .slider("Axe Slot", 1, 9, 1)
                .slider("Shears Slot", 1, 9, 1)
                .slider("Blocks Slot", 1, 9, 1)
                .slider("Settings Slot", 1, 9, 1)
                .slider("Hub slot", 1, 9, 1)
                .show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
                    if (hasDuplicates(res.formValues)) {
                        player.sendMessage("§cYou cannot place to items on the same slot!§r")
                    } else {
                        player.runCommandAsync(`scoreboard players set sword inventoryBreaking ${res.formValues[0] - 1}`)
                        player.runCommandAsync(`scoreboard players set pickaxe inventoryBreaking ${res.formValues[1] - 1}`)
                        player.runCommandAsync(`scoreboard players set axe inventoryBreaking ${res.formValues[2] - 1}`)
                        player.runCommandAsync(`scoreboard players set shears inventoryBreaking ${res.formValues[3] - 1}`)
                        player.runCommandAsync(`scoreboard players set blocks inventoryBreaking ${res.formValues[4] - 1}`)
                        player.runCommandAsync(`scoreboard players set settings inventoryBreaking ${res.formValues[5] - 1}`)
                        player.runCommandAsync(`scoreboard players set hub inventoryBreaking ${res.formValues[6] - 1}`)

                        mc.system.runTimeout(() => {
                            player.runCommandAsync('clear')
                        }, 5)
                    }
                })
                break
             case 1:
                let editBlock = new mcui.ActionFormData()
                .title("§l§dBreaking Settings§r")
                var unlockedBlocks = {}
                for (let block in blockSkins) {
                    let scoreboard = mc.world.scoreboard.getObjective("unlockable_blocks")
                    for (let score of scoreboard.getScores()) {
                        if (score.participant.displayName == block) {
                            if (score.participant.displayName == "wool") {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            } else if (score.score == 1) {
                                if (block == extensionBlock) {
                                    editBlock.button(`${blockSkins[block][0]}\n§e- Selected -§r`, blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                } else {
                                    editBlock.button(blockSkins[block][0], blockSkins[block][1])
                                    unlockedBlocks[block] = blockSkins[block]
                                }
                            }
                            break
                        }
                    }
                }
                editBlock.show(player).then(res => {
                    if (res.canceled) return
                    breakingBlock = Object.keys(unlockedBlocks)[res.selection]
                })
                break
             case 2:
                player.runCommandAsync("function breaking/settings")
                break
            case 3:
                let resetForm = new mcui.MessageFormData()
                .title("§l§dBreaking Settings§r")
                .body("Are you sure you want to erase your Treasure Breaking highscores? §cThis action is irreversible!")
                .button2("Yes, do it!")
                .button1("No, keep my highscores")
                .show(player).then(res => {
                    if (res.selection == 1) {
                        player.runCommandAsync("scoreboard players set breaking highscore 0")
                        player.sendMessage("§cSuccessfully erased highscores.")
                    } else {
                        return
                    }
                })
                break
         }
    })
}

function bowSettings (player) {
    let bowForm = new mcui.ActionFormData()
    .title("§l§bBow Settings§r")
    .body("Customize your settings")
    .button("Mode")
    .button("Projectile Trail")
    if (player.hasTag("bow3")) {
        bowForm.button("Slow Falling")
    }
    bowForm.show(player).then(res => {
        switch (res.selection) {
            case 0:
                let bowModeForm = new mcui.ActionFormData()
                .title("§l§bBow Settings§r")
                .body("Customize your settings")
                .button("Static Shot")
                .button("Moving Shot")
                .button("Falling Shot")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("function bow/load1")
                            break
                        case 1:
                            player.runCommandAsync("function bow/load2")
                            break
                        case 2:
                            player.runCommandAsync("function bow/load3")
                            break
                    }
                })
                break
            case 1:
                let trailForm = new mcui.ActionFormData()
                .title("§l§bBow Settings§r")
                .body("Customize your settings")
                .button("None")
                .show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.sendMessage("Selected None")
                    }
                })
                break
            case 2:
                let effectsForm = new mcui.ModalFormData()
                .title("§l§bBow Settings§r")
                if (player.hasTag("pvpSlowFalling")) {
                    effectsForm.toggle("Slow Falling §7(bot)§r", true)
                } else {
                    effectsForm.toggle("Slow Falling §7(bot)§r", false)
                }
                effectsForm.show(player).then(res => {
                    if (res.formValues[0]) {
                        player.runCommandAsync("tag @s add pvpSlowFalling")
                    } else {
                        player.runCommandAsync("tag @s remove pvpSlowFalling")
                    }
                })
                break
        }
    })
}


function hotbarSettings (player) {
    let hotbarForm = new mcui.ActionFormData()
    .title("§l§3Hotbar Settings§r")
    .body("Customize your settings")
    .button("Edit Inventory", "textures/ui/pencil_edit_icon")
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                player.runCommandAsync("function pvp/editinv")
                let editInvForm = new mcui.ModalFormData()
                .title("§l§3Hotbar Settings§r")
                .slider("Sword Slot", 1, 9, 1)
                .slider("Pickaxe Slot", 1, 9, 1)
                .slider("Axe Slot", 1, 9, 1)
                .slider("Blocks Slot", 1, 9, 1)
                .slider("Snowball Slot", 1, 9, 1)
                .slider("Bow Slot", 1, 9, 1)
                .slider("Pearl Slot", 1, 9, 1)
                .slider("TNT Slot", 1, 9, 1)
                .slider("Gapple Slot", 1, 9, 1)
                .show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
                    if (hasDuplicates(res.formValues)) {
                        player.sendMessage("§cYou cannot place to items on the same slot!§r")
                    } else {
                        player.runCommandAsync(`scoreboard players set sword inventoryHotbar ${res.formValues[0] - 1}`)
                        player.runCommandAsync(`scoreboard players set pickaxe inventoryHotbar ${res.formValues[1] - 1}`)
                        player.runCommandAsync(`scoreboard players set axe inventoryHotbar ${res.formValues[2] - 1}`)
                        player.runCommandAsync(`scoreboard players set blocks inventoryHotbar ${res.formValues[3] - 1}`)
                        player.runCommandAsync(`scoreboard players set snowball inventoryHotbar ${res.formValues[4] - 1}`)
                        player.runCommandAsync(`scoreboard players set bow inventoryHotbar ${res.formValues[5] - 1}`)
                        player.runCommandAsync(`scoreboard players set pearl inventoryHotbar ${res.formValues[6] - 1}`)
                        player.runCommandAsync(`scoreboard players set tnt inventoryHotbar ${res.formValues[7] - 1}`)
                        player.runCommandAsync(`scoreboard players set gapple inventoryHotbar ${res.formValues[8] - 1}`)

                        mc.system.runTimeout(() => {
                            player.runCommandAsync('clear')
                        }, 5)
                    }
                })
                break
        }
    })
}

var layers = {
    0: "minecraft:white_wool",
    1: "minecraft:planks"
}

function defenseSettings (player) {
    let defenseForm = new mcui.ActionFormData()
    .title("§l§2Defense Settings§r")
    .button("Edit Inventory", "textures/ui/pencil_edit_icon")
    .button("Treasure Defense", "textures/items/map_filled")
    .button("§cReset Highscores§r", "textures/ui/cancel")
    .show(player).then(res => {
         if (res.canceled) return player.sendMessage("§cChanges canceled.")
         switch(res.selection) {
             case 0:
                player.runCommandAsync("function pvp/editinv")
                let editInvForm = new mcui.ModalFormData()
                .title("§l§2Defense Settings§r")
                .slider("Pickaxe Slot", 1, 9, 1)
                .slider("Axe Slot", 1, 9, 1)
                .slider("Wool Slot", 1, 9, 1)
                .slider("Planks Slot", 1, 9, 1)
                .slider("Concrete Slot", 1, 9, 1)
                .slider("Endstone Slot", 1, 9, 1)
                .slider("Glass Slot", 1, 9, 1)
                .slider("Settings Slot", 1, 9, 1)
                .slider("Hub slot", 1, 9, 1)
                .show(player).then(res => {
                    if (res.canceled) return player.sendMessage("§cChanges canceled.§r")
                    if (hasDuplicates(res.formValues)) {
                        player.sendMessage("§cYou cannot place to items on the same slot!§r")
                    } else {
                        player.runCommandAsync(`scoreboard players set pickaxe inventoryDefense ${res.formValues[0] - 1}`)
                        player.runCommandAsync(`scoreboard players set axe inventoryDefense ${res.formValues[1] - 1}`)
                        player.runCommandAsync(`scoreboard players set wool inventoryDefense ${res.formValues[2] - 1}`)
                        player.runCommandAsync(`scoreboard players set planks inventoryDefense ${res.formValues[3] - 1}`)
                        player.runCommandAsync(`scoreboard players set concrete inventoryDefense ${res.formValues[4] - 1}`)
                        player.runCommandAsync(`scoreboard players set endstone inventoryDefense ${res.formValues[5] - 1}`)
                        player.runCommandAsync(`scoreboard players set glass inventoryDefense ${res.formValues[6] - 1}`)
                        player.runCommandAsync(`scoreboard players set settings inventoryDefense ${res.formValues[7] - 1}`)
                        player.runCommandAsync(`scoreboard players set hub inventoryDefense ${res.formValues[8] - 1}`)

                        mc.system.runTimeout(() => {
                            player.runCommandAsync('clear')
                        }, 5)
                    }
                })
                break
             case 1:
                let layersForm = new mcui.ModalFormData()
                .title("§l§2Defense Settings§r")
                .slider("Number of Layers", 1, 2, 1)
                .show(player).then(res => {
                    let blocksForm = new mcui.ModalFormData()
                    for (let i = 0; i < res.formValues[0]; i++) {
                        blocksForm.dropdown(`Layer ${i+1}`, [ "Wool", "Planks", "Concrete", "Endstone", "Glass" ]);
                    }
                    blocksForm.show(player).then(res => {
                        console.error(res.formValues[0])
                        var index = 0
                        layers = {}
                        for (let blockNumber of res.formValues) {
                            if (blockNumber == 0) {
                                layers[index] = "minecraft:white_wool"
                                index ++
                            } else if (blockNumber == 1) {
                                layers[index] = "minecraft:planks"
                                index ++
                            } else if (blockNumber == 2) {
                                layers[index] = "minecraft:white_concrete"
                                index ++
                            } else if (blockNumber == 3) {
                                layers[index] = "minecraft:end_stone"
                                index ++
                            } else if (blockNumber == 4) {
                                layers[index] = "minecraft:glass"
                                index ++
                            }
                        }
                    })
                })
                break
            case 2:
                let resetForm = new mcui.MessageFormData()
                .title("§l§2Defense Settings§r")
                .body("Are you sure you want to erase your Treasure Defense highscores? §cThis action is irreversible!")
                .button2("Yes, do it!")
                .button1("No, keep my highscores")
                .show(player).then(res => {
                    if (res.selection == 1) {
                        player.runCommandAsync("scoreboard players set defense highscore 0")
                        player.sendMessage("§cSuccessfully erased highscores.")
                    } else {
                        return
                    }
                })
                break
         }
    })
}

function oreSettings (player) {
    let oreForm = new mcui.ActionFormData()
    .title("§l§4Ore Settings§r")
.body("Customize your settings")
    .button("Difficulty", "textures/items/netherite_sword")
    .button("Pickaxe", "textures/items/iron_pickaxe")    
    .show(player).then(res => {
        switch (res.selection) {
            case 0:
                let diffForm = new mcui.ActionFormData()
                .title("§l§4Ore Settings§r")
                .body("Select a difficulty")
                if (player.hasTag("easy")) {
                    diffForm
                    .button("§2Easy§r\n§e- Selected -")
                    .button("§cHard§r")
                } else {
                    diffForm
                    .button("§2Easy§r")
                    .button("§cHard§r\n§e- Selected -")
                }
                diffForm.show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("tag @s add easy")
                        case 1:
                            player.runCommandAsync("tag @s remove easy")
                    }
                })
                break
            case 1:
                let pickForm = new mcui.ActionFormData()
                .title("§l§4Ore Settings§r")
                .body("Select a pickaxe")
                if (player.hasTag("iron")) {
                    pickForm
                    .button("§5Netherite Pickaxe", "textures/items/netherite_pickaxe")
                    .button("§bDiamond Pickaxe", "textures/items/diamond_pickaxe")
                    .button("§8Iron Pickaxe§r\n§e- Selected -", "textures/items/iron_pickaxe")
                } else if (player.hasTag("diamond")) {
                    pickForm
                    .button("§5Netherite Pickaxe", "textures/items/netherite_pickaxe")
                    .button("§bDiamond Pickaxe\n§e- Selected -", "textures/items/diamond_pickaxe")
                    .button("§8Iron Pickaxe", "textures/items/iron_pickaxe")
                } else if (player.hasTag("netherite")) {
                    pickForm
                    .button("§5Netherite Pickaxe§r\n§e- Selected -", "textures/items/netherite_pickaxe")
                    .button("§bDiamond Pickaxe", "textures/items/diamond_pickaxe")
                    .button("§8Iron Pickaxe", "textures/items/iron_pickaxe")
                } else {
                    pickForm
                    .button("§5Netherite Pickaxe§r\n§e- Selected -", "textures/items/netherite_pickaxe")
                    .button("§bDiamond Pickaxe", "textures/items/diamond_pickaxe")
                    .button("§8Iron Pickaxe", "textures/items/iron_pickaxe")
                }
                pickForm.show(player).then(res => {
                    switch (res.selection) {
                        case 0:
                            player.runCommandAsync("tag @s remove iron")
                            player.runCommandAsync("tag @s remove diamond")
                            player.runCommandAsync("tag @s remove netherite")
                            player.runCommandAsync("tag @s add netherite")
                            break
                        case 1:
                            player.runCommandAsync("tag @s remove iron")
                            player.runCommandAsync("tag @s remove diamond")
                            player.runCommandAsync("tag @s remove netherite")
                            player.runCommandAsync("tag @s add diamond")
                            break
                        case 2:
                            player.runCommandAsync("tag @s remove iron")
                            player.runCommandAsync("tag @s remove diamond")
                            player.runCommandAsync("tag @s remove netherite")
                            player.runCommandAsync("tag @s add iron")
                            break
                    }
                })
                break
        }
    })
}