aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/SBData.kt
blob: 8b3ad96fd36fde59fe6652e1feb85b19cb36dfc1 (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
package moe.nea.firmament.util

import java.util.UUID
import net.hypixel.modapi.HypixelModAPI
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket
import kotlin.jvm.optionals.getOrNull
import kotlin.time.Duration.Companion.seconds
import moe.nea.firmament.events.AllowChatEvent
import moe.nea.firmament.events.ProcessChatEvent
import moe.nea.firmament.events.ServerConnectedEvent
import moe.nea.firmament.events.SkyblockServerUpdateEvent

object SBData {
	private val profileRegex = "Profile ID: ([a-z0-9\\-]+)".toRegex()
	val profileSuggestTexts = listOf(
		"CLICK THIS TO SUGGEST IT IN CHAT [DASHES]",
		"CLICK THIS TO SUGGEST IT IN CHAT [NO DASHES]",
	)
	var profileId: UUID? = null
	val genericProfileId: String? get() = if (isTabCube) "tabcube-${MC.instance.session.uuidOrNull}" else profileId?.toString()

	private var hasReceivedProfile = false
	var locraw: Locraw? = null
	val skyblockLocation: SkyBlockIsland? get() = locraw?.skyblockLocation
	val hasValidLocraw get() = locraw?.server !in listOf("limbo", null)
	val isOnSkyblock get() = locraw?.gametype == "SKYBLOCK"
	var profileIdCommandDebounce = TimeMark.farPast()
	val connectionAddress: String? get() = MC.networkHandler?.serverInfo?.address
	val isTabCube get() = connectionAddress == "tapcube.minehut.gg"

	fun init() {
		ServerConnectedEvent.subscribe("SBData:onServerConnected") {
			HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket::class.java)
		}
		HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket::class.java) {
			MC.onMainThread {
				val lastLocraw = locraw
				locraw = Locraw(it.serverName,
				                it.serverType.getOrNull()?.name?.uppercase(),
				                it.mode.getOrNull(),
				                it.map.getOrNull())
				SkyblockServerUpdateEvent.publish(SkyblockServerUpdateEvent(lastLocraw, null))
				profileIdCommandDebounce = TimeMark.now()
			}
		}
		SkyblockServerUpdateEvent.subscribe("SBData:sendProfileId") {
			if (!hasReceivedProfile && isOnSkyblock && profileIdCommandDebounce.passedTime() > 10.seconds) {
				profileIdCommandDebounce = TimeMark.now()
				MC.sendServerCommand("profileid")
			}
		}
		AllowChatEvent.subscribe("SBData:hideProfileSuggest") { event ->
			if (event.unformattedString in profileSuggestTexts && profileIdCommandDebounce.passedTime() < 5.seconds) {
				event.cancel()
			}
		}
		ProcessChatEvent.subscribe(receivesCancelled = true, "SBData:loadProfile") { event ->
			val profileMatch = profileRegex.matchEntire(event.unformattedString)
			if (profileMatch != null) {
				try {
					profileId = UUID.fromString(profileMatch.groupValues[1])
					hasReceivedProfile = true
				} catch (e: IllegalArgumentException) {
					profileId = null
					e.printStackTrace()
				}
			}
		}
	}
}