From 0850dc6a0195d7e52cea13cc5838e75d216f81b7 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:39:07 +1000 Subject: Backend: Change stuff around with chat messages (#1227) --- .../java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 69 ++++++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index 180eaf5a2..3a88fd51f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -57,12 +57,11 @@ object ChatUtils { * This should be used for errors that are not caused by the user. * * Why deprecate this? Even if this message is descriptive for the user and the developer, - * we don't want inconsitencies in errors, and we would need to search + * we don't want inconsistencies in errors, and we would need to search * for the code line where this error gets printed any way. * so it's better to use the stack trace still. * * @param message The message to be sent - * @param prefix Whether to prefix the message with the error prefix, default true * * @see ERROR_PREFIX */ @@ -92,20 +91,26 @@ object ChatUtils { } private fun internalChat(message: String): Boolean { - log.log(message) + return internalChat(ChatComponentText(message)) + } + + private fun internalChat(message: ChatComponentText): Boolean { + val formattedMessage = message.formattedText + log.log(formattedMessage) + val minecraft = Minecraft.getMinecraft() if (minecraft == null) { - LorenzUtils.consoleLog(message.removeColor()) + LorenzUtils.consoleLog(formattedMessage.removeColor()) return false } val thePlayer = minecraft.thePlayer if (thePlayer == null) { - LorenzUtils.consoleLog(message.removeColor()) + LorenzUtils.consoleLog(formattedMessage.removeColor()) return false } - thePlayer.addChatMessage(ChatComponentText(message)) + thePlayer.addChatMessage(message) return true } @@ -120,12 +125,19 @@ object ChatUtils { */ fun clickableChat(message: String, command: String, prefix: Boolean = true, prefixColor: String = "§e") { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - val text = ChatComponentText(msgPrefix + message) + val fullMessage = msgPrefix + message + + internalChat(createClickableChat(fullMessage, command)) + } + + fun createClickableChat(message: String, command: String): ChatComponentText { + val text = ChatComponentText(message) val fullCommand = "/" + command.removePrefix("/") text.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.RUN_COMMAND, fullCommand) text.chatStyle.chatHoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText("§eExecute $fullCommand")) - Minecraft.getMinecraft().thePlayer.addChatMessage(text) + + return text } /** @@ -160,15 +172,27 @@ object ChatUtils { prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - val text = ChatComponentText(msgPrefix + message) + val fullMessage = msgPrefix + message + + internalChat(createHoverableChat(fullMessage, hover, command)) + } + + fun createHoverableChat( + message: String, + hover: List, + command: String? = null, + runCommand: Boolean = true + ): ChatComponentText { + val text = ChatComponentText(message) text.chatStyle.chatHoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText(hover.joinToString("\n"))) command?.let { - text.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.RUN_COMMAND, "/${it.removePrefix("/")}") + val eventType = if (runCommand) ClickEvent.Action.RUN_COMMAND else ClickEvent.Action.SUGGEST_COMMAND + text.chatStyle.chatClickEvent = ClickEvent(eventType, "/${it.removePrefix("/")}") } - Minecraft.getMinecraft().thePlayer.addChatMessage(text) + return text } /** @@ -194,10 +218,31 @@ object ChatUtils { val text = ChatComponentText(msgPrefix + message) text.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.OPEN_URL, url) text.chatStyle.chatHoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText("$prefixColor$hover")) - Minecraft.getMinecraft().thePlayer.addChatMessage(text) + internalChat(text) if (autoOpen) OSUtils.openBrowser(url) } + /** + * Sends a message to the user that combines many message components e.g. clickable, hoverable and regular text + * @param components The list of components to be joined together to form the final message + * @param prefix Whether to prefix the message with the chat prefix, default true + * @param prefixColor Color that the prefix should be, default yellow (§e) + * + * @see CHAT_PREFIX + */ + fun multiComponentMessage( + components: List, + prefix: Boolean = true, + prefixColor: String = "§e" + ) { + val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" + val baseMessage = ChatComponentText(msgPrefix) + + for (component in components) baseMessage.appendSibling(component) + + internalChat(baseMessage) + } + private var lastMessageSent = SimpleTimeMark.farPast() private val sendQueue: Queue = LinkedList() private val messageDelay = 300.milliseconds -- cgit