aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/org/polyfrost/chatting
diff options
context:
space:
mode:
authorWyvest <wyvestbusiness@gmail.com>2023-11-23 19:07:22 +0900
committerWyvest <wyvestbusiness@gmail.com>2023-11-23 19:07:22 +0900
commit50dec3baa84ea72f9a711b936aabacf53aa04e05 (patch)
tree1f2840895ebf6934e2c1e9d78cfa3d963910b061 /src/main/kotlin/org/polyfrost/chatting
parent97a700fe1af0360b6deed3c6b0b0a3839d5a690d (diff)
downloadChatting-50dec3baa84ea72f9a711b936aabacf53aa04e05.tar.gz
Chatting-50dec3baa84ea72f9a711b936aabacf53aa04e05.tar.bz2
Chatting-50dec3baa84ea72f9a711b936aabacf53aa04e05.zip
Various chat head bug fixes
- Fix chat heads making chat lines go over chat background - Fix chat heads making tooltip hovers not work properly
Diffstat (limited to 'src/main/kotlin/org/polyfrost/chatting')
-rw-r--r--src/main/kotlin/org/polyfrost/chatting/Chatting.kt2
-rw-r--r--src/main/kotlin/org/polyfrost/chatting/utils/ChatHeadHooks.kt74
-rw-r--r--src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt2
3 files changed, 76 insertions, 2 deletions
diff --git a/src/main/kotlin/org/polyfrost/chatting/Chatting.kt b/src/main/kotlin/org/polyfrost/chatting/Chatting.kt
index 0e8745c..cf439ce 100644
--- a/src/main/kotlin/org/polyfrost/chatting/Chatting.kt
+++ b/src/main/kotlin/org/polyfrost/chatting/Chatting.kt
@@ -245,7 +245,7 @@ object Chatting {
}
val fr: FontRenderer = ModCompatHooks.fontRenderer
- val width = messages.maxOf { fr.getStringWidth(it.value) + (if (ChattingConfig.showChatHeads && ((it.key as ChatLineHook).hasDetected() || ChattingConfig.offsetNonPlayerMessages)) 10 else 0) } + 4
+ val width = messages.maxOf { fr.getStringWidth(it.value) + (if (ChattingConfig.showChatHeads && ((it.key as ChatLineHook).isDetected || ChattingConfig.offsetNonPlayerMessages)) 10 else 0) } + 4
val fb: Framebuffer = createBindFramebuffer(width * 2, (messages.size * 9) * 2)
val file = File(Minecraft.getMinecraft().mcDataDir, "screenshots/chat/" + fileFormatter.format(Date()))
diff --git a/src/main/kotlin/org/polyfrost/chatting/utils/ChatHeadHooks.kt b/src/main/kotlin/org/polyfrost/chatting/utils/ChatHeadHooks.kt
new file mode 100644
index 0000000..aa777c1
--- /dev/null
+++ b/src/main/kotlin/org/polyfrost/chatting/utils/ChatHeadHooks.kt
@@ -0,0 +1,74 @@
+package org.polyfrost.chatting.utils
+
+import net.minecraft.client.Minecraft
+import net.minecraft.client.gui.ChatLine
+import net.minecraft.client.network.NetHandlerPlayClient
+import net.minecraft.client.network.NetworkPlayerInfo
+import org.polyfrost.chatting.config.ChattingConfig.hideChatHeadOnConsecutiveMessages
+import org.polyfrost.chatting.hook.ChatLineHook
+
+object ChatHeadHooks {
+ private var lastPlayerInfo: NetworkPlayerInfo? = null
+ fun detect(formattedText: String, chatLine: ChatLine?): Boolean {
+ if (chatLine !is ChatLineHook) {
+ return false
+ }
+ val netHandler = Minecraft.getMinecraft().netHandler ?: return false
+ val nicknameCache: MutableMap<String, NetworkPlayerInfo> = HashMap()
+ var detected = false
+ try {
+ formattedText.split("(ยง.)|\\W".toRegex()).dropLastWhile { it.isEmpty() }
+ .forEach { word ->
+ if (word.isNotEmpty()) {
+ var maybePlayerInfo = netHandler.getPlayerInfo(word)
+ if (maybePlayerInfo == null) {
+ maybePlayerInfo = getPlayerFromNickname(word, netHandler, nicknameCache)
+ }
+ if (maybePlayerInfo != null) {
+ detected = true
+ chatLine.run {
+ playerInfo = maybePlayerInfo
+ detectedPlayerInfo = playerInfo
+ isDetected = true
+ if (playerInfo == lastPlayerInfo) {
+ isFirstDetection = false
+ if (hideChatHeadOnConsecutiveMessages) {
+ playerInfo = null
+ }
+ } else {
+ lastPlayerInfo = playerInfo
+ }
+ return@forEach
+ }
+ }
+ }
+ }
+ } catch (ignored: Exception) {
+ }
+ return detected
+ }
+
+ private fun getPlayerFromNickname(
+ word: String,
+ connection: NetHandlerPlayClient,
+ nicknameCache: MutableMap<String, NetworkPlayerInfo>
+ ): NetworkPlayerInfo? {
+ if (nicknameCache.isEmpty()) {
+ for (p in connection.playerInfoMap) {
+ val displayName = p.displayName
+ if (displayName != null) {
+ val nickname = displayName.unformattedTextForChat
+ if (word == nickname) {
+ nicknameCache.clear()
+ return p
+ }
+ nicknameCache[nickname] = p
+ }
+ }
+ } else {
+ // use prepared cache
+ return nicknameCache[word]
+ }
+ return null
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt b/src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt
index b6198fa..591461d 100644
--- a/src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt
+++ b/src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt
@@ -58,7 +58,7 @@ object ModCompatHooks {
var actualX = x
if (showChatHeads && !screenshot) {
val hook = chatLine as ChatLineHook
- if (hook.hasDetected() || offsetNonPlayerMessages) {
+ if (hook.isDetected || offsetNonPlayerMessages) {
actualX += 10f
}
val networkPlayerInfo = hook.playerInfo