aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/fame/AccountUpgradeReminder.kt
blob: a383d86833e346b240c339bb7d2bc87df9eddab9 (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
113
114
115
116
117
118
119
120
121
122
package at.hannibal2.skyhanni.features.fame

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.seconds

class AccountUpgradeReminder {

    private var inInventory = false
    private var duration: Duration? = null
    private var lastReminderSend = SimpleTimeMark.farPast()

    // TODO: find a way to save SimpleTimeMark directly in the config
    private var nextCompletionTime: SimpleTimeMark?
        get() = ProfileStorageData.playerSpecific?.nextAccountUpgradeCompletionTime?.asTimeMark()
        set(value) {
            value?.let {
                ProfileStorageData.playerSpecific?.nextAccountUpgradeCompletionTime = it.toMillis()
            }
        }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (event.repeatSeconds(1)) {
            checkReminder()
        }
    }

    // TODO: Merge this logic with CityProjectFeatures reminder to reduce duplication
    private fun checkReminder() {
        if (!isEnabled()) return
        val playerSpecific = ProfileStorageData.playerSpecific ?: return
        if (ReminderUtils.isBusy()) return
        if (LorenzUtils.skyBlockArea == "Community Center") return

        val upgrade = playerSpecific.currentAccountUpgrade ?: return
        val nextCompletionTime = nextCompletionTime ?: return
        if (!nextCompletionTime.isInPast()) return

        if (lastReminderSend.passedSince() < 30.seconds) return
        lastReminderSend = SimpleTimeMark.now()

        ChatUtils.clickableChat(
            "The §a$upgrade §eupgrade has completed! §c(Click to disable these reminders)",
            "shstopaccountupgradereminder"
        )
    }

    @SubscribeEvent
    fun onInventoryLoad(event: InventoryFullyOpenedEvent) {
        if (!LorenzUtils.inSkyBlock) return
        inInventory = event.inventoryName == "Community Shop"
    }

    @SubscribeEvent
    @Suppress("UNUSED_PARAMETER")
    fun onInventoryClose(event: InventoryCloseEvent) {
        inInventory = false
    }

    @SubscribeEvent
    fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) {
        if (!inInventory) return
        val clickedItemLore = event.slot?.stack?.getLore() ?: return
        if (clickedItemLore.getOrNull(0) != "§8Account Upgrade") return
        val result = clickedItemLore.firstNotNullOfOrNull {
            durationRegex.matchEntire(it)
        } ?: return
        duration = result.groups[1]!!.value.toInt().days
    }

    @SubscribeEvent
    fun onUpgradeStarted(event: LorenzChatEvent) {
        if (claimedRegex.matches(event.message)) {
            clearUpgrade()
        } else {
            val upgrade = startedRegex.matchEntire(event.message)?.groups?.get(1)?.value ?: return
            startUpgrade(upgrade)
        }
    }

    private fun startUpgrade(upgrade: String) {
        val duration = duration ?: return
        val playerSpecific = ProfileStorageData.playerSpecific ?: return
        playerSpecific.currentAccountUpgrade = upgrade

        nextCompletionTime = SimpleTimeMark.now() + duration
    }

    private fun clearUpgrade() {
        val playerSpecific = ProfileStorageData.playerSpecific ?: return
        playerSpecific.currentAccountUpgrade = null
        nextCompletionTime = SimpleTimeMark.farPast()
    }

    companion object {

        private val durationRegex = "§8Duration: (\\d{1,3})d".toRegex()
        private val startedRegex = "§eYou started the §r§a(.+) §r§eupgrade!".toRegex()
        private val claimedRegex = "§eYou claimed the §r§a.+ §r§eupgrade!".toRegex()

        private fun isEnabled() = SkyHanniMod.feature.misc.accountUpgradeReminder

        fun disable() {
            SkyHanniMod.feature.misc.accountUpgradeReminder = false
        }
    }
}