aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/dulkirfabric/util/TextUtils.kt
blob: d5e3e37612828f3b3819ac1b1c625514859002e5 (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
package com.dulkirfabric.util

import com.dulkirfabric.DulkirModFabric.mc
import net.minecraft.text.Text

object TextUtils {
    const val CHAT_PREFIX = "§f<§3DulkirMod§f>§r"
    private val colorRegex = "§.".toRegex()
    fun info(text: String, prefix: Boolean = true) {
        if (mc.player == null) return

        val textPrefix = if (prefix) "${CHAT_PREFIX} " else ""
        mc.inGameHud.chatHud.addMessage(Text.literal("$textPrefix$text§r"))
    }

    fun toggledMessage(message: String, state: Boolean) {
        val stateText = if (state) "§aON" else "§cOFF"
        info("§9Toggled $message §8[$stateText§8]§r")
    }

    val Text.unformattedString
        get() = string.replace("§.".toRegex(), "")

    fun sendPartyChatMessage(message: String) {
        this.sendCommand("/pc $message")
    }

    fun sendMessage(message: String) {
        mc.player?.networkHandler?.sendChatMessage(message)
    }

    fun sendCommand(command: String) {
        mc.player?.networkHandler?.sendChatCommand(command)
    }

    fun stripColorCodes(string: String): String {
        return string.replace(colorRegex, "")
    }
}