From 8e9109d3b190f5451793fe79929ac89b3876c4ff Mon Sep 17 00:00:00 2001 From: Obsidian <108832807+Obsidianninja11@users.noreply.github.com> Date: Fri, 1 Mar 2024 01:39:56 -0900 Subject: Added Command /shconfig toggle (#835) --- .../test/SkyHanniConfigSearchResetCommand.kt | 54 +++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt index 8bfcbfd22..bb80462da 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt @@ -37,8 +37,9 @@ object SkyHanniConfigSearchResetCommand { "reset" -> resetCommand(args) "search" -> searchCommand(args) "set" -> setCommand(args) + "toggle" -> toggleCommand(args) - else -> "§c/shconfig " + else -> "§c/shconfig " } } @@ -58,7 +59,10 @@ object SkyHanniConfigSearchResetCommand { field.set(parent, defaultObject) return "§eSuccessfully reset config element '$term'" } catch (e: Throwable) { - ErrorManager.logError(e, "Could not reset config element '$term'") + ErrorManager.logErrorWithData( + e, "Could not reset config element '$term'", + "args" to args.joinToString(" ") + ) return "§cCould not reset config element '$term'" } } @@ -69,7 +73,10 @@ object SkyHanniConfigSearchResetCommand { return try { startSearch(args) } catch (e: Exception) { - ErrorManager.logError(e, "Error while trying to search config") + ErrorManager.logErrorWithData( + e, "Error while trying to search config", + "args" to args.joinToString(" ") + ) "§cError while trying to search config" } } @@ -107,13 +114,41 @@ object SkyHanniConfigSearchResetCommand { val shimmy = Shimmy.makeShimmy(root, list) ?: return "§cCould not change config element '$term', not found!" return try { shimmy.setJson(element) - "§eChanged config element $term." + "§eChanged config element $term to $rawJson." } catch (e: Exception) { - ErrorManager.logError(e, "Could not change config element '$term' to '$rawJson'") + ErrorManager.logErrorWithData( + e, "Could not change config element '$term' to '$rawJson'", + "args" to args.joinToString(" ") + ) "§cCould not change config element '$term' to '$rawJson'" } } + private suspend fun toggleCommand(args: Array): String { + if (args.size == 1 || args.size == 3) return "§c/shconfig toggle [value 1] [value 2]" + + val path = args[1] + val rawJson1 = if (args.size > 2) args[2] else "true" + val rawJson2 = if (args.size > 2) args[3] else "false" + + return try { + val (argsFilter) = createFilter(true) { path.lowercase() } + val (classFilter) = createFilter(false) { path.lowercase() } + + val currentValue = findConfigElements(argsFilter, classFilter, onlyValue = true).toString() + val newValue = if (currentValue == "[$rawJson1]") rawJson2 else rawJson1 + + setCommand(arrayOf("set", path, newValue)) + + } catch (e: Exception) { + ErrorManager.logErrorWithData( + e, "Error while trying to toggle config element", + "args" to args.joinToString(" ") + ) + "§cError while trying to toggle config element" + } + } + private fun createFilter(condition: Boolean, searchTerm: () -> String): Pair<(String) -> Boolean, String> { return if (condition && searchTerm() != "all") { val term = searchTerm() @@ -146,6 +181,7 @@ object SkyHanniConfigSearchResetCommand { private fun findConfigElements( configFilter: (String) -> Boolean, classFilter: (String) -> Boolean, + onlyValue: Boolean = false ): MutableList { val list = mutableListOf() @@ -185,13 +221,19 @@ object SkyHanniConfigSearchResetCommand { objectName.startsWith("at.hannibal2.skyhanni.config.Storage")) ) { "" + } else if (onlyValue) { + objectName } else { "$className = $objectName" } } else "null" if (configFilter(name)) { - list.add("$name $description") + if (onlyValue) { + list.add(description) + } else { + list.add("$name $description") + } } } return list -- cgit