diff options
Diffstat (limited to 'src/main/kotlin')
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 |