Skip to content

Commit

Permalink
Read Player messages for chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Sep 7, 2024
1 parent f83d110 commit a59b61c
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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() }
}
Expand Down

0 comments on commit a59b61c

Please sign in to comment.