aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt
blob: 35741622a56a4c5acc6dbc58e1b82ac0f94c897b (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
package at.hannibal2.skyhanni.features.bingo

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.hasEnchantments
import at.hannibal2.skyhanni.utils.ItemUtils.itemName
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NEUItems.getCachedIngredients
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds

class MinionCraftHelper {

    private val config get() = SkyHanniMod.feature.event.bingo

    private val minionNamePattern by RepoPattern.pattern(
        "bingo.minion.name",
        "(?<name>.*) Minion (?<number>.*)"
    )

    private var display = emptyList<String>()
    private var hasMinionInInventory = false
    private var hasItemsForMinion = false
    private val tierOneMinions = mutableListOf<NEUInternalName>()
    private val tierOneMinionsDone get() = BingoAPI.bingoStorage.tierOneMinionsDone
    private val allIngredients = mutableListOf<NEUInternalName>()
    private val alreadyNotified = mutableListOf<String>()

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        alreadyNotified.clear()
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.isBingoProfile) return
        if (!config.minionCraftHelperEnabled) return

        if (event.isMod(10)) {
            val mainInventory = Minecraft.getMinecraft()?.thePlayer?.inventory?.mainInventory ?: return
            hasMinionInInventory = mainInventory.mapNotNull { it?.name }.any { isMinionName(it) }
        }

        if (event.repeatSeconds(2)) {
            val mainInventory = Minecraft.getMinecraft()?.thePlayer?.inventory?.mainInventory ?: return
            hasItemsForMinion = loadFromInventory(mainInventory).first.isNotEmpty()
        }

        if (!hasMinionInInventory && !hasItemsForMinion) {
            display = emptyList()
            return
        }

        if (!event.isMod(3)) return

        val mainInventory = Minecraft.getMinecraft()?.thePlayer?.inventory?.mainInventory ?: return

        val (minions, otherItems) = loadFromInventory(mainInventory)

        display = drawDisplay(minions, otherItems)
    }

    private fun drawDisplay(
        minions: MutableMap<String, NEUInternalName>,
        otherItems: MutableMap<NEUInternalName, Int>,
    ): MutableList<String> {
        val newDisplay = mutableListOf<String>()
        for ((minionName, minionId) in minions) {
            minionNamePattern.matchMatcher(minionName) {
                val cleanName = group("name").removeColor()
                val number = group("number").romanToDecimalIfNecessary()
                addMinion(cleanName, number, minionId, otherItems, newDisplay)
            }
        }
        return newDisplay
    }

    private fun loadFromInventory(mainInventory: Array<ItemStack?>): Pair<MutableMap<String, NEUInternalName>, MutableMap<NEUInternalName, Int>> {
        init()

        val minions = mutableMapOf<String, NEUInternalName>()
        val otherItems = mutableMapOf<NEUInternalName, Int>()

        for (item in mainInventory) {
            val name = item?.name?.removeColor() ?: continue
            val rawId = item.getInternalName()
            if (isMinionName(name)) {
                minions[name] = rawId
            }
        }

        val allMinions = tierOneMinions.toMutableList()
        minions.values.mapTo(allMinions) { it.addOneToId() }

        for (item in mainInventory) {
            val name = item?.name?.removeColor() ?: continue
            if (item.hasEnchantments()) continue
            val rawId = item.getInternalName()
            if (!isMinionName(name)) {
                if (!allIngredients.contains(rawId)) continue
                if (!isAllowed(allMinions, rawId)) continue

                val (itemId, multiplier) = NEUItems.getMultiplier(rawId)
                val old = otherItems.getOrDefault(itemId, 0)
                otherItems[itemId] = old + item.stackSize * multiplier
            }
        }

        FirstMinionTier.firstMinionTier(otherItems, minions, tierOneMinions, tierOneMinionsDone)
        return Pair(minions, otherItems)
    }

    private fun isAllowed(allMinions: List<NEUInternalName>, internalName: NEUInternalName): Boolean {
        val a = NEUItems.getMultiplier(internalName)
        for (minion in allMinions) {
            val recipes = NEUItems.getRecipes(minion)

            for (recipe in recipes) {
                for (ingredient in recipe.getCachedIngredients()) {
                    val ingredientInternalName = ingredient.internalItemId.asInternalName()
                    if (ingredientInternalName == internalName) return true

                    val b = NEUItems.getMultiplier(ingredientInternalName)
                    if (a.first == b.first && a.second < b.second) return true
                }
            }
        }
        return false
    }

    private fun init() {
        if (tierOneMinions.isNotEmpty()) return

        allIngredients.clear()

        for (internalId in NEUItems.allNeuRepoItems().keys) {
            val internalName = internalId.asInternalName()
            if (internalName.endsWith("_GENERATOR_1")) {
                if (internalName == "REVENANT_GENERATOR_1".asInternalName() ||
                    internalName == "TARANTULA_GENERATOR_1".asInternalName() ||
                    internalName == "VOIDLING_GENERATOR_1".asInternalName() ||
                    internalName == "INFERNO_GENERATOR_1".asInternalName() ||
                    internalName == "VAMPIRE_GENERATOR_1".asInternalName()
                ) continue
                tierOneMinions.add(internalName)
            }

            if (internalName.contains("_GENERATOR_")) {
                for (recipe in NEUItems.getRecipes(internalName)) {
                    if (recipe !is CraftingRecipe) continue

                    for (ingredient in recipe.getCachedIngredients()) {
                        val id = ingredient.internalItemId.asInternalName()
                        if (!id.contains("_GENERATOR_") && !allIngredients.contains(id)) {
                            allIngredients.add(id)
                        }
                    }
                }
            }
        }
    }

    private fun addMinion(
        name: String,
        minionTier: Int,
        minionId: NEUInternalName,
        otherItems: MutableMap<NEUInternalName, Int>,
        newDisplay: MutableList<String>,
    ) {
        val nextTier = minionTier + 1
        val minionName = "§9$name Minion $nextTier"
        newDisplay.add(minionName)
        val nextMinionId = minionId.addOneToId()
        for (recipe in NEUItems.getRecipes(nextMinionId)) {
            if (recipe !is CraftingRecipe) continue
            val output = recipe.output
            val internalItemId = output.internalItemId.asInternalName()
            if (!internalItemId.contains("_GENERATOR_")) continue
            val map = mutableMapOf<NEUInternalName, Int>()
            for (input in recipe.inputs) {
                val itemId =