From 497d652aa83ed81e04f9400ae84362642585dfca Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 6 Oct 2024 22:48:31 +0200 Subject: code cleanup in the chat error repo logic --- .../data/jsonobjects/repo/RepoErrorData.kt | 18 +++++-- .../skyhanni/test/command/ErrorManager.kt | 60 +++++++++------------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/RepoErrorData.kt b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/RepoErrorData.kt index 486617651..3ec529db3 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/RepoErrorData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/RepoErrorData.kt @@ -1,10 +1,18 @@ package at.hannibal2.skyhanni.data.jsonobjects.repo import com.google.gson.annotations.Expose +import com.google.gson.annotations.SerializedName -data class RepoErrorData( - @Expose var messageExact: List?, - @Expose var messageStartsWith: List?, - @Expose var replaceMessage: String?, - @Expose var affectedVersions: List = listOf(), +data class RepoErrorJson( + @Expose @SerializedName("changed_error_messages") val changedErrorMessages: List, ) + +data class RepoErrorData( + @Expose @SerializedName("message_exact") private var rawMessageExact: List?, + @Expose @SerializedName("message_starts_with") private var rawMessageStartsWith: List?, + @Expose @SerializedName("replace_message") var replaceMessage: String?, + @Expose @SerializedName("affected_versions") var affectedVersions: List = listOf(), +) { + val messageExact by lazy { rawMessageExact ?: emptyList() } + val messageStartsWith by lazy { rawMessageStartsWith ?: emptyList() } +} diff --git a/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt b/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt index e0fcf8fcb..7fed3ad02 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt @@ -1,8 +1,8 @@ package at.hannibal2.skyhanni.test.command import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.data.jsonobjects.repo.RepoErrorData +import at.hannibal2.skyhanni.data.jsonobjects.repo.RepoErrorJson import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.ChatUtils @@ -12,7 +12,6 @@ import at.hannibal2.skyhanni.utils.OSUtils import at.hannibal2.skyhanni.utils.StringUtils import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.TimeLimitedSet -import at.hannibal2.skyhanni.utils.json.fromJson import net.minecraft.client.Minecraft import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.time.Duration.Companion.minutes @@ -24,6 +23,7 @@ object ErrorManager { private val errorMessages = mutableMapOf() private val fullErrorMessages = mutableMapOf() private var cache = TimeLimitedSet>(10.minutes) + private var repoErrors: List = emptyList() private val breakAfter = listOf( "at at.hannibal2.skyhanni.config.commands.Commands\$createCommand", @@ -156,66 +156,56 @@ object ErrorManager { val extraDataString = buildExtraDataString(extraData) val rawMessage = message.removeColor() - errorMessages[randomId] = - "```\nSkyHanni ${SkyHanniMod.version}: $rawMessage\n \n$stackTrace\n$extraDataString```" + errorMessages[randomId] = "```\nSkyHanni ${SkyHanniMod.version}: $rawMessage\n \n$stackTrace\n$extraDataString```" fullErrorMessages[randomId] = "```\nSkyHanni ${SkyHanniMod.version}: $rawMessage\n(full stack trace)\n \n$fullStackTrace\n$extraDataString```" + val finalMessage = buildFinalMessage(message) ?: return + ChatUtils.clickableChat( + "§c[SkyHanni-${SkyHanniMod.version}]: $finalMessage Click here to copy the error into the clipboard.", + onClick = { copyError(randomId) }, + "§eClick to copy!", + prefix = false, + ) + } + + private fun buildFinalMessage(message: String): String? { var finalMessage = message if (finalMessage.last() !in ".?!") { finalMessage += "§c." } + val rawMessage = message.removeColor() var hideError = false for (repoError in repoErrors) { - for (s in repoError.messageStartsWith ?: listOf()) { - if (rawMessage.startsWith(s)) { + for (string in repoError.messageStartsWith) { + if (rawMessage.startsWith(string)) { hideError = true } } - for (s in repoError.messageExact ?: listOf()) { - if (rawMessage == s) { + for (string in repoError.messageExact) { + if (rawMessage == string) { hideError = true } } if (hideError) { - if (repoError.replaceMessage != null) { - finalMessage = repoError.replaceMessage!! + repoError.replaceMessage?.let { + finalMessage = it hideError = false } break } } - if (!hideError) { - ChatUtils.clickableChat( - "§c[SkyHanni-${SkyHanniMod.version}]: $finalMessage Click here to copy the error into the clipboard.", - onClick = { copyError(randomId) }, - "§eClick to copy!", - prefix = false, - ) - } - + return if (hideError) null else finalMessage } - private var repoErrors: List = listOf() - @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - val chatErrors = event.repoLocation.resolve("chat_errors") - repoErrors = if (chatErrors.exists()) { - chatErrors.listFiles() - .filter { - it != null && it.isFile && it.canRead() - } - .map { - ConfigManager.gson.fromJson(it.readText()) - }.filter { - SkyHanniMod.version in it.affectedVersions - } - } else { - listOf() - } + val data = event.getConstant("ChangedChatErrors") + val version = SkyHanniMod.version + + repoErrors = data.changedErrorMessages.filter { version in it.affectedVersions } } private fun buildExtraDataString(extraData: Array>): String { -- cgit