aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-07-21 16:10:42 +0200
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-07-21 16:10:42 +0200
commit68c6bd4834cca0fe224074209ac90f04454e4850 (patch)
tree6c4d5e03bdfd0f554a55fa644e3fc72f09e3d68a /src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt
parentb5b9133275724d24dfee182dd18a6ce546b49d66 (diff)
downloadskyhanni-68c6bd4834cca0fe224074209ac90f04454e4850.tar.gz
skyhanni-68c6bd4834cca0fe224074209ac90f04454e4850.tar.bz2
skyhanni-68c6bd4834cca0fe224074209ac90f04454e4850.zip
moved into separate methods
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt132
1 files changed, 66 insertions, 66 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt
index 37176c85c..d498d5e99 100644
--- a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt
+++ b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt
@@ -15,78 +15,78 @@ import java.lang.reflect.Field
object SkyHanniConfigSearchResetCommand {
fun command(args: Array<String>) {
- val result = runCommand(args)
- LorenzUtils.chat(result)
+ LorenzUtils.chat(runCommand(args))
}
- fun runCommand(args: Array<String>): String {
+ private fun runCommand(args: Array<String>): String {
if (args.isEmpty()) {
- return "§c[SkyHanni] This is a config-edit command, only use it if you know what you are doing!"
+ return "§c[SkyHanni] This is a powerful config-edit command, only use it if you know what you are doing!"
}
- if (args[0] == "reset") {
- if (args.size != 2) {
- return "§c/shconfig reset <config element>"
- }
- val term = args[1]
- if (term.startsWith("playerSpecific")) {
- return "§cCannot reset playerSpecific config elements. use §e/shconfig set §cinstead."
- }
- if (term.startsWith("profileSpecific")) {
- return "§cCannot reset profileSpecific config elements. use §e/shconfig set §cinstead."
- }
- try {
- val (field, defaultObject, _) = getComplexField(term, Features())
- val (_, _, parent) = getComplexField(term, SkyHanniMod.feature)
- field.set(parent, defaultObject)
- return "§eSuccessfully reset config element '$term'"
- } catch (e: Exception) {
- CopyErrorCommand.logError(e, "Could not reset config element '$term'")
- return "Could not reset config element '$term'"
- }
- } else if (args[0] == "search") {
- if (args.size == 1) {
- return "§c/shconfig search <config name> [class name]"
- }
- try {
- startSearch(args)
- } catch (e: Exception) {
- CopyErrorCommand.logError(e, "Error while trying to search config")
- return "Error while trying to search config"
- }
- } else if (args[0] == "set") {
- if (args.size < 3) {
- return "§c/shconfig set <config name> <json element>"
- }
- val term = args[1]
- var rawJson = args.drop(2).joinToString(" ")
- if (rawJson == "clipboard") {
- val readFromClipboard = OSUtils.readFromClipboard() ?: return "§cClipboard has no string!"
- rawJson = readFromClipboard
- }
- val root: Any = if (term.startsWith("config")) {
- SkyHanniMod.feature
- } else if (term.startsWith("playerSpecific")) {
- ProfileStorageData.playerSpecific ?: return "§cplayerSpecific is null!"
- } else if (term.startsWith("profileSpecific")) {
- ProfileStorageData.profileSpecific ?: return "§cprofileSpecific is null!"
- } else {
- return "§cUnknown config location!"
- }
+ return when (args[0].lowercase()) {
+ "reset" -> resetCommand(args)
+ "search" -> searchCommand(args)
+ "set" -> setCommand(args)
- val element = ConfigManager.gson.fromJson(rawJson, JsonElement::class.java)
- val list = term.split(".").drop(1)
- val shimmy = Shimmy.makeShimmy(root, list) ?: return "§cCould not change config element '$term', not found!"
- return try {
- shimmy.setJson(element)
- "§eChanged config element $term."
- } catch (e: Exception) {
- CopyErrorCommand.logError(e, "Could not change config element '$term' to '$rawJson'")
- "Could not change config element '$term' to '$rawJson'"
- }
+ else -> "§c/shconfig <search;reset;set>"
}
+ }
+
+ private fun resetCommand(args: Array<String>): String {
+ if (args.size != 2) return "§c/shconfig reset <config element>"
+ val term = args[1]
+ if (term.startsWith("playerSpecific")) return "§cCannot reset playerSpecific! Use §e/shconfig set §cinstead."
+ if (term.startsWith("profileSpecific")) return "§cCannot reset profileSpecific! Use §e/shconfig set §cinstead."
+
+ return try {
+ val (field, defaultObject, _) = getComplexField(term, Features())
+ val (_, _, parent) = getComplexField(term, SkyHanniMod.feature)
+ field.set(parent, defaultObject)
+ "§eSuccessfully reset config element '$term'"
+ } catch (e: Exception) {
+ CopyErrorCommand.logError(e, "Could not reset config element '$term'")
+ "Could not reset config element '$term'"
+ }
+ }
+
+ private fun searchCommand(args: Array<String>): String {
+ if (args.size == 1) return "§c/shconfig search <config name> [class name]"
- return "§c/shconfig <search;reset;set>"
+ return try {
+ startSearch(args)
+ } catch (e: Exception) {
+ CopyErrorCommand.logError(e, "Error while trying to search config")
+ "Error while trying to search config"
+ }
+ }
+
+ private fun setCommand(args: Array<String>): String {
+ if (args.size < 3) return "§c/shconfig set <config name> <json element>"
+ val term = args[1]
+ var rawJson = args.drop(2).joinToString(" ")
+ if (rawJson == "clipboard") {
+ val readFromClipboard = OSUtils.readFromClipboard() ?: return "§cClipboard has no string!"
+ rawJson = readFromClipboard
+ }
+
+ val root: Any = if (term.startsWith("config")) {
+ SkyHanniMod.feature
+ } else if (term.startsWith("playerSpecific")) {
+ ProfileStorageData.playerSpecific ?: return "§cplayerSpecific is null!"
+ } else if (term.startsWith("profileSpecific")) {
+ ProfileStorageData.profileSpecific ?: return "§cprofileSpecific is null!"
+ } else return "§cUnknown config location!"
+
+ val element = ConfigManager.gson.fromJson(rawJson, JsonElement::class.java)
+ val list = term.split(".").drop(1)
+ val shimmy = Shimmy.makeShimmy(root, list) ?: return "§cCould not change config element '$term', not found!"
+ return try {
+ shimmy.setJson(element)
+ "§eChanged config element $term."
+ } catch (e: Exception) {
+ CopyErrorCommand.logError(e, "Could not change config element '$term' to '$rawJson'")
+ "Could not change config element '$term' to '$rawJson'"
+ }
}
private fun createFilter(condition: Boolean, searchTerm: () -> String): Pair<(String) -> Boolean, String> {
@@ -96,7 +96,7 @@ object SkyHanniConfigSearchResetCommand {
} else Pair({ true }, "<all>")
}
- private fun startSearch(args: Array<String>) {
+ private fun startSearch(args: Array<String>): String {
val (configFilter, configSearchTerm) = createFilter(true) { args[1].lowercase() }
val (classFilter, classSearchTerm) = createFilter(args.size == 3) { args[2].lowercase() }
@@ -115,7 +115,7 @@ object SkyHanniConfigSearchResetCommand {
}
builder.append("```")
OSUtils.copyToClipboard(builder.toString())
- LorenzUtils.chat("§eCopied search result ($size) to clipboard.")
+ return "§eCopied search result ($size) to clipboard."
}
private fun findConfigElements(