From ef7fcbfdf3bf954275e1d3b0c08c13214bff0f01 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 3 May 2024 17:30:54 +0200 Subject: Add shtestmessage for complex messages (#1663) --- .../skyhanni/test/command/TestChatCommand.kt | 49 ++++++++++++---------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/test/command') diff --git a/src/main/java/at/hannibal2/skyhanni/test/command/TestChatCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/command/TestChatCommand.kt index 0e719c4e1..73645b6a8 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/command/TestChatCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/command/TestChatCommand.kt @@ -5,8 +5,8 @@ import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils -import kotlinx.coroutines.launch import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent object TestChatCommand { @@ -16,36 +16,43 @@ object TestChatCommand { return } - val last = args.last() - if (last == "-clipboard") { - SkyHanniMod.coroutineScope.launch { - OSUtils.readFromClipboard()?.let { - test(it) - } ?: run { - ChatUtils.userError("Clipboard does not contain a string!") - } - } - return + SkyHanniMod.launchCoroutine { + val mutArgs = args.toMutableList() + val isComplex = mutArgs.remove("-complex") + val isClipboard = mutArgs.remove("-clipboard") + val isHidden = mutArgs.remove("-s") + val text = if (isClipboard) { + OSUtils.readFromClipboard() + ?: return@launchCoroutine ChatUtils.userError("Clipboard does not contain a string!") + } else mutArgs.joinToString(" ") + val component = + if (isComplex) + try { + IChatComponent.Serializer.jsonToComponent(text) + } catch (ex: Exception) { + ChatUtils.userError("Please provide a valid JSON chat component (either in the command or via -clipboard)") + return@launchCoroutine + } + else ChatComponentText(text.replace("&", "§")) + if (!isHidden) ChatUtils.chat("Testing message: §7${component.formattedText}", prefixColor = "§a") + test(component) } - val hidden = last == "-s" - var rawMessage = args.toList().joinToString(" ") - if (!hidden) ChatUtils.chat("Testing message: §7$rawMessage", prefixColor = "§a") - if (hidden) rawMessage = rawMessage.replace(" -s", "") - test(rawMessage.replace("&", "§")) } - private fun test(message: String) { - val event = LorenzChatEvent(message, ChatComponentText(message)) + private fun test(componentText: IChatComponent) { + val event = LorenzChatEvent(LorenzUtils.stripVanillaMessage(componentText.formattedText), componentText) event.postAndCatch() if (event.blockedReason != "") { ChatUtils.chat("§cChat blocked: ${event.blockedReason}") } else { - val finalMessage = event.chatComponent.formattedText - if (LorenzUtils.stripVanillaMessage(finalMessage) != LorenzUtils.stripVanillaMessage(message)) { + val finalMessage = event.chatComponent + if (LorenzUtils.stripVanillaMessage(finalMessage.formattedText) != LorenzUtils.stripVanillaMessage( + componentText.formattedText) + ) { ChatUtils.chat("§eChat modified!") } - ChatUtils.chat(finalMessage, false) + ChatUtils.chatComponent(finalMessage) } } } -- cgit