Skip to content

Commit 999ff7b

Browse files
committed
Should have done everything the comments said! (hopefully!)
1 parent 7032143 commit 999ff7b

File tree

4 files changed

+32
-53
lines changed

4 files changed

+32
-53
lines changed

src/main/kotlin/com/github/subat0m1c/hatecheaters/HateCheaters.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class HateCheaters {
4545
if (timesCheckedForUpdate > 1 || isUpdateCheckInProgress) return
4646

4747
isUpdateCheckInProgress = true
48-
launch(Dispatchers.IO) {
48+
launch {
4949
CheckUpdate.lookForUpdates()
5050
isUpdateCheckInProgress = false
5151
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/modules/HateCheatersModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ object HateCheatersModule : Module(
1414
) {
1515
val logJson: Boolean by BooleanSetting("Log Json", default = false, description = "Logs requested json data to Config/hatecheaters/json_logs")
1616
val debugMessages: Boolean by BooleanSetting("Debug messages", default = false, description = "Prints debug messages in your chat instead of needing to open logs.")
17-
17+
val checkUpdates: Boolean by BooleanSetting("Update Checker", default = true, description = "Checks GitHub for latest HateCheaters releases and notifies you if you are on an old version!")
1818
val server: String by StringSetting("Api Server", default = "default", hidden = false, description = "Server to be used to connect to the api. set to \"default\" to use the default. Only change if you know what you're doing. format: \"subdomain.domain.tld\"")
1919
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/CheckUpdate.kt

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package com.github.subat0m1c.hatecheaters.utils
22

33
import com.github.subat0m1c.hatecheaters.utils.ChatUtils
44
import com.github.subat0m1c.hatecheaters.utils.LogHandler.Logger
5+
import com.github.subat0m1c.hatecheaters.utils.WebUtils
6+
import me.odinmain.utils.skyblock.getChatBreak
7+
import com.github.subat0m1c.hatecheaters.utils.OdinCheck.compareVersions
8+
import com.github.subat0m1c.hatecheaters.modules.HateCheatersModule.checkUpdates
59
import com.github.subat0m1c.hatecheaters.HateCheaters
10+
import net.minecraft.event.ClickEvent
611
import kotlinx.serialization.*
712
import kotlinx.serialization.json.*
813
import kotlinx.coroutines.*
@@ -19,37 +24,30 @@ data class ReleaseInfo(
1924
val tag_name: String
2025
)
2126

22-
object CheckUpdate { // hi my goat SubAt0m1c <3
27+
object CheckUpdate {
2328
private const val githubAPIURL = "https://api.github.com/repos/subat0m1c/HateCheaters/releases/latest"
24-
private val currentVersion: String = HateCheaters.version
25-
private val updateScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
29+
private inline val currentVersion: String get() = HateCheaters.version
2630

2731
fun lookForUpdates() {
28-
updateScope.launch {
29-
try {
30-
val url = URL(githubAPIURL)
31-
val connection = url.openConnection() as HttpURLConnection
32-
connection.requestMethod = "GET"
33-
connection.setRequestProperty("Accept", "application/json")
34-
connection.setRequestProperty("User-Agent", "Mozilla/5.0")
35-
connection.connectTimeout = 5000
36-
connection.readTimeout = 5000
32+
if (!checkUpdates) {
33+
return
34+
}
3735

36+
HateCheaters.scope.launch {
37+
try {
3838
Logger.info("Currently on HateCheaters $currentVersion")
39-
40-
if (connection.responseCode == HttpURLConnection.HTTP_OK) {
41-
val jsonResponse = connection.inputStream.bufferedReader().use { it.readText() }
42-
43-
checkVersion(jsonResponse)
44-
} else {
45-
Logger.warning("Failed to check GitHub releases. HTTP ${connection.responseCode}")
39+
val jsonResponse = WebUtils.getInputStream(githubAPIURL).getOrElse {
40+
return@launch Logger.warning("Failed to fetch data from GitHub API: ${it.message}")
4641
}
42+
val jsonString = jsonResponse.bufferedReader().use { it.readText() }
43+
checkVersion(jsonString)
4744
} catch (e: Exception) {
4845
Logger.warning("Error while checking for updates: ${e.message}")
4946
}
5047
}
5148
}
5249

50+
5351
private suspend fun checkVersion(jsonResponse: String) {
5452
val releaseInfo = json.decodeFromString<ReleaseInfo>(jsonResponse)
5553
val latestVersion = releaseInfo.tag_name
@@ -60,43 +58,23 @@ object CheckUpdate { // hi my goat SubAt0m1c <3
6058
ChatUtils.modMessage("You are on a development build! Hi!")
6159
Logger.info("HC > Development Build Detected!")
6260
} else if (isNewVersionAvailable > 0) {
63-
val newReleaseURL = "https://github.com/SubAt0m1c/HateCheaters/releases/tag/"
64-
val releaseLink = "$newReleaseURL$latestVersion"
61+
val releaseLink = "https://github.com/SubAt0m1c/HateCheaters/releases/tag/$latestVersion"
6562
delay(2000)
66-
ChatUtils.modMessage("Update available! ($currentVersion$latestVersion)")
6763
ChatUtils.chatConstructor {
64+
displayText(getChatBreak())
65+
displayText("§bH§3C §8»§r Update available! ($currentVersion$latestVersion) ")
6866
clickText(
69-
"§bH§3C §8»§r Click HERE to copy the latest release link",
70-
"/hcdev writetoclipboard $releaseLink",
71-
hoverText = listOf("Click to copy the release link to clipboard") // ill try make it open the link but idk how to do that atm!
67+
"Click here to open the latest release link!",
68+
releaseLink,
69+
listOf("Click to open the release link."),
70+
ClickEvent.Action.OPEN_URL
7271
)
72+
displayText(getChatBreak())
7373
}.print()
74+
7475
Logger.info("HC > Update available.")
7576
} else {
7677
Logger.info("HC > No update needed.")
7778
}
7879
}
79-
80-
private fun compareVersions(version1: String, version2: String): Int {
81-
fun parseVersion(version: String): List<Int> {
82-
return if (version.contains(".")) {
83-
version.split(".").map { it.toIntOrNull() ?: 0 }
84-
} else {
85-
version.map { it.toString().toIntOrNull() ?: 0 }
86-
}
87-
}
88-
89-
val v1Components = parseVersion(version1)
90-
val v2Components = parseVersion(version2)
91-
92-
val maxLength = maxOf(v1Components.size, v2Components.size)
93-
val paddedV1 = v1Components + List(maxLength - v1Components.size) { 0 }
94-
val paddedV2 = v2Components + List(maxLength - v2Components.size) { 0 }
95-
96-
for (i in 0 until maxLength) {
97-
if (paddedV1[i] > paddedV2[i]) return 1
98-
if (paddedV1[i] < paddedV2[i]) return -1
99-
}
100-
return 0
101-
}
10280
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/OdinCheck.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object OdinCheck {
3030

3131
try {
3232
val currentOdin = Loader.instance().activeModList.find { it.modId == "od" || it.modId == "odclient" }?.version ?: ""
33-
if (compareVersions(currentOdin) == -1) odinWarning("Odin is outdated!", currentOdin)
33+
if (compareVersions(currentOdin, "@REQUIREDODINVERSION@") == -1) odinWarning("Odin is outdated!", currentOdin)
3434
} catch (e: Throwable) {
3535
odinWarning("An unknown error occurred trying to determine Odin version!")
3636
return
@@ -120,9 +120,10 @@ object OdinCheck {
120120
*
121121
* I think this code is bad but i cant be bothered fixing it. also can return lower than -1.
122122
*/
123-
private fun compareVersions(version1: String): Int {
123+
fun compareVersions(version1: String, version2: String): Int {
124124
val v1 = versionRegex.find(version1)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return -2
125-
val v2 = versionRegex.find("@REQUIREDODINVERSION@")?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return -2
125+
val v2 = versionRegex.find(version2)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return -2
126+
126127

127128
for (i in 0..2) {
128129
val compared = compareNumbers(v1[i], v2[i])

0 commit comments

Comments
 (0)