summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/FarmingFortuneDisplay.kt
blob: 29b92f24afd8facb5e1030bb76cbd0323a76ffdc (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package at.hannibal2.skyhanni.features.garden

import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.GardenCropMilestones
import at.hannibal2.skyhanni.data.GardenCropMilestones.getCounter
import at.hannibal2.skyhanni.events.CropClickEvent
import at.hannibal2.skyhanni.events.GardenToolChangeEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.TabListUpdateEvent
import at.hannibal2.skyhanni.features.garden.CropType.Companion.getTurboCrop
import at.hannibal2.skyhanni.features.garden.pests.PestAPI
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RegexUtils.groupOrNull
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getFarmingForDummiesCount
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getHoeCounter
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.math.floor
import kotlin.math.log10
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object FarmingFortuneDisplay {
    private val config get() = GardenAPI.config.farmingFortunes

    private val patternGroup = RepoPattern.group("garden.fortunedisplay")
    private val universalTabFortunePattern by patternGroup.pattern(
        "tablist.universal",
        " Farming Fortune: §r§6☘(?<fortune>\\d+)",
    )
    private val cropSpecificTabFortunePattern by patternGroup.pattern(
        "tablist.cropspecific",
        " (?<crop>Wheat|Carrot|Potato|Pumpkin|Sugar Cane|Melon|Cactus|Cocoa Beans|Mushroom|Nether Wart) Fortune: §r§6☘(?<fortune>\\d+)",
    )
    private val collectionPattern by patternGroup.pattern(
        "collection",
        "§7You have §6\\+(?<ff>\\d{1,3})☘ .*",
    )
    private val tooltipFortunePattern by patternGroup.pattern(
        "tooltip.new",
        "^§7Farming Fortune: §a\\+(?<display>[\\d.]+)(?: §2\\(\\+\\d\\))?(?: §9\\(\\+(?<reforge>\\d+)\\))?(?: §d\\(\\+(?<gemstone>\\d+)\\))?\$",
    )
    private val armorAbilityPattern by patternGroup.pattern(
        "armorability",
        "Tiered Bonus: .* [(](?<pieces>.*)/4[)]",
    )
    private val lotusAbilityPattern by patternGroup.pattern(
        "lotusability",
        "§7Piece Bonus: §6+(?<bonus>.*)☘",
    )

    // todo make pattern work on Melon and Cropie armor
    private val armorAbilityFortunePattern by patternGroup.pattern(
        "armorabilityfortune",
        "§7.*§7Grants §6(?<bonus>.*)☘.*",
    )

    private var display = emptyList<Renderable>()

    private var lastToolSwitch = SimpleTimeMark.farPast()

    private val latestFF: MutableMap<CropType, Double>? get() = GardenAPI.storage?.latestTrueFarmingFortune

    private var currentCrop: CropType? = null

    private var tabFortuneUniversal: Double = 0.0
    private var tabFortuneCrop: Double = 0.0

    var displayedFortune = 0.0
    var reforgeFortune = 0.0
    var gemstoneFortune = 0.0
    var itemBaseFortune = 0.0
    var greenThumbFortune = 0.0
    var pesterminatorFortune = 0.0

    private var foundTabUniversalFortune = false
    private var foundTabCropFortune = false
    private var gardenJoinTime = SimpleTimeMark.farPast()
    private var firstBrokenCropTime = SimpleTimeMark.farPast()
    private var lastUniversalFortuneMissingError = SimpleTimeMark.farPast()
    private var lastCropFortuneMissingError = SimpleTimeMark.farPast()

    @SubscribeEvent
    fun onTabListUpdate(event: TabListUpdateEvent) {
        if (!GardenAPI.inGarden()) return
        event.tabList.firstNotNullOfOrNull {
            universalTabFortunePattern.matchMatcher(it) {
                val fortune = group("fortune").toDouble()
                foundTabUniversalFortune = true
                if (fortune != tabFortuneUniversal) {
                    tabFortuneUniversal = fortune
                    update()
                }
            }
            cropSpecificTabFortunePattern.matchMatcher(it) {
                val crop = CropType.getByName(group("crop"))
                val cropFortune = group("fortune").toDouble()

                currentCrop = crop
                foundTabCropFortune = true
                if (cropFortune != tabFortuneCrop) {
                    tabFortuneCrop = cropFortune
                    update()
                }
                if (GardenAPI.cropInHand == crop) {
                    latestFF?.put(crop, getCurrentFarmingFortune())
                }
            }
        }
    }

    @SubscribeEvent
    fun onGardenToolChange(event: GardenToolChangeEvent) {
        lastToolSwitch = SimpleTimeMark.now()
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent) {
        if (!isEnabled()) return
        if (GardenAPI.hideExtraGuis()) return
        if (GardenAPI.toolInHand == null) return
        config.pos.renderRenderables(display, posLabel = "True Farming Fortune")
    }

    private fun update() {
        display =
            if (gardenJoinTime.passedSince() > 5.seconds && !foundTabUniversalFortune && !gardenJoinTime.isFarPast()) {
                drawMissingFortuneDisplay(false)
            } else if (firstBrokenCropTime.passedSince() > 10.seconds && !foundTabCropFortune && !firstBrokenCropTime.isFarPast()) {
                drawMissingFortuneDisplay(true)
            } else drawDisplay()
    }

    private fun drawDisplay() = buildList {
        val displayCrop = GardenAPI.cropInHand ?: currentCrop ?: return@buildList

        val list = mutableListOf<Renderable>()
        list.add(Renderable.itemStack(displayCrop.icon))

        var recentlySwitchedTool = lastToolSwitch.passedSince() < 1.5.seconds
        val wrongTabCrop = GardenAPI.cropInHand != null && GardenAPI.cropInHand != currentCrop

        val farmingFortune = if (wrongTabCrop) {
            (displayCrop.getLatestTrueFarmingFortune() ?: -1.0).also {
                recentlySwitchedTool = false
            }
        } else getCurrentFarmingFortune()

        list.add(
            Renderable.string(
                "§6Farming Fortune§7: §e" + if (!recentlySwitchedTool && farmingFortune != -1.0) {
                    farmingFortune.round(0).addSeparators()
                } else "§7" + (displayCrop.getLatestTrueFarmingFortune()?.addSeparators() ?: "?"),
            ),
        )
        add(Renderable.horizontalContainer(list))

        val ffReduction = getPestFFReduction()
        if (ffReduction > 0) {
            add(Renderable.string("§cPests are reducing your fortune by §e$ffReduction%§c!"))
        }

        if (wrongTabCrop) {
            var text = "§cBreak §e${GardenAPI.cropInHand?.cropName}§c to see"
            if (farmingFortune != -1.0) text += " latest"
            text += " fortune!"

            add(Renderable.string(text))
        }
    }

    private fun drawMissingFortuneDisplay(cropFortune: Boolean) = buildList {
        if (cropFortune) {
            add(
                Renderable.clickAndHover(
                    "§cNo Crop Fortune Found! Enable The Stats Widget",
                    listOf(
                        "§cEnable the Stats widget and enable",
                        "§cshowing latest Crop Fortune.",
                    ),
                    onClick = {
                        HypixelCommands.widget()
                    },
                ),
            )
        } else {
            add(
                Renderable.clickAndHover(
                    "§cNo Farming Fortune Found! Enable The Stats Widget",
                    listOf(
                        "§cEnable the Stats widget and enable",
                        "§cshowing the Farming Fortune stat.",
                    ),
                    onClick = {
                        HypixelCommands.widget()
                    },
                ),
            )
        }
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!GardenAPI.inGarden()) return
        if (event.isMod(2)) update()
        if (gardenJoinTime.passedSince() > 5.seconds && !foundTabUniversalFortune && !gardenJoinTime.isFarPast()) {
            if (lastUniversalFortuneMissingError.passedSince() < 1.minutes) return
            ChatUtils.clickableChat(
                "§cCan not read Farming Fortune from tab list! Open /widget, enable the Stats Widget and " +
                    "show the Farming Fortune stat, also give the widget enough priority.",
                onClick = { HypixelCommands.widget() },
                "§eClick to run /widget!",
            )
            lastUniversalFortuneMissingError = SimpleTimeMark.now()
        }
        if (firstBrokenCropTime.passedSince() ><