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

import com.dulkirfabric.config.DulkirConfig
import com.dulkirfabric.events.LongUpdateEvent
import com.dulkirfabric.events.chat.OverlayReceivedEvent
import com.dulkirfabric.util.ScoreBoardUtils.formattedString
import meteordevelopment.orbit.EventHandler
import net.minecraft.text.Text
import java.text.NumberFormat

object ActionBarUtil {

    private val splitRegex = " {2,}".toRegex()
    var healthStr = ""
    var defStr = ""
    var mana = ""
    var ehp = "§2?"
    var stacks = ""
    private val healthRegex = "([0-9,]+)/([0-9,]+)❤".toRegex()
    private var healthInt = 0
    private val defRegex = "([0-9,]+)❈ Defense".toRegex()
    private var defInt = 0

    @EventHandler
    fun onRenderActionBar(event: OverlayReceivedEvent) {
        if (!Utils.isInSkyblock()) return
        if (!DulkirConfig.configOptions.hudifyActionBar) return
        val sb = StringBuilder()
        var stackFlag = false
        event.message.formattedString().split(splitRegex).forEach {
            healthRegex.matchEntire(TextUtils.stripColorCodes(it))?.let { result ->
                healthInt = result.groupValues[1].replace(",", "").toInt()
                healthStr = it
                return@forEach
            }
            defRegex.matchEntire(TextUtils.stripColorCodes(it))?.let { result ->
                defInt = result.groupValues[1].replace(",", "").toInt()
                defStr = it.replace(" Defense", "")
                return@forEach
            }
            if (it.contains('ᝐ')) {
                stackFlag = true
                stacks = it
                return@forEach
            }
            if (it.contains('✎')) {
                mana = it
                return@forEach
            }
            sb.append(it)
        }
        if (!stackFlag) stacks = ""
        event.setReturnValue(Text.literal(sb.toString()))
    }

    @EventHandler
    fun onLongUpdate(event: LongUpdateEvent) {
        ehp = "§2${NumberFormat.getInstance().format((healthInt * (1 + (defInt / 100f))).toInt())}"
    }
}