summaryrefslogtreecommitdiff
path: root/src/main/kotlin/ChatStore.kt
blob: 4cc6ee1ede26ecf12f1682f456b70007bb8b20fb (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package moe.nea.ultranotifier

import com.mojang.brigadier.builder.LiteralArgumentBuilder
import moe.nea.ultranotifier.commands.UltraCommandSource
import moe.nea.ultranotifier.commands.literalText
import moe.nea.ultranotifier.event.ChatGuiLineEvent
import moe.nea.ultranotifier.event.CommandRegistrationEvent
import moe.nea.ultranotifier.event.PacketChatLineEvent
import moe.nea.ultranotifier.event.SubscriptionTarget
import moe.nea.ultranotifier.event.UltraSubscribe
import moe.nea.ultranotifier.gui.MessageUi
import moe.nea.ultranotifier.gui.ScreenUtil
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 registerCommands(event: CommandRegistrationEvent) {
		event.dispatcher.register(LiteralArgumentBuilder.literal<UltraCommandSource?>("ultranotifier")
			                          .executes {
				                          it.source.sendFeedback(literalText("Opening screen"))
				                          ScreenUtil.openScreen = (MessageUi())
				                          0
			                          })
	}

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