diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-04-10 09:00:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-10 09:00:22 +0200 |
commit | 3397cc4474e3165865707182a5dcacf5bb4e68bd (patch) | |
tree | c714eab2aae2453a01f546cb4811c87dcd18a7e7 /src/main/java/at/hannibal2/skyhanni/utils | |
parent | 8c86c32807dc697218d71bdc9eef7aef68ba90e7 (diff) | |
download | skyhanni-3397cc4474e3165865707182a5dcacf5bb4e68bd.tar.gz skyhanni-3397cc4474e3165865707182a5dcacf5bb4e68bd.tar.bz2 skyhanni-3397cc4474e3165865707182a5dcacf5bb4e68bd.zip |
Backend: Chat and Open Config (#1396)
Co-authored-by: Empa <42304516+ItsEmpa@users.noreply.github.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Co-authored-by: Cal <cwolfson58@gmail.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 17 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt | 37 |
2 files changed, 51 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index d2dd5c9b0..eaae104ac 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.data.ChatClickActionManager import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.MessageSendToServerEvent +import at.hannibal2.skyhanni.utils.ConfigUtils.jumpToEditor import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraft.client.Minecraft import net.minecraft.event.ClickEvent @@ -12,6 +13,7 @@ import net.minecraft.util.ChatComponentText import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.LinkedList import java.util.Queue +import kotlin.reflect.KMutableProperty0 import kotlin.time.Duration.Companion.milliseconds import kotlin.time.times @@ -194,7 +196,7 @@ object ChatUtils { message: String, hover: List<String>, command: String? = null, - runCommand: Boolean = true + runCommand: Boolean = true, ): ChatComponentText { val text = ChatComponentText(message) text.chatStyle.chatHoverEvent = @@ -225,7 +227,7 @@ object ChatUtils { hover: String = "§eOpen $url", autoOpen: Boolean = false, prefix: Boolean = true, - prefixColor: String = "§e" + prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" val text = ChatComponentText(msgPrefix + message) @@ -246,7 +248,7 @@ object ChatUtils { fun multiComponentMessage( components: List<ChatComponentText>, prefix: Boolean = true, - prefixColor: String = "§e" + prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" val baseMessage = ChatComponentText(msgPrefix) @@ -297,4 +299,13 @@ object ChatUtils { fun MessageSendToServerEvent.eventWithNewMessage(message: String) = MessageSendToServerEvent(message, message.split(" "), this.originatingModContainer) + + fun chatAndOpenConfig(message: String, property: KMutableProperty0<*>) { + clickableChat( + message, + onClick = { + property.jumpToEditor() + } + ) + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt index 55773a9c7..b6f096371 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt @@ -1,9 +1,18 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigGuiManager import at.hannibal2.skyhanni.config.HasLegacyId +import at.hannibal2.skyhanni.events.LorenzEvent +import at.hannibal2.skyhanni.test.command.ErrorManager import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonPrimitive +import io.github.notenoughupdates.moulconfig.gui.GuiScreenElementWrapper +import io.github.notenoughupdates.moulconfig.gui.MoulConfigEditor +import io.github.notenoughupdates.moulconfig.processor.ProcessedOption +import kotlin.reflect.KMutableProperty0 +import kotlin.reflect.jvm.javaField object ConfigUtils { @@ -55,4 +64,32 @@ object ConfigUtils { require(element is JsonPrimitive) { "Expected a JsonPrimitive but got ${element.javaClass.simpleName}" } return JsonPrimitive(getEnumConstantFromLegacyId(element.asInt, enumClass)?.name) } + + fun KMutableProperty0<*>.tryFindEditor(editor: MoulConfigEditor<*>): ProcessedOption? { + return editor.processedConfig.getOptionFromField(this.javaField ?: return null) + } + + fun KMutableProperty0<*>.jumpToEditor() { + if (tryJumpToEditor(ConfigGuiManager.getEditorInstance())) return + + // TODO create utils function "crashIfInDevEnv" + if (LorenzEvent.isInGuardedEventHandler) { + throw Error("can not jump to editor $name") + } + ErrorManager.logErrorStateWithData( + "Can not open the config", + "error while trying to jump to an editor element", + "this.name" to this.name, + "this.toString()" to this.toString(), + "this" to this, + ) + } + + private fun KMutableProperty0<*>.tryJumpToEditor(editor: MoulConfigEditor<*>): Boolean { + val option = tryFindEditor(editor) ?: return false + editor.search("") + if (!editor.goToOption(option)) return false + SkyHanniMod.screenToOpen = GuiScreenElementWrapper(editor) + return true + } } |