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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
package moe.nea.ultranotifier.datamodel
import moe.nea.ultranotifier.UltraNotifier
import moe.nea.ultranotifier.event.SubscriptionTarget
import moe.nea.ultranotifier.event.TickEvent
import moe.nea.ultranotifier.event.UltraSubscribe
import moe.nea.ultranotifier.event.VisibleChatMessageAddedEvent
import moe.nea.ultranotifier.util.GsonUtil
import moe.nea.ultranotifier.util.duplicatesBy
import moe.nea.ultranotifier.util.minecrat.MC
import moe.nea.ultranotifier.util.minecrat.category
import moe.nea.ultranotifier.util.minecrat.getFormattedTextCompat
import moe.nea.ultranotifier.util.minecrat.removeFormattingCodes
import net.minecraft.text.Text
import util.KSerializable
import java.util.function.Predicate
import java.util.regex.Pattern
data class ChatTypeId(
val id: String
)
@KSerializable
data class ChatType(
val name: String,
val patterns: List<ChatPattern>,
)
data class ChatPattern(
val text: String
) {
val pattern = Pattern.compile(text)
val predicate: Predicate<String> =
//#if JAVA > 11
pattern.asMatchPredicate()
//#else
//$$ Predicate { it: String -> pattern.matcher(it).matches() }
//#endif
}
data class CategoryId(val id: String)
@KSerializable
data class ChatCategory(
val id: CategoryId,
val label: String,
val chatTypes: Set<ChatTypeId>,
)
data class ChatUniverse(
val name: String,
val types: Map<ChatTypeId, ChatType>,
val categories: List<ChatCategory>,
) {
fun categorize(
text: String
): CategorizedChatLine {
val types = this.types
.asSequence()
.filter {
it.value.patterns.any {
it.predicate.test(text)
}
}
.map {
it.key
}
.toSet()
return CategorizedChatLine(
text, types
)
}
}
data class CategorizedChatLine(
val text: String,
val types: Set<ChatTypeId>,
// val categories: Set<ChatCategory>,
)
@KSerializable
data class UniverseMeta(
// TODO: implement the ip filter
val ipFilter: List<ChatPattern> = listOf(),
val name: String,
)
interface HasCategorizedChatLine {
val categorizedChatLine_ultraNotifier: CategorizedChatLine
}
data class UniverseId(
val id: String
)
private fun loadAllUniverses(): Map<UniverseId, ChatUniverse> = buildMap {
for (file in UltraNotifier.configFolder
.resolve("universes/")
.listFiles() ?: emptyArray()) {
runCatching {
val meta = GsonUtil.read<UniverseMeta>(file.resolve("meta.json"))
val types = GsonUtil.read<Map<ChatTypeId, ChatType>>(file.resolve("chattypes.json"))
val categories = GsonUtil.read<List<ChatCategory>>(file.resolve("categories.json"))
// Validate categories linking properly
for (category in categories) {
for (chatType in category.chatTypes) {
if (chatType in types.keys) {
UltraNotifier.logger.warn("Missing definition for $chatType required by ${category.id} in $file")
}
}
}
for (category in categories.asSequence().duplicatesBy { it.id }) {
UltraNotifier.logger.warn("Found duplicate category ${category.id} in $file")
}
put(
UniverseId(file.name),
ChatUniverse(
meta.name,
types,
categories,
))
}.getOrElse {
UltraNotifier.logger.warn("Could not load universe at $file", it)
}
}
}
object ChatCategoryArbiter : SubscriptionTarget {
val specialAll = CategoryId("special-all")
var allUniverses = loadAllUniverses()
var activeUniverse: ChatUniverse? = allUniverses.values.single()
private val allCategoryList = listOf(
ChatCategory(specialAll, "All", setOf())
)
val categories // TODO: memoize
get() = (activeUniverse?.categories ?: listOf()) + allCategoryList
var selectedCategoryId = specialAll
set(value) {
field = value
selectedCategory = findCategory(value)
}
private var lastSelectedId = selectedCategoryId
var selectedCategory: ChatCategory = findCategory(selectedCategoryId)
private set
@UltraSubscribe
fun onTick(event: TickEvent) {
if (lastSelectedId != selectedCategoryId) {
MC.chatHud.reset()
lastSelectedId = selectedCategoryId
}
}
@UltraSubscribe
fun onVisibleChatMessage(event: VisibleChatMessageAddedEvent) {
val cl = event.chatLine.category
if (selectedCategory.id == specialAll)
return
if (cl.types.none { it in selectedCategory.chatTypes })
event.cancel()
}
fun findCategory(id: CategoryId) = categories.find { it.id == id }!!
fun categorize(content: Text): CategorizedChatLine {
val stringContent = content.getFormattedTextCompat().removeFormattingCodes()
return activeUniverse?.categorize(stringContent) ?: CategorizedChatLine(stringContent, setOf())
}
}
|