diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-21 11:16:34 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-21 11:16:34 +0200 |
commit | 830fdb7022ac173b421daeddfbc7e67c0d9f891b (patch) | |
tree | a9e4397add1cc121e9cea0a35500de5ccd66f5a9 /src/main/java/at/hannibal2/skyhanni/data | |
parent | 020c278b3c6cdff38449e0fe67bb36bf196ed045 (diff) | |
download | skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.gz skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.bz2 skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.zip |
better repo error handling
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
5 files changed, 36 insertions, 36 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/BingoAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BingoAPI.kt index ca2557af0..857b73d3f 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/BingoAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/BingoAPI.kt @@ -9,13 +9,11 @@ object BingoAPI { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - event.getConstant<BingoRanks>("BingoRanks")?.let { - ranks = it.ranks - } + ranks = event.getConstant<BingoRanks>("BingoRanks").ranks } fun getRank(text: String) = ranks.entries.find { text.contains(it.key) }?.value fun getIcon(searchRank: Int) = ranks.entries.find { it.value == searchRank }?.key -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt index 277fdb56c..02d003398 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt @@ -99,7 +99,7 @@ object GardenCropMilestones { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - val data = event.getConstant<GardenJson>("Garden") ?: return + val data = event.getConstant<GardenJson>("Garden") cropMilestoneData = data.crop_milestones } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoError.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoError.kt new file mode 100644 index 000000000..60606ffa9 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoError.kt @@ -0,0 +1,6 @@ +package at.hannibal2.skyhanni.data.repo + +class RepoError : Error { + constructor(errorMessage: String) : super(errorMessage) + constructor(errorMessage: String, cause: Throwable) : super(errorMessage, cause) +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt index ed35272cd..df19aac2e 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt @@ -25,6 +25,7 @@ class RepoManager(private val configLocation: File) { private val gson get() = ConfigManager.gson private var latestRepoCommit: String? = null private val repoLocation: File = File(configLocation, "repo") + private var error = false fun loadRepoInformation() { atomicShouldManuallyReload.set(true) @@ -39,12 +40,12 @@ class RepoManager(private val configLocation: File) { fun updateRepo() { atomicShouldManuallyReload.set(true) - fetchRepository(true).thenRun { this.reloadRepository("Repo updated successful :)") } + fetchRepository(true).thenRun { this.reloadRepository("Repo updated successful.") } } fun reloadLocalRepo() { atomicShouldManuallyReload.set(true) - reloadRepository("Repo loaded from local files successful :)") + reloadRepository("Repo loaded from local files successful.") } private fun fetchRepository(command: Boolean): CompletableFuture<Boolean> { @@ -62,7 +63,9 @@ class RepoManager(private val configLocation: File) { e.printStackTrace() } if (latestRepoCommit == null || latestRepoCommit!!.isEmpty()) return@supplyAsync false - if (File(configLocation, "repo").exists() && currentCommitJSON != null && currentCommitJSON["sha"].asString == latestRepoCommit) { + val file = File(configLocation, "repo") + if (file.exists() && currentCommitJSON != null && currentCommitJSON["sha"].asString == latestRepoCommit + ) { if (command) { LorenzUtils.chat("§e[SkyHanni] §7The repo is already up to date!") atomicShouldManuallyReload.set(false) @@ -120,15 +123,19 @@ class RepoManager(private val configLocation: File) { private fun reloadRepository(answerMessage: String = ""): CompletableFuture<Void?> { val comp = CompletableFuture<Void?>() if (!atomicShouldManuallyReload.get()) return comp + ErrorManager.resetCache() Minecraft.getMinecraft().addScheduledTask { - try { - RepositoryReloadEvent(repoLocation, gson).postAndCatch() - comp.complete(null) - if (answerMessage.isNotEmpty()) { - LorenzUtils.chat("§e[SkyHanni] §a$answerMessage") - } - } catch (e: java.lang.Exception) { - ErrorManager.logError(e, "Error reading repo data!") + error = false + RepositoryReloadEvent(repoLocation, gson).postAndCatchAndBlock(true, true) { + LorenzUtils.clickableChat( + "§e[SkyHanni] Error with the repo detected, try /shupdaterepo to fix it!", + "shupdaterepo" + ) + error = true + } + comp.complete(null) + if (answerMessage.isNotEmpty() && !error) { + LorenzUtils.chat("§e[SkyHanni] §a$answerMessage") } } return comp @@ -175,4 +182,4 @@ class RepoManager(private val configLocation: File) { ) ).use { writer -> writer.write(gson.toJson(json)) } } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt index 2438a9be3..7ec70c673 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt @@ -1,6 +1,5 @@ package at.hannibal2.skyhanni.data.repo -import at.hannibal2.skyhanni.test.command.ErrorManager import com.google.gson.Gson import java.io.BufferedReader import java.io.File @@ -8,10 +7,10 @@ import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException import java.io.InputStreamReader +import java.lang.reflect.Type import java.nio.charset.StandardCharsets import java.nio.file.Files import java.util.zip.ZipInputStream -import java.lang.reflect.Type object RepoUtils { @@ -84,23 +83,13 @@ object RepoUtils { return false } - fun <T> getConstant(repo: File, constant: String, gson: Gson, clazz: Class<T>?, type: Type? = null): T? { - if (!repo.exists()) return null - - val jsonFile = File(repo, "constants/$constant.json") + fun <T> getConstant(repoLocation: File, constant: String, gson: Gson, clazz: Class<T>?, type: Type? = null): T { + val name = "constants/$constant.json" + val jsonFile = File(repoLocation, name) if (!jsonFile.isFile) { - ErrorManager.logError( - Error("File '$jsonFile' not found!"), - "File in repo missing! ($jsonFile). Try §e/shupdaterepo" - ) - return null + throw RepoError("Repo file '$name' not found.") } - BufferedReader( - InputStreamReader( - FileInputStream(jsonFile), - StandardCharsets.UTF_8 - ) - ).use { reader -> + BufferedReader(InputStreamReader(FileInputStream(jsonFile), StandardCharsets.UTF_8)).use { reader -> if (type == null) { return gson.fromJson(reader, clazz) } else { @@ -108,4 +97,4 @@ object RepoUtils { } } } -}
\ No newline at end of file +} |