aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/UserLuckBreakdown.kt
blob: 8592a0d7259c12644f1864585d5bc6415e1c5985 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent
import at.hannibal2.skyhanni.features.skillprogress.SkillType
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.client.player.inventory.ContainerLocalMenu
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object UserLuckBreakdown {
    private var inMiscStats = false
    private var replaceSlot: Int? = null
    private var itemCreateCoolDown = SimpleTimeMark.farPast()
    private var skillCalcCoolDown = SimpleTimeMark.farPast()

    private val storage get() = ProfileStorageData.playerSpecific

    private lateinit var mainLuckItem: ItemStack
    private val mainLuckID = "ENDER_PEARL".asInternalName()
    private val mainLuckName = "§a✴ SkyHanni User Luck"

    private lateinit var fillerItem: ItemStack
    private var fillerID = "STAINED_GLASS_PANE".asInternalName()
    private val fillerName = " "

    private lateinit var limboItem: ItemStack
    private var limboID = "ENDER_PEARL".asInternalName()
    private val limboName = "§a✴ Limbo Personal Best"

    private lateinit var skillsItem: ItemStack
    private var skillsID = "DIAMOND_SWORD".asInternalName()
    private val skillsName = "§a✴ Category: Skills"

    private var showAllStats = true

    /**
     * REGEX-TEST: §7Show all stats: §aYes
     * REGEX-TEST: §7Show all stats: §cNope
     */
    private val showAllStatsPattern by RepoPattern.pattern(
        "misc.statsbreakdown.showallstats",
        "§7Show all stats: §.(?<toggle>.*)",
    )

    private val luckTooltipString = "§5§o §a✴ SkyHanni User Luck §f"
    private var inCustomBreakdown = false

    private val validItemSlots = (10..53).filter { it !in listOf(17, 18, 26, 27, 35, 36) && it !in 44..53 }
    private val invalidItemSlots = (0..53).filter { it !in validItemSlots }

    private var skillOverflowLuck = mutableMapOf<SkillType, Int>()

    @SubscribeEvent
    fun replaceItem(event: ReplaceItemEvent) {
        if (event.inventory !is ContainerLocalMenu) return
        if (!inMiscStats) return

        if (event.slot == replaceSlot && !inCustomBreakdown) {
            val limboUserLuck = storage?.limbo?.userLuck ?: 0.0f
            if (limboUserLuck == 0.0f && !showAllStats) return
            if (itemCreateCoolDown.passedSince() > 3.seconds) {
                itemCreateCoolDown = SimpleTimeMark.now()
                createItems()
            }
            event.replace(mainLuckItem)
            return
        }
        if (inCustomBreakdown) {
            if (itemCreateCoolDown.passedSince() > 3.seconds) {
                itemCreateCoolDown = SimpleTimeMark.now()
                createItems()
            }
            checkItemSlot(event)
        }
    }

    private fun checkItemSlot(event: ReplaceItemEvent) {
        when (event.slot) {
            48, 49 -> return

            10 -> event.replace(skillsItem)
            11 -> event.replace(limboItem)

            in validItemSlots -> event.replace(null)

            in invalidItemSlots -> {
                if (event.originalItem.item == limboID.getItemStack().item) return
                event.replace(fillerItem)
                return
            }
        }
    }

    @SubscribeEvent
    fun openInventory(event: InventoryOpenEvent) {
        if (event.inventoryName != "Your Stats Breakdown") {
            inMiscStats = false
            return
        }
        val inventoryName = event.inventoryItems[4]?.name ?: ""
        if (inventoryName != "§dMisc Stats") return
        inMiscStats = true
        replaceSlot = findValidSlot(event.inventoryItems)
        val showAllStatsLore = event.inventoryItems[50]?.getLore() ?: listOf("")
        for (line in showAllStatsLore) {
            showAllStatsPattern.matchMatcher(line) {
                showAllStats = when (group("toggle")) {
                    "Yes" -> true
                    else -> false
                }
            }
        }
        return
    }

    @SubscribeEvent
    fun closeInventory(event: InventoryCloseEvent) {
        inMiscStats = false
        inCustomBreakdown = false
    }

    private fun findValidSlot(input: Map<Int, ItemStack>): Int? {
        for (slot in input.keys) {
            if (slot !in validItemSlots && slot < 44) continue
            val itemStack = input[slot]
            if (itemStack?.name == " ") {
                return slot
            }
        }
        return null
    }

    @SubscribeEvent
    fun onHoverItem(event: LorenzToolTipEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (skillCalcCoolDown.passedSince() > 3.seconds) {
            skillCalcCoolDown = SimpleTimeMark.now()
            calcSkillLuck()
        }
        val limboLuck = storage?.limbo?.userLuck?.round(1) ?: 0.0f
        when (event.slot.inventory.name) {
            "Your Equipment and Stats" -> equipmentMenuTooltip(event, limboLuck)
            "Your Stats Breakdown" -> statsBreakdownLoreTooltip(event, limboLuck)
            "SkyBlock Menu" -> skyblockMenuTooltip(event, limboLuck)
        }
    }

    private fun equipmentMenuTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
        if (event.slot.slotIndex != 25) return
        if (limboLuck == 0.0f && !showAllStats) return

        val skillLuck = skillOverflowLuck.values.sum()
        val totalLuck = skillLuck + limboLuck
        val lastIndex = event.toolTip.indexOfLast { it == "§5§o" }
        if (lastIndex == -1) return

        val luckString = tryTruncateFloat(totalLuck)
        event.toolTip.add(lastIndex, "$luckTooltipString$luckString")
    }

    private fun statsBreakdownLoreTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
        if (!inMiscStats) return
        if (inCustomBreakdown && event.slot.slotIndex == 48) {
            event.toolTip[1] = "§7To Your Stats Breakdown"
        }
        if (event.slot.slotIndex != 4) return
        if (limboLuck == 0.0f && !showAllStats) return

        val skillLuck = skillOverflowLuck.values.sum()
        val totalLuck = skillLuck + limboLuck
        val luckString = tryTruncateFloat(totalLuck)
        event.toolTip.add("§5§o §a✴ SkyHanni User Luck §f$luckString")
    }

    private fun skyblockMenuTooltip(event: LorenzToolTipEvent, limboLuck: Float) {
        if (event.slot.slotIndex != 13) return
        val lastIndex = event.toolTip.indexOfLast { it == "§5§o" }
        if (lastIndex == -1) return

        val skillLuck = skillOverflowLuck.values.sum()
        val totalLuck =