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 | |
| parent | 020c278b3c6cdff38449e0fe67bb36bf196ed045 (diff) | |
| download | skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.gz skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.bz2 skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.zip | |
better repo error handling
34 files changed, 204 insertions, 239 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 +} diff --git a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt index 32c552221..e4e6f87c5 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt @@ -1,8 +1,10 @@ package at.hannibal2.skyhanni.events +import at.hannibal2.skyhanni.mixins.transformers.AccessorEventBus import at.hannibal2.skyhanni.test.command.ErrorManager import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.eventhandler.Event +import net.minecraftforge.fml.common.eventhandler.IEventListener abstract class LorenzEvent : Event() { @@ -10,16 +12,35 @@ abstract class LorenzEvent : Event() { this::class.simpleName } - fun postAndCatch(): Boolean { - return runCatching { - postWithoutCatch() - }.onFailure { - ErrorManager.logError( - it, - "Caught an ${it::class.simpleName ?: "error"} at ${eventName}: '${it.message}'" - ) - }.getOrDefault(isCanceled) + fun postAndCatch() = postAndCatchAndBlock {} + + fun postAndCatchAndBlock( + printError: Boolean = true, + stopOnError: Boolean = false, + onError: (Throwable) -> Unit, + ): Boolean { + for (listener in getListeners()) { + try { + listener.invoke(this) + } catch (e: Throwable) { + if (printError) { + val callerName = listener.toString().split(" ")[1].split("@")[0].split(".").last() + ErrorManager.logError( + e, + "Caught an ${e::class.simpleName ?: "error"} at $eventName in $callerName: '${e.message}'" + ) + } + onError(e) + if (stopOnError) break + } + } + return if (isCancelable) isCanceled else false + } + + private fun getListeners(): Array<out IEventListener> { + val accessorEventBus = MinecraftForge.EVENT_BUS as AccessorEventBus + return listenerList.getListeners(accessorEventBus.busId) } fun postWithoutCatch() = MinecraftForge.EVENT_BUS.post(this) -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt index 8010564a0..e0eb01b9b 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt @@ -1,7 +1,7 @@ package at.hannibal2.skyhanni.events +import at.hannibal2.skyhanni.data.repo.RepoError import at.hannibal2.skyhanni.data.repo.RepoUtils -import at.hannibal2.skyhanni.test.command.ErrorManager import com.google.gson.Gson import com.google.gson.JsonObject import java.io.File @@ -10,13 +10,10 @@ import java.lang.reflect.Type class RepositoryReloadEvent(val repoLocation: File, val gson: Gson) : LorenzEvent() { fun getConstant(constant: String) = getConstant<JsonObject>(constant) - inline fun <reified T : Any> getConstant(constant: String, type: Type? = null) = try { + inline fun <reified T : Any> getConstant(constant: String, type: Type? = null): T = try { + if (!repoLocation.exists()) throw RepoError("Repo folder does not exist!") RepoUtils.getConstant(repoLocation, constant, gson, T::class.java, type) } catch (e: Exception) { - ErrorManager.logError( - Exception("Repo parsing error while trying to read constant '$constant'", e), - "Error reading repo data" - ) - null + throw RepoError("Repo parsing error while trying to read constant '$constant'", e) } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardTips.kt b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardTips.kt index a112616a3..318888fec 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardTips.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardTips.kt @@ -20,9 +20,7 @@ class BingoCardTips { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - event.getConstant<BingoJson>("Bingo")?.let { - tips = it.bingo_tips - } + tips = event.getConstant<BingoJson>("Bingo").bingo_tips } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt index af0d1b805..6de251af5 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt @@ -29,25 +29,18 @@ class PlayerChatFilter { var countCategories = 0 var countFilters = 0 - try { - val data = event.getConstant("PlayerChatFilter") ?: return - - for (category in data["filters"].asJsonArray) { - val jsonObject = category.asJsonObject - val description = jsonObject["description"].asString - val filter = MultiFilter() - filter.load(jsonObject) - filters[description] = filter - - countCategories++ - countFilters += filter.count() - } - - LorenzUtils.debug("Loaded $countFilters filters in $countCategories categories from repo") - - } catch (e: Exception) { - e.printStackTrace() - LorenzUtils.error("error in RepositoryReloadEvent") + val data = event.getConstant("PlayerChatFilter") + for (category in data["filters"].asJsonArray) { + val jsonObject = category.asJsonObject + val description = jsonObject["description"].asString + val filter = MultiFilter() + filter.load(jsonObject) + filters[description] = filter + + countCategories++ + countFilters += filter.count() } + + LorenzUtils.debug("Loaded $countFilters filters in $countCategories categories from repo") } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt index 62e6f9e9c..2c1c339dc 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt @@ -15,7 +15,7 @@ object GetFromSacksTabComplete { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - sackList = event.getConstant<SackListJson>("Sacks")?.sackList ?: return + sackList = event.getConstant<SackListJson>("Sacks").sackList } fun handleTabComplete(command: String): List<String>? { @@ -42,4 +42,4 @@ object GetFromSacksTabComplete { } fun isEnabled() = LorenzUtils.inSkyBlock && config.gfsSack -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/SeaCreatureManager.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/SeaCreatureManager.kt index 9e54fef06..c41129c5f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/SeaCreatureManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/SeaCreatureManager.kt @@ -35,31 +35,26 @@ class SeaCreatureManager { allFishingMobs.clear() var counter = 0 - try { - val data = event.getConstant<Map<String, SeaCreatureJson.Variant>>("SeaCreatures", SeaCreatureJson.TYPE) ?: return - val allFishingMobs = mutableMapOf<String,SeaCreature>() + val data = event.getConstant<Map<String, SeaCreatureJson.Variant>>("SeaCreatures", SeaCreatureJson.TYPE) + val allFishingMobs = mutableMapOf<String, SeaCreature>() - for (variant in data.values) { - val chatColor = variant.chat_color - for ((displayName, seaCreature) in variant.sea_creatures) { - val chatMessage = seaCreature.chat_message - val fishingExperience = seaCreature.fishing_experience - val rarity = seaCreature.rarity - val rare = seaCreature.rare ?: false + for (variant in data.values) { + val chatColor = variant.chat_color + for ((displayName, seaCreature) in variant.sea_creatures) { + val chatMessage = seaCreature.chat_message + val fishingExperience = seaCreature.fishing_experience + val rarity = seaCreature.rarity + val rare = seaCreature.rare ?: false - val creature = SeaCreature(displayName, fishingExperience, chatColor, rare, rarity) - seaCreatureMap[chatMessage] = creature - allFishingMobs[displayName] = creature - counter++ - } + val creature = SeaCreature(displayName, fishingExperience, chatColor, rare, rarity) + seaCreatureMap[chatMessage] = creature + allFishingMobs[displayName] = creature + counter++ } - SeaCreatureManager.allFishingMobs = allFishingMobs - LorenzUtils.debug("Loaded $counter sea creatures from repo") - - } catch (e: Exception) { - e.printStackTrace() - LorenzUtils.error("error in RepositoryReloadEvent") } + SeaCreatureManager.allFishingMobs = allFishingMobs + LorenzUtils.debug("Loaded $counter sea creatures from repo") + } companion object { @@ -75,4 +70,4 @@ class SeaCreatureManager { return seaCreatureMap.getOrDefault(message, null) } } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt index c2f4e64fe..4d8b547a4 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/trophy/TrophyFishManager.kt @@ -6,20 +6,13 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.jsonobjects.TrophyFishJson import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - class TrophyFishManager { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - try { - val json = event.getConstant<TrophyFishJson>("TrophyFish") - ?: error("Could not read repo data from TrophyFish.json") - trophyFishInfo = json.trophy_fish - LorenzUtils.debug("Loaded trophy fish from repo") - } catch (e: Exception) { - e.printStackTrace() - LorenzUtils.error("error in RepositoryReloadEvent") - } + val json = event.getConstant<TrophyFishJson>("TrophyFish") + trophyFishInfo = json.trophy_fish + LorenzUtils.debug("Loaded trophy fish from repo") } companion object { @@ -32,4 +25,4 @@ class TrophyFishManager { fun getInfoByName(name: String) = trophyFishInfo.values.find { it.displayName == name } } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt index e67de332f..05b78d047 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt @@ -243,10 +243,10 @@ object GardenAPI { @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - val data = event.getConstant<GardenJson>("Garden") ?: return + val data = event.getConstant<GardenJson>("Garden") gardenExperience = data.garden_exp } private var gardenExperience = listOf<Int>() private const val gardenOverflowExp = 10000 // can be changed I guess -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt index 3cd2e0a39..8b0b3d70d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterOverlay.kt @@ -15,7 +15,6 @@ import at.hannibal2.skyhanni.features.bazaar.BazaarApi import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.features.garden.composter.ComposterAPI.getLevel import at.hannibal2.skyhanni.utils.InventoryUtils -import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName_old import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.KeyboardManager @@ -332,7 +331,6 @@ class ComposterOverlay { if (currentTimeType == TimeType.COMPOST) "Compost multiplier" else "Composts per $timeText" newList.addAsSingletonList(" §7$compostPerTitle: §e${multiplier.round(2)}$compostPerTitlePreview") - val organicMatterPrice = getPrice(organicMatterItem) val organicMatterFactor = organicMatterFactors[organicMatterItem]!! @@ -360,7 +358,6 @@ class ComposterOverlay { " §7Material costs per $timeText: §6${NumberUtil.format(totalCost)}$materialCostFormatPreview" newList.addAsSingletonList(materialCostFormat) - val priceCompost = getPrice("COMPOST") val profit = ((priceCompost * multiDropFactor) - (fuelPricePer + organicMatterPricePer)) * timeMultiplier val profitPreview = @@ -463,7 +460,8 @@ class ComposterOverlay { if (sackStatus == SackStatus.MISSING || sackStatus == SackStatus.OUTDATED) { if (sackStatus == SackStatus.OUTDATED) LorenzUtils.sendCommandToServer("gfs $internalName ${itemsNeeded - having}") - val sackType = if (internalName == "VOLTA" || internalName == "OIL_BARREL") "Mining" else "Enchanted Agronomy" // TODO Add sack type repo data + val sackType = + if (internalName == "VOLTA" || internalName == "OIL_BARREL") "Mining" else "Enchanted Agronomy" // TODO Add sack type repo data LorenzUtils.clickableChat( "§e[SkyHanni] Sacks could not be loaded. Click here and open your §9$sackType Sack §eto update the data!", "sax" @@ -513,15 +511,9 @@ class ComposterOverlay { } private fun updateOrganicMatterFactors() { - try { - val garden = this.garden ?: return - organicMatterFactors = updateOrganicMatterFactors(garden.organic_matter) - fuelFactors = garden.fuel - - } catch (e: Exception) { - e.printStackTrace() - LorenzUtils.error("error in RepositoryReloadEvent") - } + val garden = this.garden ?: return + organicMatterFactors = updateOrganicMatterFactors(garden.organic_matter) + fuelFactors = garden.fuel } private fun updateOrganicMatterFactors(baseValues: Map<String, Double>): Map<String, Double> { @@ -580,4 +572,4 @@ class ComposterOverlay { event.move(3, "garden.composterOverlayFuelExtrasPos", "garden.composters.overlayFuelExtrasPos") event.move(3, "garden.composterRoundDown", "garden.composters.roundDown") } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingArmorDrops.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingArmorDrops.kt index 1f35e0647..fc68546e8 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingArmorDrops.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingArmorDrops.kt @@ -12,7 +12,6 @@ import at.hannibal2.skyhanni.features.garden.CropType import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.utils.InventoryUtils |
