aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/HypixelData.kt
blob: 53a1286d312d750e5ee302d1e63edcf9750ccc7b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.utils.LorenzLogger
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TabListUtils
import net.minecraft.client.Minecraft
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent

class HypixelData {

    companion object {
        var hypixel = false
        var skyblock = false
        var skyBlockIsland: String = ""

        fun readSkyBlockArea(): String {
            for (line in ScoreboardData.sidebarLinesFormatted()) {
                if (line.startsWith(" §7⏣ ")) {
                    return line.substring(5).removeColor()
                }
            }

            return "invalid"
        }

    }

    var loggerIslandChange = LorenzLogger("debug/island_change")

    @SubscribeEvent
    fun onConnect(event: FMLNetworkEvent.ClientConnectedToServerEvent) {
        hypixel = Minecraft.getMinecraft().runCatching {
            !event.isLocal && (thePlayer?.clientBrand?.lowercase()?.contains("hypixel")
                ?: currentServerData?.serverIP?.lowercase()?.contains("hypixel") ?: false)
        }.onFailure { it.printStackTrace() }.getOrDefault(false)
    }

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        skyblock = false
    }

    @SubscribeEvent
    fun onDisconnect(event: FMLNetworkEvent.ClientDisconnectionFromServerEvent) {
        hypixel = false
        skyblock = false
    }

    @SubscribeEvent
    fun onStatusBar(event: LorenzChatEvent) {
        if (!hypixel) return

        val message = event.message.removeColor().lowercase()

        if (message.startsWith("your profile was changed to:")) {
            val stripped = message.replace("your profile was changed to:", "").replace("(co-op)", "").trim()
            ProfileJoinEvent(stripped).postAndCatch()
        }
        if (message.startsWith("you are playing on profile:")) {
            val stripped = message.replace("you are playing on profile:", "").replace("(co-op)", "").trim()
            ProfileJoinEvent(stripped).postAndCatch()

        }
    }

    var tick = 0

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (!hypixel) return
        if (event.phase != TickEvent.Phase.START) return

        tick++

        if (tick % 5 != 0) return

        val newState = checkScoreboard()
        if (newState) {
            checkIsland()
        }

        if (newState == skyblock) return
        skyblock = newState
    }

    private fun checkIsland() {
        var newIsland = ""
        var guesting = false
        for (line in TabListUtils.getTabList()) {
            if (line.startsWith("§r§b§lArea: ")) {
                newIsland = line.split(": ")[1].removeColor()
            }
            if (line == "§r Status: §r§9Guest§r") {
                guesting = true
            }
        }
        if (guesting) {
            newIsland = "$newIsland guesting"
        }

        if (skyBlockIsland != newIsland) {
            IslandChangeEvent(newIsland, skyBlockIsland).postAndCatch()
            loggerIslandChange.log(newIsland)
            skyBlockIsland = newIsland
        }
    }

    private fun checkScoreboard(): Boolean {
        val minecraft = Minecraft.getMinecraft()
        val world = minecraft.theWorld ?: return false

        val sidebarObjective = world.scoreboard.getObjectiveInDisplaySlot(1) ?: return false

        val displayName = sidebarObjective.displayName

        return displayName.removeColor().contains("SKYBLOCK")

    }

}