aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/fame/UpgradeReminder.kt
blob: 22171ed3bf5320ed68870f99beef03972b6b563c (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package at.hannibal2.skyhanni.features.fame

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
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.SecondPassedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RegexUtils.anyMatches
import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.TimeUtils
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.annotations.Expose
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object UpgradeReminder {
    private val patternGroup = RepoPattern.group("fame.upgrades")

    private val accountUpgradePattern by patternGroup.pattern(
        "account",
        "§8Account Upgrade"
    )
    private val profileUpgradePattern by patternGroup.pattern(
        "profile",
        "§8Profile Upgrade"
    )
    private val upgradeDurationPattern by patternGroup.pattern(
        "duration",
        "§8Duration: (?<duration>.+)"
    )
    private val upgradeStartedPattern by patternGroup.pattern(
        "started",
        "§eYou started the §r§a(?<upgrade>.+) §r§eupgrade!"
    )
    private val upgradeClaimedPattern by patternGroup.pattern(
        "claimed",
        "§eYou claimed the §r§a(?<upgrade>.+) §r§eupgrade!"
    )
    private val upgradePattern by patternGroup.pattern(
        "upgrade",
        "§eClick to start upgrade!"
    )

    private var currentProfileUpgrade: CommunityShopUpgrade?
        get() = ProfileStorageData.profileSpecific?.communityShopProfileUpgrade
        set(value) {
            ProfileStorageData.profileSpecific?.communityShopProfileUpgrade = value
        }

    private var currentAccountUpgrade: CommunityShopUpgrade?
        get() = ProfileStorageData.playerSpecific?.communityShopAccountUpgrade
        set(value) {
            ProfileStorageData.playerSpecific?.communityShopAccountUpgrade = value
        }

    private var inInventory = false
    private var clickedUpgradeType: UpgradeType? = null
    private var clickedUpgrade: CommunityShopUpgrade? = null
    private var lastReminderSend = SimpleTimeMark.farPast()

    // TODO: (for 0.27) merge this logic with reminder manager
    @SubscribeEvent
    fun onSecondPassed(event: SecondPassedEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!isEnabled()) return
        if (ReminderUtils.isBusy()) return
        if (inInventory || LorenzUtils.skyBlockArea == "Community Center") return
        if (lastReminderSend.passedSince() < 30.seconds) return

        currentProfileUpgrade?.sendReminderIfClaimable()
        currentAccountUpgrade?.sendReminderIfClaimable()

        lastReminderSend = SimpleTimeMark.now()
    }

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

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

    @SubscribeEvent
    fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) {
        if (!inInventory) return
        val item = event.item ?: return
        clickedUpgradeType = UpgradeType.fromItem(item) ?: return
        clickedUpgrade = CommunityShopUpgrade.fromItem(item) ?: return
    }

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (upgradeStartedPattern.matches(event.message)) {
            clickedUpgrade?.start()
            when (clickedUpgradeType) {
                UpgradeType.PROFILE -> currentProfileUpgrade = clickedUpgrade
                UpgradeType.ACCOUNT -> currentAccountUpgrade = clickedUpgrade
                null -> {}
            }
            return
        }

        upgradeClaimedPattern.matchMatcher(event.message) {
            val claimedUpgradeName = group("upgrade")
            when (claimedUpgradeName) {
                currentProfileUpgrade?.name -> currentProfileUpgrade = null
                currentAccountUpgrade?.name -> currentAccountUpgrade = null
            }
        }
    }

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

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

    @SubscribeEvent
    fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
        event.move(49,
            "#player.currentAccountUpgrade",
            "#player.communityShopAccountUpgrade.name"
        )

        event.move(49,
            "#player.nextAccountUpgradeCompletionTime",
            "#player.communityShopAccountUpgrade.completionTime"
        )
    }

    class CommunityShopUpgrade(
        @Expose val name: String?,
        @Expose var completionTime: SimpleTimeMark = SimpleTimeMark.farFuture()
    ) {
        private var duration: Duration = Duration.ZERO

        fun start() {
            this.completionTime = SimpleTimeMark.now() + duration
        }

        fun sendReminderIfClaimable() {
            if (this.name == null || this.completionTime.isInFuture()) return
            ChatUtils.clickableChat(
                "The §a$name §eupgrade has completed! §c(Click to disable these reminders)", onClick = {
                    disable()
                }, oneTimeClick = true
            )
        }

        companion object {
            fun fromItem(item: ItemStack): CommunityShopUpgrade? {
                val name = item.displayName
                val lore = item.getLore()
                val upgrade = CommunityShopUpgrade(name)
                upgrade.duration = lore.matchFirst(upgradeDurationPattern) {
                    val durationStr = group("duration")
                    if (durationStr == "Instant!") return null
                    TimeUtils.getDuration(durationStr)
                } ?: Duration.ZERO
                return upgrade
            }
        }
    }

    enum class UpgradeType {
        PROFILE,
        ACCOUNT,
        ;

        companion object {
            fun fromItem(item: ItemStack): UpgradeType? {
                val lore = item.getLore()
                return when {
                    accountUpgradePattern.anyMatches(lore) -> ACCOUNT
                    profileUpgradePattern.anyMatches(lore) -> PROFILE
                    else -> null
                }
            }
        }
    }
}