aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/fame/UpgradeReminder.kt
blob: 4fc3c2de24aa785629e1e23cc2dfdc2d3e9058eb (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package at.hannibal2.skyhanni.features.fame

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.EntityMovementData
import at.hannibal2.skyhanni.data.IslandGraphs
import at.hannibal2.skyhanni.data.IslandType
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.HypixelCommands
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzVec
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 config get() = SkyHanniMod.feature.misc

    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!",
    )
    @Suppress("UnusedPrivateProperty")
    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 (!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"
        if (!inInventory) return

        if (currentProfileUpgrade == null && currentAccountUpgrade == null) return
        detectWrongAccountUpgradeData(event.inventoryItems)
    }

    private fun detectWrongAccountUpgradeData(items: Map<Int, ItemStack>) {
        val hasProfileUpgrade = foundActiveUpgrade(items, 27..35)
        if (!hasProfileUpgrade && currentProfileUpgrade != null) {
            ChatUtils.chat("§eRemoved invalid Profile Upgrade information.")
            currentProfileUpgrade = null
        }

        val hasAccountUpgrade = foundActiveUpgrade(items, 36..44)
        if (!hasAccountUpgrade && currentAccountUpgrade != null) {
            ChatUtils.chat("§eRemoved invalid Account Upgrade information.")
            currentAccountUpgrade = null
        }
    }

    private fun foundActiveUpgrade(items: Map<Int, ItemStack>, slots: IntRange): Boolean {
        for (slot in slots) {
            val item = items[slot] ?: continue
            val isUpgrading = item.getLore().any { it == "§aCurrently upgrading!" }
            val isDone = item.getLore().any { it == "§cClick to claim!" }
            val isReadyForUpgrade = item.getLore().any { it == "§eClick to start upgrade!" }
            if (isUpgrading || isDone) return true
            if (isReadyForUpgrade) return false
        }
        return false
    }

    @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() = LorenzUtils.inSkyBlock && config.accountUpgradeReminder

    @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.clickToActionOrDisable(
                "The §a$name §eupgrade has completed!",
                config::accountUpgradeReminder,
                actionName = "warp to Hub",
                action = {
                    HypixelCommands.warp("hub")
                    EntityMovementData.onNextTeleport(IslandType.HUB) {
                        IslandGraphs.pathFind(
                            LorenzVec(-2.6, 73.0, -101.6),
                            "§eCommunity Shop",
                            condition = { config.accountUpgradeReminder },
                        )
                    }
                },
            )
        }

        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
                }
            }
        }
    }
}