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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.TitleUtils
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNeeded
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.regex.Pattern

class MinionCraftHelper {
    private var minionNamePattern = Pattern.compile("(.*) Minion (.*)")
    private var tick = 0
    private var display = listOf<String>()
    private var hasMinionInInventory = false
    private var hasItemsForMinion = false
    private val tierOneMinions = mutableListOf<String>()
    private val tierOneMinionsDone = mutableListOf<String>()
    private val allIngredients = mutableListOf<String>()
    private val alreadyNotified = mutableListOf<String>()

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

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (event.phase != TickEvent.Phase.START) return
        if (!LorenzUtils.isBingoProfile) return
        if (!SkyHanniMod.feature.bingo.minionCraftHelperEnabled) return

        tick++

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

        if (tick % (60 * 2) == 0) {
            val mainInventory = Minecraft.getMinecraft()?.thePlayer?.inventory?.mainInventory ?: return
            hasItemsForMinion = loadFromInventory(mainInventory).first.isNotEmpty()
        }

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

        if (tick % 3 != 0) return
//        if (tick % 60 != 0) return

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

        val (minions, otherItems) = loadFromInventory(mainInventory)

        display = drawDisplay(minions, otherItems)
    }

    private fun drawDisplay(
        minions: MutableMap<String, String>,
        otherItems: MutableMap<String, Int>,
    ): MutableList<String> {
        val newDisplay = mutableListOf<String>()
        for ((minionName, minionId) in minions) {
            val matcher = minionNamePattern.matcher(minionName)
            if (!matcher.matches()) continue
            val cleanName = matcher.group(1).removeColor()
            val number = matcher.group(2).romanToDecimalIfNeeded()
            addMinion(cleanName, number, minionId, otherItems, newDisplay)
        }
        return newDisplay
    }

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

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

        val minions = mutableMapOf<String, String>()
        val otherItems = mutableMapOf<String, 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
            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(otherItems, minions)
        return Pair(minions, otherItems)
    }

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

            for (recipe in recipes) {
                for (ingredient in recipe.ingredients) {
                    val ingredientInternalName = ingredient.internalItemId
                    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 NotEnoughUpdates.INSTANCE.manager.itemInformation.keys) {
            if (internalId.endsWith("_GENERATOR_1")) {
                if (internalId == "REVENANT_GENERATOR_1") continue
                if (internalId == "TARANTULA_GENERATOR_1") continue
                if (internalId == "VOIDLING_GENERATOR_1") continue
                tierOneMinions.add(internalId)
            }

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

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

    private fun firstMinionTier(otherItems: Map<String, Int>, minions: MutableMap<String, String>) {
        val help = otherItems.filter { !it.key.startsWith("WOOD_") }
        val tierOneMinionsFiltered = tierOneMinions.filter { it !in tierOneMinionsDone }
        for (minionId in tierOneMinionsFiltered) {
            val prefix = minionId.dropLast(1)
            if (minions.any { it.value.startsWith(prefix) }) {
                tierOneMinionsDone.add(minionId)
            }
        }
        for (minionId in tierOneMinionsFiltered) {
            for (recipe in NEUItems.getRecipes(minionId)) {
                if (recipe !is CraftingRecipe) continue
                if (recipe.ingredients.any { help.contains(it.internalItemId) }) {
                    val name = recipe.output.itemStack.name!!.removeColor()
                    val abc = name.replace(" I", " 0")
                    minions[abc] = minionId.replace("_1", "_0")
                }