aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/api/CollectionAPI.kt
blob: f86819895f1ad939ad3ac77cce6e81eb9ba57ab9 (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
package at.hannibal2.skyhanni.api

import at.hannibal2.skyhanni.events.CollectionUpdateEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.ProfileApiDataLoadedEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.features.bazaar.BazaarApi
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.StringUtils.removeColor
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.regex.Pattern

class CollectionAPI {
    private val counterPattern = Pattern.compile("(?:.*) §e(.*)§6\\/(?:.*)")
    private val singleCounterPattern = Pattern.compile("§7Total Collected: §e(.*)")

    private val hypixelApiHasWrongItems = listOf(
        "WOOL",
        "CORRUPTED_FRAGMENT",
        "EGG",
        "POISONOUS_POTATO",
    )

    @SubscribeEvent
    fun onProfileDataLoad(event: ProfileApiDataLoadedEvent) {
        val profileData = event.profileData
        val jsonElement = profileData["collection"]?: return
        val asJsonObject = jsonElement.asJsonObject ?: return
        for ((rawName, rawCounter) in asJsonObject.entrySet()) {
            val counter = rawCounter.asLong
            var itemName = BazaarApi.getBazaarDataForInternalName(rawName)?.itemName
            if (rawName == "MUSHROOM_COLLECTION") {
                itemName = "Mushroom"
            }
            if (rawName == "MELON") {
                itemName = "Melon"
            }
            if (rawName == "GEMSTONE_COLLECTION") {
                itemName = "Gemstone"
            }

            // Hypixel moment
            if (hypixelApiHasWrongItems.contains(rawName)) continue

            if (itemName == null) {
                LorenzUtils.debug("collection name is null for '$rawName'")
                println()
                continue
            }
            collectionValue[itemName] = counter
        }

        CollectionUpdateEvent().postAndCatch()
    }

    @SubscribeEvent
    fun onProfileJoin(event: ProfileJoinEvent) {
        collectionValue.clear()
    }

    @SubscribeEvent
    fun onTick(event: InventoryOpenEvent) {
        val inventoryName = event.inventoryName
        if (inventoryName.endsWith(" Collection")) {
            val stack = event.inventoryItems[4] ?: return
            for (line in stack.getLore()) {
                val matcher = singleCounterPattern.matcher(line)
                if (matcher.matches()) {
                    val counter = matcher.group(1).replace(",", "").toLong()
                    val name = inventoryName.split(" ").dropLast(1).joinToString(" ")
                    collectionValue[name] = counter
                }
            }
            CollectionUpdateEvent().postAndCatch()
        }

        if (inventoryName.endsWith(" Collections")) {
            if (inventoryName == "Boss Collections") return

            for ((_, stack) in event.inventoryItems) {
                var name = stack.name?.removeColor() ?: continue
                if (name.contains("Collections")) continue

                val lore = stack.getLore()
                if (!lore.any { it.contains("Click to view!") }) continue

                if (!isCollectionTier0(lore)) {
                    name = name.split(" ").dropLast(1).joinToString(" ")
                }

                for (line in lore) {
                    val matcher = counterPattern.matcher(line)
                    if (matcher.matches()) {
                        val counter = matcher.group(1).replace(",", "").toLong()
                        collectionValue[name] = counter
                    }
                }
            }
            CollectionUpdateEvent().postAndCatch()
        }
    }

    companion object {
        private val collectionValue = mutableMapOf<String, Long>()
        private val collectionTier0Pattern = Pattern.compile("§7Progress to .* I: .*")

        fun isCollectionTier0(lore: List<String>) = lore.map { collectionTier0Pattern.matcher(it) }.any { it.matches() }

        fun getCollectionCounter(searchName: String): Pair<String, Long>? {
            for ((collectionName, counter) in collectionValue) {
                if (collectionName.equals(searchName, true)) {
                    return Pair(collectionName, counter)
                }
            }
            return null
        }
    }
}