Skip to content

Commit

Permalink
Added the ability to disable chat errors via the repo (#2668)
Browse files Browse the repository at this point in the history
Co-authored-by: Cal <cwolfson58@gmail.com>
  • Loading branch information
NopoTheGamer and CalMWolfs authored Oct 6, 2024
1 parent 4df35d0 commit 023b44c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package at.hannibal2.skyhanni.data.jsonobjects.repo

import com.google.gson.annotations.Expose

data class RepoErrorData(
@Expose var messageExact: List<String>?,
@Expose var messageStartsWith: List<String>?,
@Expose var replaceMessage: String?,
@Expose var affectedVersions: List<String> = listOf(),
)
64 changes: 58 additions & 6 deletions src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
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.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.LorenzUtils
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

@SkyHanniModule
object ErrorManager {

// random id -> error message
Expand Down Expand Up @@ -158,12 +165,57 @@ object ErrorManager {
if (finalMessage.last() !in ".?!") {
finalMessage += "§c."
}
ChatUtils.clickableChat(
"§c[SkyHanni-${SkyHanniMod.version}]: $finalMessage Click here to copy the error into the clipboard.",
onClick = { copyError(randomId) },
"§eClick to copy!",
prefix = false,
)

var hideError = false
for (repoError in repoErrors) {
for (s in repoError.messageStartsWith ?: listOf()) {
if (rawMessage.startsWith(s)) {
hideError = true
}
}
for (s in repoError.messageExact ?: listOf()) {
if (rawMessage == s) {
hideError = true
}
}
if (hideError) {
if (repoError.replaceMessage != null) {
finalMessage = repoError.replaceMessage!!
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,
)
}

}

private var repoErrors: List<RepoErrorData> = 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<RepoErrorData>(it.readText())
}.filter {
SkyHanniMod.version in it.affectedVersions
}
} else {
listOf()
}
}

private fun buildExtraDataString(extraData: Array<out Pair<String, Any?>>): String {
Expand Down

0 comments on commit 023b44c

Please sign in to comment.