aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/ServerRestartTitle.kt
blob: 2272cdd62923858557d9497723c6352275b51e1a (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
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.data.TitleUtils
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.TimeUtils
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class ServerRestartTitle {
    private val config get() = SkyHanniMod.feature.misc
    private val pattern = "§cServer closing: (?<minutes>\\d+):(?<seconds>\\d+) §8.*".toPattern()

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!config.serverRestartTitle) return

        if (!event.repeatSeconds(1)) return

        for (line in ScoreboardData.sidebarLinesFormatted) {
            pattern.matchMatcher(line) {
                val minutes = group("minutes").toInt()
                val seconds = group("seconds").toInt()
                val totalSeconds = minutes * 60 + seconds
                if (totalSeconds > 120) {
                    if (totalSeconds % 30 != 0) return
                }
                val time = TimeUtils.formatDuration(totalSeconds.toLong() * 1000)
                TitleUtils.sendTitle("§cServer Restart in §b$time", 2_000)
            }
        }
    }
}