summaryrefslogtreecommitdiff
path: root/src/main/kotlin/ChatStore.kt
blob: 2d9ff30de6b27faff38980d39c8519cb6789d65d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package moe.nea.ultranotifier

import moe.nea.ultranotifier.event.ChatGuiLineEvent
import moe.nea.ultranotifier.event.PacketChatLineEvent
import moe.nea.ultranotifier.event.SubscriptionTarget
import moe.nea.ultranotifier.event.UltraSubscribe
import moe.nea.ultranotifier.util.IdentityCharacteristics
import net.minecraft.text.Text

object ChatStore : SubscriptionTarget {

	data class ChatLine(
		val text: Text,
		var fromPacket: Boolean = false,
		var isDisplayed: Boolean = false,
	)

	val allLines = object : LinkedHashMap<IdentityCharacteristics<Text>, ChatLine>() {
		override fun removeEldestEntry(eldest: MutableMap.MutableEntry<IdentityCharacteristics<Text>, ChatLine>?): Boolean {
			return size > 500 // TODO: config
		}
	}

	fun insertChatLine(text: Text): ChatLine {
		return allLines.getOrPut(IdentityCharacteristics(text)) { ChatLine(text) }
	}

	@UltraSubscribe
	fun onMessageDisplayed(event: ChatGuiLineEvent) {
		insertChatLine(event.component).isDisplayed = true
	}

	@UltraSubscribe
	fun onMessageReceived(event: PacketChatLineEvent) {
		insertChatLine(event.component).fromPacket = true
	}
}