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 | 105 |
1 files changed, 25 insertions, 80 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index c2292845c..d1dce7fe3 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -1,14 +1,18 @@ package at.hannibal2.skyhanni.utils 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 at.hannibal2.skyhanni.utils.chat.Text +import at.hannibal2.skyhanni.utils.chat.Text.asComponent +import at.hannibal2.skyhanni.utils.chat.Text.command +import at.hannibal2.skyhanni.utils.chat.Text.hover +import at.hannibal2.skyhanni.utils.chat.Text.onClick +import at.hannibal2.skyhanni.utils.chat.Text.prefix +import at.hannibal2.skyhanni.utils.chat.Text.url 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 @@ -56,19 +60,6 @@ object ChatUtils { } /** - * Sends a message to the user that they did something incorrectly. - * Runs a command when clicked to fix the issue. - * - * @param message The message to be sent - * @param command The command to be executed when the message is clicked - * - * @see USER_ERROR_PREFIX - */ - fun clickableUserError(message: String, command: String) { - internalChat(createClickableChat(USER_ERROR_PREFIX + message, command)) - } - - /** * Sends a message to the user that an error occurred caused by something in the code. * This should be used for errors that are not caused by the user. * @@ -106,15 +97,11 @@ object ChatUtils { } } - fun chatComponent(message: IChatComponent) { - internalChat(message) - } - private fun internalChat(message: String): Boolean { - return internalChat(ChatComponentText(message)) + return chat(ChatComponentText(message)) } - private fun internalChat(message: IChatComponent): Boolean { + fun chat(message: IChatComponent): Boolean { val formattedMessage = message.formattedText log.log(formattedMessage) @@ -135,34 +122,6 @@ object ChatUtils { } /** - * Sends a message to the user that they can click and run a command - * @param message The message to be sent - * @param command The command to be executed when the message is clicked - * @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 - */ - //TODO rename to runHypixelCommand - @Deprecated("Use clickableChat with onClick or use HypixelCommands", ReplaceWith("")) - fun clickableChat(message: String, command: String, prefix: Boolean = true, prefixColor: String = "§e") { - val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - val fullMessage = msgPrefix + message - - internalChat(createClickableChat(fullMessage, command)) - } - - private 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("§eClick here!")) - - return text - } - - /** * Sends a message to the user that they can click and run an action * @param message The message to be sent * @param onClick The runnable to be executed when the message is clicked @@ -178,9 +137,13 @@ object ChatUtils { expireAt: SimpleTimeMark = SimpleTimeMark.farFuture(), prefix: Boolean = true, prefixColor: String = "§e", + oneTimeClick: Boolean = false, ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - ChatClickActionManager.oneTimeClick(msgPrefix + message, onClick, expireAt) + chat(Text.text(msgPrefix + message) { + this.onClick(expireAt, oneTimeClick, onClick) + this.hover = "§eClick here!".asComponent() + }) } /** @@ -201,27 +164,13 @@ object ChatUtils { prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - 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 { - val eventType = if (runCommand) ClickEvent.Action.RUN_COMMAND else ClickEvent.Action.SUGGEST_COMMAND - text.chatStyle.chatClickEvent = ClickEvent(eventType, "/${it.removePrefix("/")}") - } - - return text + chat(Text.text(msgPrefix + message) { + this.hover = Text.multiline(hover) + if (command != null) { + this.command = command + } + }) } /** @@ -244,10 +193,10 @@ object ChatUtils { prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - 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")) - internalChat(text) + chat(Text.text(msgPrefix + message) { + this.url = url + this.hover = "$prefixColor$hover".asComponent() + }) if (autoOpen) OSUtils.openBrowser(url) } @@ -265,11 +214,7 @@ object ChatUtils { prefixColor: String = "§e", ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - val baseMessage = ChatComponentText(msgPrefix) - - for (component in components) baseMessage.appendSibling(component) - - internalChat(baseMessage) + chat(Text.join(components).prefix(msgPrefix)) } private var lastMessageSent = SimpleTimeMark.farPast() |