aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt
diff options
context:
space:
mode:
authorWyvest <wyvestbusiness@gmail.com>2023-11-22 08:18:19 +0900
committerWyvest <wyvestbusiness@gmail.com>2023-11-22 08:18:19 +0900
commit8b373f577d9c6dde26357ef3fc86691f1efef9b4 (patch)
treea5328e995d8f4df21a9fe94ac8e384be08833c70 /src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt
parent64230799777473246b5f98efbc596206c5bbf42d (diff)
downloadChatting-8b373f577d9c6dde26357ef3fc86691f1efef9b4.tar.gz
Chatting-8b373f577d9c6dde26357ef3fc86691f1efef9b4.tar.bz2
Chatting-8b373f577d9c6dde26357ef3fc86691f1efef9b4.zip
update PGT and relocate to org.polyfrost
Diffstat (limited to 'src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt')
-rw-r--r--src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt b/src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt
new file mode 100644
index 0000000..d20a358
--- /dev/null
+++ b/src/main/kotlin/org/polyfrost/chatting/chat/ChatSearchingManager.kt
@@ -0,0 +1,42 @@
+package org.polyfrost.chatting.chat
+
+import cc.polyfrost.oneconfig.libs.caffeine.cache.Cache
+import cc.polyfrost.oneconfig.libs.caffeine.cache.Caffeine
+import cc.polyfrost.oneconfig.libs.universal.wrappers.message.UTextComponent
+import net.minecraft.client.gui.ChatLine
+import java.util.concurrent.LinkedBlockingQueue
+import java.util.concurrent.ThreadPoolExecutor
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicInteger
+
+object ChatSearchingManager {
+ private var counter: AtomicInteger = AtomicInteger(0)
+ private var POOL: ThreadPoolExecutor = ThreadPoolExecutor(
+ 50, 50,
+ 0L, TimeUnit.SECONDS,
+ LinkedBlockingQueue()
+ ) { r ->
+ Thread(
+ r,
+ "Chat Filter Cache Thread ${counter.incrementAndGet()}"
+ )
+ }
+
+ @JvmStatic
+ val cache: Cache<String, List<ChatLine>> = Caffeine.newBuilder().executor(POOL).maximumSize(5000).build()
+
+ var lastSearch = ""
+
+ @JvmStatic
+ fun filterMessages(text: String, list: List<ChatLine>): List<ChatLine>? {
+ if (text.isBlank()) return list
+ val cached = cache.getIfPresent(text)
+ return cached ?: run {
+ cache.put(text, list.filter {
+ UTextComponent.stripFormatting(it.chatComponent.unformattedText).lowercase()
+ .contains(text.lowercase())
+ })
+ cache.getIfPresent(text)
+ }
+ }
+} \ No newline at end of file