aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/nether/reputationhelper/dailyquest/QuestLoader.kt
blob: e4bba6494bde288c9cc53e7834f5d29806795deb (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
package at.hannibal2.skyhanni.features.nether.reputationhelper.dailyquest

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.features.nether.reputationhelper.dailyquest.quest.*
import at.hannibal2.skyhanni.utils.InventoryUtils.getInventoryName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.TabListUtils
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest

class QuestLoader(private val dailyQuestHelper: DailyQuestHelper) {

    fun loadFromTabList() {
        var i = -1
        for (line in TabListUtils.getTabList()) {
            if (line.contains("Faction Quests:")) {
                i = 0
                continue
            }
            if (i == -1) continue

            i++
            readQuest(line)
            if (i == 5) {
                break
            }
        }
    }

    private fun readQuest(line: String) {
        var text = line.substring(3)
        val green = text.startsWith("§a")
        text = text.substring(2)

        val amount: Int
        val name: String
        if (text.contains(" §r§8x")) {
            val split = text.split(" §r§8x")
            name = split[0]
            amount = split[1].toInt()
        } else {
            name = text
            amount = 1
        }

        checkQuest(name, green, amount)
    }

    private fun checkQuest(name: String, green: Boolean, needAmount: Int) {
        val oldQuest = getQuestByName(name)
        if (oldQuest != null) {
            if (green) {
                if (oldQuest.state != QuestState.READY_TO_COLLECT && oldQuest.state != QuestState.COLLECTED) {
                    oldQuest.state = QuestState.READY_TO_COLLECT
                    dailyQuestHelper.update()
                    LorenzUtils.debug("Reputation Helper: Tab-List updated ${oldQuest.internalName} (This should not happen)")
                }
            }
            return
        }

        val state = if (green) QuestState.READY_TO_COLLECT else QuestState.NOT_ACCEPTED
        dailyQuestHelper.update()
        dailyQuestHelper.quests.add(addQuest(name, state, needAmount))
    }

    private fun addQuest(name: String, state: QuestState, needAmount: Int): Quest {
        for (miniBoss in dailyQuestHelper.reputationHelper.miniBossHelper.miniBosses) {
            if (name == miniBoss.displayName) {
                return MiniBossQuest(miniBoss, state, needAmount)
            }
        }
        for (kuudraTier in dailyQuestHelper.reputationHelper.kuudraBossHelper.kuudraTiers) {
            val kuudraName = kuudraTier.name
            if (name == "Kill Kuudra $kuudraName Tier") {
                return KuudraQuest(kuudraTier, state)
            }
        }

        for (entry in dailyQuestHelper.reputationHelper.repoData.entrySet()) {
            val categoryName = entry.key
            val category = entry.value.asJsonObject
            for ((entryName, extraData) in category.entrySet()) {
                val data = extraData.asJsonObject
                val displayItem = data["item"]?.asString
                val location = dailyQuestHelper.reputationHelper.readLocationData(data)
                if (name.startsWith("$entryName Rank ")) {
                    val split = name.split(" Rank ")
                    val dojoName = split[0]
                    val dojoRankGoal = split[1]
                    return DojoQuest(dojoName, location, displayItem, dojoRankGoal, state)
                }

                if (name == entryName) {
                    when (categoryName) {
                        "FISHING" -> return TrophyFishQuest(name, location, displayItem, state, needAmount)
                        "RESCUE" -> return RescueMissionQuest(displayItem, location, state)
                        "FETCH" -> return FetchQuest(name, location, displayItem, state, needAmount)
                    }
                }
            }
        }

        println("Unknown quest: '$name'")
        return UnknownQuest(name)
    }

    private fun getQuestByName(name: String): Quest? {
        return dailyQuestHelper.quests.firstOrNull { it.internalName == name }
    }

    fun checkInventory() {
        val inMageRegion = LorenzUtils.skyBlockArea == "Community Center"
        val inBarbarianRegion = LorenzUtils.skyBlockArea == "Dragontail"
        if (!inMageRegion && !inBarbarianRegion) return

        val gui = Minecraft.getMinecraft().currentScreen
        if (gui !is GuiChest) return
        val chest = gui.inventorySlots as ContainerChest
        val name = chest.getInventoryName()

        for (quest in dailyQuestHelper.quests) {
            val categoryName = quest.category.name
            if (!categoryName.equals(name, ignoreCase = true)) continue

            for (slot in chest.inventorySlots) {
                if (slot == null) continue
                if (slot.slotNumber != slot.slotIndex) continue

                // Only checking the middle slot
                if (slot.slotNumber != 22) continue

                val stack = slot.stack ?: continue

                val completed = stack.getLore().any { it.contains("Completed!") }
                if (completed) {
                    if (quest.state != QuestState.COLLECTED) {
                        quest.state = QuestState.COLLECTED
                        dailyQuestHelper.update()
                    }
                }

                val accepted = !stack.getLore().any { it.contains("Click to start!") }
                if (accepted) {
                    if (quest.state == QuestState.NOT_ACCEPTED) {
                        quest.state = QuestState.ACCEPTED
                        dailyQuestHelper.update()
                    }
                }
            }
        }
    }

    fun loadConfig() {
        for (text in SkyHanniMod.feature.hidden.crimsonIsleQuests) {
            val split = text.split(":")
            val name = split[0]
            val state = QuestState.valueOf(split[1])
            val needAmount = split[2].toInt()
            val quest = addQuest(name, state, needAmount)
            if (quest is ProgressQuest) {
                if (split.size == 4) {
                    try {
                        val haveAmount = split[3].toInt()
                        quest.haveAmount = haveAmount
                    } catch (e: IndexOutOfBoundsException) {
                        println("text: '$text'")
                        e.printStackTrace()
                    }
                }
            }
            dailyQuestHelper.quests.add(quest)
        }
    }
}