aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/raeids/stratus/hook/ChatSearching.kt
blob: 57cfc932cce80824c0714f6728e4e323732c6b55 (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
package com.raeids.stratus.hook

import gg.essential.lib.caffeine.cache.Cache
import gg.essential.lib.caffeine.cache.Caffeine
import gg.essential.universal.wrappers.message.UTextComponent
import net.minecraft.client.Minecraft
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

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()}"
    )
}

val cache: Cache<String, List<ChatLine?>> = Caffeine.newBuilder().executor(POOL).maximumSize(5000).build()

fun filterMessages(text: String, list: List<ChatLine?>?): List<ChatLine?>? {
    if (list.isNullOrEmpty() || text.isBlank()) return list
    val cached = cache.getIfPresent(text)
    return cached ?: run {
        cache.put(text, list.filter {
            it != null && UTextComponent.stripFormatting(it.chatComponent.unformattedText).lowercase()
                .contains(text.lowercase())
        })
        cache.getIfPresent(text)
    }
}

fun setPrevText(text: String) {
    (Minecraft.getMinecraft().ingameGUI.chatGUI as GuiNewChatHook).prevText = text
}