From a59b61cabf70a9303ebb4212a377f453b7db12df Mon Sep 17 00:00:00 2001 From: Gabber235 Date: Sat, 7 Sep 2024 08:14:20 +0200 Subject: [PATCH] Read Player messages for chat history --- .../paper/interaction/ChatHistoryHandler.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/interaction/ChatHistoryHandler.kt b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/interaction/ChatHistoryHandler.kt index b82cb35636..6a4b42d096 100644 --- a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/interaction/ChatHistoryHandler.kt +++ b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/interaction/ChatHistoryHandler.kt @@ -4,13 +4,16 @@ import com.github.retrooper.packetevents.PacketEvents import com.github.retrooper.packetevents.event.PacketListenerAbstract import com.github.retrooper.packetevents.event.PacketListenerPriority import com.github.retrooper.packetevents.event.PacketSendEvent +import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage_v1_19_3 import com.github.retrooper.packetevents.protocol.packettype.PacketType +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSystemChatMessage import com.github.shynixn.mccoroutine.bukkit.registerSuspendingEvents import lirand.api.extensions.server.server import com.typewritermc.engine.paper.plugin import com.typewritermc.engine.paper.snippets.snippet import com.typewritermc.engine.paper.utils.plainText +import io.papermc.paper.event.player.AsyncChatEvent import net.kyori.adventure.text.Component import net.kyori.adventure.text.TextComponent import net.kyori.adventure.text.format.TextColor @@ -40,12 +43,7 @@ class ChatHistoryHandler : // When the serer sends a message to the player override fun onPacketSend(event: PacketSendEvent?) { if (event == null) return - if (event.packetType != PacketType.Play.Server.SYSTEM_CHAT_MESSAGE) return - - val packet = WrapperPlayServerSystemChatMessage(event) - if (packet.isOverlay) return - - val component = packet.message + val component = findMessage(event) ?: return if (component is TextComponent && component.content() == "no-index") return val history = getHistory(event.user.uuid) history.addMessage(component) @@ -55,6 +53,22 @@ class ChatHistoryHandler : } } + private fun findMessage(event: PacketSendEvent): Component? { + return when (event.packetType) { + PacketType.Play.Server.CHAT_MESSAGE -> { + val packet = WrapperPlayServerChatMessage(event) + val message = packet.message as? ChatMessage_v1_19_3 ?: return packet.message.chatContent + message.unsignedChatContent.orElseGet { message.chatContent } + } + PacketType.Play.Server.SYSTEM_CHAT_MESSAGE -> { + val packet = WrapperPlayServerSystemChatMessage(event) + if (packet.isOverlay) return null + packet.message + } + else -> null + } + } + fun getHistory(pid: UUID): ChatHistory { return histories.getOrPut(pid) { ChatHistory() } }