aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-04-07 18:39:07 +1000
committerGitHub <noreply@github.com>2024-04-07 10:39:07 +0200
commit0850dc6a0195d7e52cea13cc5838e75d216f81b7 (patch)
treee59d05b9fff66dd3d50ff75d497cbc8448d2fdd2
parent31bfd8a1d3437f4bdbefa7bcba75143e37c4ac90 (diff)
downloadskyhanni-0850dc6a0195d7e52cea13cc5838e75d216f81b7.tar.gz
skyhanni-0850dc6a0195d7e52cea13cc5838e75d216f81b7.tar.bz2
skyhanni-0850dc6a0195d7e52cea13cc5838e75d216f81b7.zip
Backend: Change stuff around with chat messages (#1227)
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt26
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt32
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt4
-rw-r--r--src/main/java/at/hannibal2/skyhanni/events/LorenzChatEvent.kt2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt69
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt35
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt2
9 files changed, 78 insertions, 96 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
index 4059761c3..3a38db02a 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
@@ -236,7 +236,7 @@ class ConfigManager {
val jsonObject = gson.fromJson(bufferedReader.readText(), JsonObject::class.java)
val newJsonObject = ConfigUpdaterMigrator.fixConfig(jsonObject)
val run = { gson.fromJson(newJsonObject, defaultValue.javaClass) }
- if (LorenzUtils.isInDevEnviromen()) {
+ if (LorenzUtils.isInDevEnvironment()) {
try {
run()
} catch (e: Throwable) {
diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
index 1b8c2bdeb..1e1cc0da3 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
+++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
@@ -80,10 +80,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SoundUtils
import at.hannibal2.skyhanni.utils.TabListData
import at.hannibal2.skyhanni.utils.repopatterns.RepoPatternGui
-import net.minecraft.client.Minecraft
import net.minecraft.command.ICommandSender
-import net.minecraft.event.ClickEvent
-import net.minecraft.event.HoverEvent
import net.minecraft.util.BlockPos
import net.minecraft.util.ChatComponentText
import net.minecraftforge.client.ClientCommandHandler
@@ -522,30 +519,33 @@ object Commands {
} else {
title = "All SkyHanni commands"
}
- val base = ChatComponentText(" \n§7$title:\n")
+
+ val components = mutableListOf<ChatComponentText>()
+ components.add(ChatComponentText(" \n§7$title:\n"))
+
for (command in commands) {
if (!filter(command.name) && !filter(command.description)) continue
val category = command.category
val name = command.name
val color = category.color
- val text = ChatComponentText("$color/$name")
- text.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/$name")
val hoverText = buildList {
add("§e/$name")
- add(" §7${command.description}")
+ if (command.description.isNotEmpty()) {
+ add(" §7${command.description}")
+ }
add("")
add("$color${category.categoryName}")
add(" §7${category.description}")
}
- text.chatStyle.chatHoverEvent =
- HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText(hoverText.joinToString("\n")))
- base.appendSibling(text)
- base.appendSibling(ChatComponentText("§7, "))
+ val commandInfo = ChatUtils.createHoverableChat("$color/$name", hoverText, "/$name", false)
+
+ components.add(commandInfo)
+ components.add(ChatComponentText("§7, "))
}
- base.appendSibling(ChatComponentText("\n "))
- Minecraft.getMinecraft().thePlayer.addChatMessage(base)
+ components.add(ChatComponentText("\n "))
+ ChatUtils.multiComponentMessage(components)
}
@JvmStatic
diff --git a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt
index 8573a629f..40ef1399a 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt
@@ -16,7 +16,6 @@ import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.ChatLine
import net.minecraft.client.gui.GuiNewChat
-import net.minecraft.event.HoverEvent
import net.minecraft.network.play.client.C01PacketChatMessage
import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting
@@ -124,9 +123,7 @@ object ChatManager {
}
val key = IdentityCharacteristics(original)
val chatEvent = LorenzChatEvent(message, original)
- if (!isSoopyMessage(event.message)) {
- chatEvent.postAndCatch()
- }
+ chatEvent.postAndCatch()
val blockReason = chatEvent.blockedReason.uppercase()
if (blockReason != "") {
@@ -161,33 +158,6 @@ object ChatManager {
}
}
- private fun isSoopyMessage(message: IChatComponent): Boolean {
- for (sibling in message.siblings) {
- if (isSoopyMessage(sibling)) return true
- }
-
- val style = message.chatStyle ?: return false
- val hoverEvent = style.chatHoverEvent ?: return false
- if (hoverEvent.action != HoverEvent.Action.SHOW_TEXT) return false
- val text = hoverEvent.value?.formattedText ?: return false
-
- val lines = text.split("\n")
- if (lines.isEmpty()) return false
-
- val last = lines.last()
- if (last.startsWith("§f§lCOMMON")) return true
- if (last.startsWith("§a§lUNCOMMON")) return true
- if (last.startsWith("§9§lRARE")) return true
- if (last.startsWith("§5§lEPIC")) return true
- if (last.startsWith("§6§lLEGENDARY")) return true
- if (last.startsWith("§d§lMYTHIC")) return true
- if (last.startsWith("§c§lSPECIAL")) return true
-
- // TODO confirm this format is correct
- if (last.startsWith("§c§lVERY SPECIAL")) return true
- return false
- }
-
fun openChatFilterGUI(args: Array<String>) {
SkyHanniMod.screenToOpen = if (args.isEmpty()) {
ChatFilterGui(getRecentMessageHistory())
diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt
index 4a42001df..fc024a4c8 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt
@@ -7,7 +7,6 @@ import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
-import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
@@ -233,8 +232,7 @@ class RepoManager(private val configLocation: File) {
return
}
ChatUtils.chat("Repo has errors! Commit has: ${latestRepoCommit ?: "null"}", prefixColor = "§c")
-// if (successfulConstants.isNotEmpty()) ChatUtils.chat(
- if (successfulConstants.isNotEmpty()) LorenzUtils.chat(
+ if (successfulConstants.isNotEmpty()) ChatUtils.chat(
"Successful Constants §7(${successfulConstants.size}):",
prefixColor = "§a"
)
diff --git a/src/main/java/at/hannibal2/skyhanni/events/LorenzChatEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/LorenzChatEvent.kt
index c2c51cec6..9d03549ac 100644
--- a/src/main/java/at/hannibal2/skyhanni/events/LorenzChatEvent.kt
+++ b/src/main/java/at/hannibal2/skyhanni/events/LorenzChatEvent.kt
@@ -3,7 +3,7 @@ package at.hannibal2.skyhanni.events
import net.minecraft.util.IChatComponent
class LorenzChatEvent(
- var message: String,
+ val message: String,
var chatComponent: IChatComponent,
var blockedReason: String = "",
var chatLineId: Int = 0,
diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt
index ed210ef2b..a39c59274 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt
@@ -48,7 +48,7 @@ object WikiManager {
}
if (message == ("/wikithis")) {
val itemInHand = InventoryUtils.getItemInHand() ?: run {
- LorenzUtils.chat("§cYou must be holding an item to use this command!")
+ ChatUtils.chat("§cYou must be holding an item to use this command!")
return
}
wikiTheItem(itemInHand, config.autoOpenWiki)
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
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
index 9d65eca03..c45bcae0e 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
@@ -336,13 +336,13 @@ object LorenzUtils {
inline fun <reified T : Enum<T>> enumValueOf(name: String) =
enumValueOfOrNull<T>(name)
- ?: kotlin.error("Unknown enum constant for ${enumValues<T>().first().name.javaClass.simpleName}: '$name'")
+ ?: error("Unknown enum constant for ${enumValues<T>().first().name.javaClass.simpleName}: '$name'")
inline fun <reified T : Enum<T>> enumJoinToPattern(noinline transform: (T) -> CharSequence = { it.name }) =
enumValues<T>().joinToString("|", transform = transform)
// TODO move to val by lazy
- fun isInDevEnviromen() = Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean
+ fun isInDevEnvironment() = Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean
fun shutdownMinecraft(reason: String? = null) {
System.err.println("SkyHanni-${SkyHanniMod.version} forced the game to shutdown.")
@@ -352,11 +352,6 @@ object LorenzUtils {
FMLCommonHandler.instance().handleExit(-1)
}
- @Deprecated("moved", ReplaceWith("ChatUtils.sendCommandToServer(command)"))
- fun sendCommandToServer(command: String) {
- ChatUtils.sendCommandToServer(command)
- }
-
/**
* Get the group, otherwise, return null
* @param groupName The group name in the pattern
@@ -365,32 +360,6 @@ object LorenzUtils {
return runCatching { this.group(groupName) }.getOrNull()
}
- @Deprecated("moved", ReplaceWith("ChatUtils.debug(message)"))
- fun debug(message: String) = ChatUtils.debug(message)
-
- @Deprecated("moved", ReplaceWith("ChatUtils.userError(message)"))
- fun userError(message: String) = ChatUtils.userError(message)
-
- @Deprecated("moved", ReplaceWith("ChatUtils.chat(message, prefix, prefixColor)"))
- fun chat(message: String, prefix: Boolean = true, prefixColor: String = "§e") =
- ChatUtils.chat(message, prefix, prefixColor)
-
- @Deprecated("moved", ReplaceWith("ChatUtils.clickableChat(message, command, prefix, prefixColor)"))
- fun clickableChat(message: String, command: String, prefix: Boolean = true, prefixColor: String = "§e") =
- ChatUtils.clickableChat(message, command, prefix, prefixColor)
-
- @Deprecated("moved", ReplaceWith("ChatUtils.hoverableChat(message, hover, command, prefix, prefixColor)"))
- fun hoverableChat(
- message: String,
- hover: List<String>,
- command: String? = null,
- prefix: Boolean = true,
- prefixColor: String = "§e",
- ) = ChatUtils.hoverableChat(message, hover, command, prefix, prefixColor)
-
- @Deprecated("moved", ReplaceWith("ChatUtils.sendMessageToServer(message)"))
- fun sendMessageToServer(message: String) = ChatUtils.sendMessageToServer(message)
-
fun inAdvancedMiningIsland() =
IslandType.DWARVEN_MINES.isInIsland() || IslandType.CRYSTAL_HOLLOWS.isInIsland() || IslandType.MINESHAFT.isInIsland()
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt b/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt
index 15dbfbaf0..26452e58f 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternManager.kt
@@ -58,7 +58,7 @@ object RepoPatternManager {
}
}
- val localLoading: Boolean get() = config.forceLocal.get() || LorenzUtils.isInDevEnviromen()
+ val localLoading: Boolean get() = config.forceLocal.get() || LorenzUtils.isInDevEnvironment()
/**
* Crash if in a development environment, or if inside a guarded event handler.