aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/profileviewer/ProfileViewer.kt
blob: 63cb5a83c336c7d355e978777099c8fadc3d0c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package moe.nea.firmament.gui.profileviewer

import io.github.cottonmc.cotton.gui.client.CottonClientScreen
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WTabPanel
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.http.URLProtocol
import io.ktor.http.path
import java.util.UUID
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import kotlinx.coroutines.launch
import net.minecraft.text.Text
import moe.nea.firmament.Firmament
import moe.nea.firmament.apis.AshconNameLookup
import moe.nea.firmament.apis.Member
import moe.nea.firmament.apis.PlayerData
import moe.nea.firmament.apis.PlayerResponse
import moe.nea.firmament.apis.Profile
import moe.nea.firmament.apis.Profiles
import moe.nea.firmament.util.ScreenUtil

class ProfileViewer(
    val primaryPlayer: UUID,
    val playerNames: Map<UUID, String>,
    val accountData: Map<UUID, PlayerData>,
    val profile: Profile,
) : LightweightGuiDescription() {

    val member: Member = profile.members[primaryPlayer] ?: error("Primary player not in profile")
    val primaryName: String = playerNames[primaryPlayer] ?: error("Primary player has no name")
    val account: PlayerData = accountData[primaryPlayer] ?: error("Primary player has no data")

    init {
        val panel = WTabPanel().also { rootPanel = it }
        panel.backgroundPainter
        listOf<ProfilePage>(SkillPage)
            .forEach { page ->
                panel.add(page.getElements(this)) {
                    it.icon(page.icon)
                    it.tooltip(page.text)
                }
            }
    }

    companion object {
        fun onCommand(source: FabricClientCommandSource, name: String) {
            source.sendFeedback(Text.translatable("firmament.pv.lookingup", name))
            Firmament.coroutineScope.launch {
                val nameData = Firmament.httpClient.get("https://api.ashcon.app/mojang/v2/user/$name").body<AshconNameLookup>()
                val names = mapOf(nameData.uuid to nameData.username)
                val data = Firmament.httpClient.get {
                    url {
                        protocol = URLProtocol.HTTPS
                        host = "api.hypixel.net"
                        path("player")
                        parameter("key", "e721a103-96e0-400f-af2a-73b2a91007b1")
                        parameter("uuid", nameData.uuid)
                    }
                }.body<PlayerResponse>()
                val accountData = mapOf(data.player.uuid to data.player)
                val playerUuid = data.player.uuid
                val profiles = Firmament.httpClient.get {
                    url {
                        protocol = URLProtocol.HTTPS
                        host = "api.hypixel.net"
                        path("skyblock", "profiles")
                        parameter("key", "e721a103-96e0-400f-af2a-73b2a91007b1")
                        parameter("uuid", playerUuid)
                    }
                }.body<Profiles>()
                val profile = profiles.profiles.find { it.selected }
                if (profile == null) {
                    source.sendFeedback(Text.translatable("firmament.pv.noprofile", name))
                    return@launch
                }
                ScreenUtil.setScreenLater(
                    CottonClientScreen(
                        ProfileViewer(
                            playerUuid, names, accountData, profile
                        )
                    )
                )
            }
        }
    }
}