-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
BestiaryData.kt
534 lines (473 loc) · 21 KB
/
BestiaryData.kt
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package at.hannibal2.skyhanni.features.combat
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.features.combat.BestiaryConfig
import at.hannibal2.skyhanni.config.features.combat.BestiaryConfig.DisplayTypeEntry
import at.hannibal2.skyhanni.config.features.combat.BestiaryConfig.NumberFormatEntry
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.ConfigUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.addButton
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary
import at.hannibal2.skyhanni.utils.NumberUtil.roundToPrecision
import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat
import at.hannibal2.skyhanni.utils.NumberUtil.toRoman
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@SkyHanniModule
object BestiaryData {
private val config get() = SkyHanniMod.feature.combat.bestiary
private val patternGroup = RepoPattern.group("combat.bestiary.data")
/**
* REGEX-TEST: §7Progress to Tier 14: §b26%
* REGEX-TEST: §7Progress to Tier XV: §b57.1%
*/
private val tierProgressPattern by patternGroup.pattern(
"tierprogress",
"§7Progress to Tier [\\dIVXC]+: §b[\\d.]+%"
)
/**
* REGEX-TEST: §7Overall Progress: §b55.2%
* REGEX-TEST: §7Overall Progress: §b100% §7(§c§lMAX!§7)
*/
private val overallProgressPattern by patternGroup.pattern(
"overallprogress",
"§7Overall Progress: §b[\\d.]+%(?: §7\\(§c§lMAX!§7\\))?"
)
private val progressPattern by patternGroup.pattern(
"progress",
"(?<current>[0-9kKmMbB,.]+)/(?<needed>[0-9kKmMbB,.]+\$)"
)
private val titlePattern by patternGroup.pattern(
"title",
"^(?:\\(\\d+/\\d+\\) )?(Bestiary|.+) ➜ (.+)\$"
)
private var display = emptyList<List<Any>>()
private val mobList = mutableListOf<BestiaryMob>()
private val stackList = mutableMapOf<Int, ItemStack>()
private val catList = mutableListOf<Category>()
private var inInventory = false
private var isCategory = false
private var overallProgressEnabled = false
private var indexes = listOf(
10, 11, 12, 13, 14, 15, 16,
19, 20, 21, 22, 23, 24, 25,
28, 29, 30, 31, 32, 33, 34,
37, 38, 39, 40, 41, 42, 43
)
@SubscribeEvent
fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
if (!isEnabled()) return
if (inInventory) {
config.position.renderStringsAndItems(
display, extraSpace = -1, itemScale = 0.7, posLabel = "Bestiary Data"
)
}
}
@SubscribeEvent
fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) {
if (!isEnabled()) return
if (inInventory) {
for (slot in InventoryUtils.getItemsInOpenChest()) {
val stack = slot.stack
val lore = stack.getLore()
if (lore.any { it == "§7Overall Progress: §b100% §7(§c§lMAX!§7)" || it == "§7Families Completed: §a100%" }) {
slot highlight LorenzColor.GREEN
}
if (!overallProgressEnabled && lore.any { it == "§7Overall Progress: §cHIDDEN" }) {
slot highlight LorenzColor.RED
}
}
}
}
@SubscribeEvent
fun onInventoryOpen(event: InventoryFullyOpenedEvent) {
if (!isEnabled()) return
val inventoryName = event.inventoryName
val items = event.inventoryItems
val stack = items[4] ?: return
val bestiaryGui = isBestiaryGui(stack, inventoryName)
if (!(inventoryName == "Bestiary ➜ Fishing" || inventoryName == "Bestiary") && !bestiaryGui) return
isCategory = inventoryName == "Bestiary ➜ Fishing" || inventoryName == "Bestiary"
stackList.putAll(items)
inInventory = true
overallProgressEnabled = isOverallProgressEnabled(items)
update()
}
@SubscribeEvent
fun onInventoryClose(event: InventoryCloseEvent) {
mobList.clear()
stackList.clear()
inInventory = false
}
@SubscribeEvent
fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
event.move(2, "misc.bestiaryData", "combat.bestiary")
event.transform(15, "combat.bestiary.numberFormat") { element ->
ConfigUtils.migrateIntToEnum(element, NumberFormatEntry::class.java)
}
event.transform(15, "combat.bestiary.displayType") { element ->
ConfigUtils.migrateIntToEnum(element, DisplayTypeEntry::class.java)
}
}
private fun update() {
display = drawDisplay()
}
private fun init() {
mobList.clear()
catList.clear()
if (isCategory) {
inCategory()
} else {
notInCategory()
}
}
private fun inCategory() {
for ((index, stack) in stackList) {
if (stack.displayName == " ") continue
if (!indexes.contains(index)) continue
inInventory = true
val name = stack.displayName
var familiesFound: Long = 0
var totalFamilies: Long = 0
var familiesCompleted: Long = 0
for ((lineIndex, loreLine) in stack.getLore().withIndex()) {
val line = loreLine.removeColor()
if (!line.startsWith(" ")) continue
val previousLine = stack.getLore()[lineIndex - 1]
val progress = line.substring(line.lastIndexOf(' ') + 1)
if (previousLine.contains("Families Found")) {
progressPattern.matchMatcher(progress) {
familiesFound = group("current").formatLong()
totalFamilies = group("needed").formatLong()
}
} else if (previousLine.contains("Families Completed")) {
progressPattern.matchMatcher(progress) {
familiesCompleted = group("current").formatLong()
}
}
}
catList.add(Category(name, familiesFound, totalFamilies, familiesCompleted))
}
}
private fun notInCategory() {
for ((index, stack) in stackList) {
if (stack.displayName == " ") continue
if (!indexes.contains(index)) continue
inInventory = true
val name = " [IVX0-9]+$".toPattern().matcher(stack.displayName).replaceFirst("")
val level = " ([IVX0-9]+$)".toRegex().find(stack.displayName)?.groupValues?.get(1) ?: "0"
var totalKillToMax: Long = 0
var currentTotalKill: Long = 0
var totalKillToTier: Long = 0
var currentKillToTier: Long = 0
var actualRealTotalKill: Long = 0
for ((lineIndex, line) in stack.getLore().withIndex()) {
val loreLine = line.removeColor()
if (loreLine.startsWith("Kills: ")) {
actualRealTotalKill = "([0-9,.]+)".toRegex().find(loreLine)?.groupValues?.get(1)?.formatLong()
?: 0
}
if (!loreLine.startsWith(" ")) continue
val previousLine = stack.getLore()[lineIndex - 1]
val progress = loreLine.substring(loreLine.lastIndexOf(' ') + 1)
if (previousLine.contains("Progress to Tier")) {
progressPattern.matchMatcher(progress) {
totalKillToTier = group("needed").formatLong()
currentKillToTier = group("current").formatLong()
}
} else if (previousLine.contains("Overall Progress")) {
progressPattern.matchMatcher(progress) {
totalKillToMax = group("needed").formatLong()
currentTotalKill = group("current").formatLong()
}
}
}
mobList.add(
BestiaryMob(
name,
level,
totalKillToMax,
currentTotalKill,
totalKillToTier,
currentKillToTier,
actualRealTotalKill
)
)
}
}
private fun drawDisplay(): List<List<Any>> {
val newDisplay = mutableListOf<List<Any>>()
if (!overallProgressEnabled) {
newDisplay.addAsSingletonList("§7Bestiary Data")
newDisplay.addAsSingletonList(" §cPlease enable Overall Progress")
newDisplay.addAsSingletonList(" §cUsing the Eye of Ender highlighted in red.")
return newDisplay
}
init()
addCategories(newDisplay)
if (mobList.isEmpty()) return newDisplay
addList(newDisplay)
addButtons(newDisplay)
return newDisplay
}
private fun sortMobList(): MutableList<BestiaryMob> {
val sortedMobList = when (config.displayType) {
DisplayTypeEntry.GLOBAL_MAX -> mobList.sortedBy { it.percentToMax() }
DisplayTypeEntry.GLOBAL_NEXT -> mobList.sortedBy { it.percentToTier() }
DisplayTypeEntry.LOWEST_TOTAL -> mobList.sortedBy { it.actualRealTotalKill }
DisplayTypeEntry.HIGHEST_TOTAL -> mobList.sortedByDescending { it.actualRealTotalKill }
DisplayTypeEntry.LOWEST_MAX -> mobList.sortedBy { it.killNeededToMax() }
DisplayTypeEntry.HIGHEST_MAX -> mobList.sortedByDescending { it.killNeededToMax() }
DisplayTypeEntry.LOWEST_NEXT -> mobList.sortedBy { it.killNeededToNextLevel() }
DisplayTypeEntry.HIGHEST_NEXT -> mobList.sortedByDescending { it.killNeededToNextLevel() }
else -> mobList.sortedBy { it.actualRealTotalKill }
}.toMutableList()
return sortedMobList
}
private fun addList(newDisplay: MutableList<List<Any>>) {
val sortedMobList = sortMobList()
newDisplay.addAsSingletonList("§7Bestiary Data")
for (mob in sortedMobList) {
val isUnlocked = mob.actualRealTotalKill != 0.toLong()
val isMaxed = mob.percentToMax() >= 1
if (!isUnlocked) {
newDisplay.add(buildList {
add(" §7- ")
add("${mob.name}: §cNot unlocked!")
})
continue
}
if (isMaxed && config.hideMaxed) continue
val text = getMobLine(mob, isMaxed)
val tips = getMobHover(mob)
newDisplay.addAsSingletonList(Renderable.hoverTips(text, tips) { true })
}
}
private fun getMobHover(mob: BestiaryMob) = listOf(
"§6Name: §b${mob.name}",
"§6Level: §b${mob.level} ${if (!config.replaceRoman) "§7(${mob.level.romanToDecimalIfNecessary()})" else ""}",
"§6Total Kills: §b${mob.actualRealTotalKill.formatNumber()}",
"§6Kills needed to max: §b${mob.killNeededToMax().formatNumber()}",
"§6Kills needed to next lvl: §b${mob.killNeededToNextLevel().formatNumber()}",
"§6Current kill to next level: §b${mob.currentKillToNextLevel.formatNumber()}",
"§6Kill needed for next level: §b${mob.killNeededForNextLevel.formatNumber()}",
"§6Current kill to max: §b${mob.killToMax.formatNumber()}",
"§6Percent to max: §b${mob.percentToMaxFormatted()}",
"§6Percent to tier: §b${mob.percentToTierFormatted()}",
"",
"§7More info thing"
)
private fun getMobLine(mob: BestiaryMob, isMaxed: Boolean): String {
val type = config.displayType
var text = ""
text += " §7- "
text += "${mob.name} ${mob.level.romanOrInt()}: "
text += if (isMaxed) {
"§c§lMAXED! §7(§b${mob.actualRealTotalKill.formatNumber()}§7 kills)"
} else {
when (type) {
DisplayTypeEntry.GLOBAL_MAX, DisplayTypeEntry.GLOBAL_NEXT -> {
val currentKill = when (type) {
DisplayTypeEntry.GLOBAL_MAX -> mob.totalKills
DisplayTypeEntry.GLOBAL_NEXT -> mob.currentKillToNextLevel
else -> 0
}
val killNeeded = when (type) {
DisplayTypeEntry.GLOBAL_MAX -> mob.killToMax
DisplayTypeEntry.GLOBAL_NEXT -> mob.killNeededForNextLevel
else -> 0
}
val percentage = ((currentKill.toDouble() / killNeeded) * 100).roundToPrecision(2)
val suffix = if (type == DisplayTypeEntry.GLOBAL_NEXT) "§ato level ${mob.getNextLevel()}" else ""
"§7(§b${currentKill.formatNumber()}§7/§b${killNeeded.formatNumber()}§7) §a$percentage§6% $suffix"
}
DisplayTypeEntry.LOWEST_TOTAL, DisplayTypeEntry.HIGHEST_TOTAL -> {
"§6${mob.actualRealTotalKill.formatNumber()} §7total kills"
}
DisplayTypeEntry.LOWEST_MAX, DisplayTypeEntry.HIGHEST_MAX -> {
"§6${mob.killNeededToMax().formatNumber()} §7kills needed"
}
DisplayTypeEntry.LOWEST_NEXT, DisplayTypeEntry.HIGHEST_NEXT -> {
"§6${mob.killNeededToNextLevel().formatNumber()} §7kills needed"
}
else -> "§cYou are not supposed to see this, please report it to @HiZe on discord!"
}
}
return text
}
private fun addButtons(newDisplay: MutableList<List<Any>>) {
newDisplay.addButton(
prefix = "§7Number Format: ",
getName = FormatType.entries[config.numberFormat.ordinal].type, // todo: avoid ordinal
onChange = {
// todo: avoid ordinal
config.numberFormat = BestiaryConfig.NumberFormatEntry.entries[(config.numberFormat.ordinal + 1) % 2]
update()
})
newDisplay.addButton(
prefix = "§7Display Type: ",
getName = DisplayType.entries[config.displayType.ordinal].type, // todo: avoid ordinal
onChange = {
// todo: avoid ordinal
config.displayType = DisplayTypeEntry.entries[(config.displayType.ordinal + 1) % 8]
update()
})
newDisplay.addButton(
prefix = "§7Number Type: ",
getName = NumberType.entries[if (config.replaceRoman) 0 else 1].type,
onChange = {
config.replaceRoman = !config.replaceRoman
update()
}
)
newDisplay.addButton(
prefix = "§7Hide Maxed: ",
getName = HideMaxed.entries[if (config.hideMaxed) 1 else 0].type,
onChange = {
config.hideMaxed = !config.hideMaxed
update()
}
)
}
private fun addCategories(newDisplay: MutableList<List<Any>>) {
if (catList.isNotEmpty()) {
newDisplay.addAsSingletonList("§7Category")
for (cat in catList) {
newDisplay.add(buildList {
add(" §7- ${cat.name}§7: ")
val element = when {
cat.familiesCompleted == cat.totalFamilies -> "§c§lCompleted!"
cat.familiesFound == cat.totalFamilies -> "§b${cat.familiesCompleted}§7/§b${cat.totalFamilies} §7completed"
cat.familiesFound < cat.totalFamilies ->
"§b${cat.familiesFound}§7/§b${cat.totalFamilies} §7found, §b${cat.familiesCompleted}§7/§b${cat.totalFamilies} §7completed"
else -> continue
}
add(element)
})
}
}
}
private fun isOverallProgressEnabled(inventoryItems: Map<Int, ItemStack>): Boolean {
if (inventoryItems[52]?.item == Items.ender_eye) {
return inventoryItems[52]?.getLore()?.any { it == "§7Overall Progress: §aSHOWN" } == true
}
indexes.forEach { index ->
val item = inventoryItems[index] ?: return true
val hasTierProgress = item.getLore().any { tierProgressPattern.matches(it) }
val hasOverallProgress = item.getLore().any { overallProgressPattern.matches(it) }
if (hasTierProgress && !hasOverallProgress) return false
}
return true
}
private fun isBestiaryGui(stack: ItemStack?, name: String): Boolean {
if (stack == null) return false
val bestiaryGuiTitleMatcher = titlePattern.matcher(name)
if (bestiaryGuiTitleMatcher.matches()) {
if ("Bestiary" != bestiaryGuiTitleMatcher.group(1)) {
var loreContainsFamiliesFound = false
for (line in stack.getLore()) {
if (line.removeColor().startsWith("Families Found")) {
loreContainsFamiliesFound = true
break
}
}
if (!loreContainsFamiliesFound) {
return false
}
}
return true
} else if (name == "Search Results") {
val loreList = stack.getLore()
if (loreList.size >= 2 && loreList[0].startsWith("§7Query: §a")
&& loreList[1].startsWith("§7Results: §a")
) {
return true
}
}
return false
}
enum class FormatType(val type: String) {
SHORT("Short"),
LONG("Long")
}
enum class NumberType(val type: String) {
INT("Normal (1, 2, 3)"),
ROMAN("Roman (I, II, III")
}
enum class DisplayType(val type: String) {
GLOBAL_MAX("Global display (to max)"),
GLOBAL_TIER("Global display (to next tier)"),
LOWEST_TOTAL("Lowest total kills"),
HIGHEST_TOTAL("Highest total kills"),
LOWEST_NEEDED_MAX("Lowest kills needed to max"),
HIGHEST_NEEDED_MAX("Highest kills needed to max"),
LOWEST_NEEDED_TIER("Lowest kills needed to next tier"),
HIGHEST_NEEDED_TIER("Highest kills needed to next tier"),
}
enum class HideMaxed(val type: String) {
NO("Show"),
YES("Hide")
}
private fun Long.formatNumber(): String = when (config.numberFormat) {
BestiaryConfig.NumberFormatEntry.SHORT -> this.shortFormat()
BestiaryConfig.NumberFormatEntry.LONG -> this.addSeparators()
else -> "0"
}
data class Category(
val name: String,
val familiesFound: Long,
val totalFamilies: Long,
val familiesCompleted: Long,
)
data class BestiaryMob(
var name: String,
var level: String,
var killToMax: Long,
var totalKills: Long,
var killNeededForNextLevel: Long,
var currentKillToNextLevel: Long,
var actualRealTotalKill: Long,
) {
fun killNeededToMax(): Long {
return 0L.coerceAtLeast(killToMax - actualRealTotalKill)
}
fun killNeededToNextLevel(): Long {
return 0L.coerceAtLeast(killNeededForNextLevel - currentKillToNextLevel)
}
fun percentToMax() = actualRealTotalKill.toDouble() / killToMax
fun percentToMaxFormatted() = LorenzUtils.formatPercentage(percentToMax())
fun percentToTier() =
if (killNeededForNextLevel == 0L) 1.0 else currentKillToNextLevel.toDouble() / killNeededForNextLevel
fun percentToTierFormatted() = LorenzUtils.formatPercentage(percentToTier())
fun getNextLevel() = level.getNextLevel()
}
private fun String.romanOrInt() = romanToDecimalIfNecessary().let {
if (config.replaceRoman || it == 0) it.toString() else it.toRoman()
}
private fun String.getNextLevel() = if (this == "0") {
"I".romanOrInt()
} else {
val intValue = romanToDecimalIfNecessary()
(intValue + 1).toRoman().romanOrInt()
}
private fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled
}