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) --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 12 ++++++ .../skyhanni/test/command/TestChatCommand.kt | 49 ++++++++++++---------- .../java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 7 +++- 3 files changed, 46 insertions(+), 22 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 80dcc16f8..2c6dddc72 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -439,6 +439,7 @@ import at.hannibal2.skyhanni.test.TestExportTools import at.hannibal2.skyhanni.test.TestShowSlotNumber import at.hannibal2.skyhanni.test.WorldEdit import at.hannibal2.skyhanni.test.command.CopyNearbyParticlesCommand +import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.test.command.TrackSoundsCommand import at.hannibal2.skyhanni.test.hotswap.HotswapSupport import at.hannibal2.skyhanni.utils.ChatUtils @@ -457,6 +458,7 @@ import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiScreen import net.minecraftforge.common.MinecraftForge @@ -1006,5 +1008,15 @@ class SkyHanniMod { fun consoleLog(message: String) { logger.log(Level.INFO, message) } + + fun launchCoroutine(function: suspend () -> Unit) { + coroutineScope.launch { + try { + function() + } catch (ex: Exception) { + ErrorManager.logErrorWithData(ex, "Asynchronous exception caught") + } + } + } } } 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) } } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index b155f2b73..c2292845c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -10,6 +10,7 @@ import net.minecraft.client.Minecraft import net.minecraft.event.ClickEvent import net.minecraft.event.HoverEvent import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.LinkedList import java.util.Queue @@ -105,11 +106,15 @@ object ChatUtils { } } + fun chatComponent(message: IChatComponent) { + internalChat(message) + } + private fun internalChat(message: String): Boolean { return internalChat(ChatComponentText(message)) } - private fun internalChat(message: ChatComponentText): Boolean { + private fun internalChat(message: IChatComponent): Boolean { val formattedMessage = message.formattedText log.log(formattedMessage) -- cgit