diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 69 |
1 files changed, 57 insertions, 12 deletions
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<String>, + 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<ChatComponentText>, + 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<String> = LinkedList() private val messageDelay = 300.milliseconds |