aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/org/polyfrost/chatting/chat/ChatTab.kt
blob: bd65f11af781e2a3b3b58e2bf80ed33facbd8b33 (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
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
package org.polyfrost.chatting.chat

import org.polyfrost.chatting.gui.components.TabButton
import com.google.gson.annotations.SerializedName
import net.minecraft.client.Minecraft
import net.minecraft.util.EnumChatFormatting
import net.minecraft.util.IChatComponent
import java.util.*

data class ChatTab(
    val enabled: Boolean,
    val name: String,
    val unformatted: Boolean,
    val lowercase: Boolean?,
    @SerializedName("starts") val startsWith: List<String>?,
    val contains: List<String>?,
    @SerializedName("ends") val endsWith: List<String>?,
    val equals: List<String>?,
    @SerializedName("regex") val uncompiledRegex: List<String>?,
    @SerializedName("ignore_starts") val ignoreStartsWith: List<String>?,
    @SerializedName("ignore_contains") val ignoreContains: List<String>?,
    @SerializedName("ignore_ends") val ignoreEndsWith: List<String>?,
    @SerializedName("ignore_equals") val ignoreEquals: List<String>?,
    @SerializedName("ignore_regex") val uncompiledIgnoreRegex: List<String>?,
    val color: Int?,
    @SerializedName("hovered_color") val hoveredColor: Int?,
    @SerializedName("selected_color") val selectedColor: Int?,
    val prefix: String?,
) {
    lateinit var button: TabButton
    lateinit var compiledRegex: ChatRegexes
    lateinit var compiledIgnoreRegex: ChatRegexes

    //Ugly hack to make GSON not make button / regex null
    fun initialize() {
        compiledRegex = ChatRegexes(uncompiledRegex)
        compiledIgnoreRegex = ChatRegexes(uncompiledIgnoreRegex)
        val width = Minecraft.getMinecraft().fontRendererObj.getStringWidth(name)
        button = TabButton(653452, run {
            val returnValue = x - 2
            x += 6 + width
            return@run returnValue
        }, width + 4, 12, this)
    }

    fun shouldRender(chatComponent: IChatComponent): Boolean {
        val message =
            (if (unformatted) EnumChatFormatting.getTextWithoutFormattingCodes(chatComponent.unformattedText) else chatComponent.formattedText).let {
                if (lowercase == true) it.lowercase(
                    Locale.ENGLISH
                ) else it
            }
        ignoreStartsWith?.forEach {
            if (message.startsWith(it)) {
                return false
            }
        }
        ignoreEquals?.forEach {
            if (message == it) {
                return false
            }
        }
        ignoreEndsWith?.forEach {
            if (message.endsWith(it)) {
                return false
            }
        }
        ignoreContains?.forEach {
            if (message.contains(it)) {
                return false
            }
        }
        compiledIgnoreRegex.compiledRegexList.forEach {
            if (it.matches(message)) {
                return false
            }
        }
        if (startsWith.isNullOrEmpty() && equals.isNullOrEmpty() && endsWith.isNullOrEmpty() && contains.isNullOrEmpty() && uncompiledRegex.isNullOrEmpty()) {
            return true
        }
        equals?.forEach {
            if (message == it) {
                return true
            }
        }
        startsWith?.forEach {
            if (message.startsWith(it)) {
                return true
            }
        }
        endsWith?.forEach {
            if (message.endsWith(it)) {
                return true
            }
        }
        contains?.forEach {
            if (message.contains(it)) {
                return true
            }
        }
        compiledRegex.compiledRegexList.forEach {
            if (it.matches(message)) {
                return true
            }
        }
        return false
    }

    companion object {
        private var x = 4
    }
}