aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/dulkirfabric/features/chat/BridgeBotFormatter.kt
blob: e584431e3eab932255f9fbae230c5b49848df545 (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
package com.dulkirfabric.features.chat

import com.dulkirfabric.config.DulkirConfig
import com.dulkirfabric.events.chat.ChatEvents
import com.dulkirfabric.util.TextUtils
import meteordevelopment.orbit.EventHandler
import net.minecraft.text.Style
import net.minecraft.text.Text

object BridgeBotFormatter {
    private val bridgeRegex: Regex = "^(§2Guild|§3Officer) > (?:\\S+ )?([\\w§]{3,18})(?: §[a-z0-9][[A-Z]+])?§f: ([^>]+)(?: >| »|:) (.+)".toRegex()

    @EventHandler
    fun onChat(event: ChatEvents.AllowChat) {
        if (!DulkirConfig.configOptions.bridgeFormatter) return

        val message = event.message.string
        if (bridgeRegex matches message) {
            val matchResult = bridgeRegex.find(message)
            val (prefix, name, playerName) = matchResult!!.destructured
            if (TextUtils.stripColorCodes(name).equals(DulkirConfig.configOptions.bridgeBotName, ignoreCase = true)) {
                val newPrefix = if (prefix == "§2Guild") "§2Bridge" else "§3Bridge"
                event.message.siblings[0] = Text.literal(
                    "$newPrefix > "
                ).append(Text.literal(playerName).setStyle(Style.EMPTY.withColor(DulkirConfig.configOptions.bridgeNameColor)))
                    .append(": ")
                event.message.siblings[1] = Text.literal(
                    event.message.siblings[1].string.replace("$playerName > ", "")
                ).setStyle(event.message.siblings[1].style)
            }
        }
    }
}