@@ -2,7 +2,12 @@ package com.github.subat0m1c.hatecheaters.utils
22
33import com.github.subat0m1c.hatecheaters.utils.ChatUtils
44import 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
59import com.github.subat0m1c.hatecheaters.HateCheaters
10+ import net.minecraft.event.ClickEvent
611import kotlinx.serialization.*
712import kotlinx.serialization.json.*
813import 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}
0 commit comments