summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/inventory/MaxPurseItems.kt
blob: 517cf4b593464d46a9027aff12de6e5299a77fba (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
package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.PurseAPI
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.features.inventory.bazaar.BazaarApi
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble
import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object MaxPurseItems {
    private val config get() = SkyHanniMod.feature.inventory.bazaar

    private val patternGroup = RepoPattern.group("inventory.maxpurse")
    private val orderPattern by patternGroup.pattern(
        "order",
        ".*§6(?<coins>[\\d.,]+) coins §7each.*"
    )
    private val instantPattern by patternGroup.pattern(
        "instant",
        ".*Price per unit: §6(?<coins>[\\d.,]+) coins.*"
    )
    private val createOrderPattern by patternGroup.pattern(
        "createorder",
        "§aCreate Buy Order"
    )
    private val createInstantPattern by patternGroup.pattern(
        "createinstant",
        "§aBuy Instantly"
    )

    private var buyOrderPrice: Double? = null
    private var instantBuyPrice: Double? = null

    private fun getPrices() {
        for (item in Minecraft.getMinecraft().thePlayer.openContainer.inventory) {
            val name = item?.displayName ?: continue
            createOrderPattern.matchMatcher(name) {
                item.getLore().matchFirst(orderPattern) {
                    // +0.1 because I expect people to use the gold nugget option
                    buyOrderPrice = group("coins").formatDouble() + 0.1
                    // If we get to this point, we have the instant price because instant is earlier in the list of items
                    // So we can return
                    return
                }
            }
            createInstantPattern.matchMatcher(name) {
                item.getLore().matchFirst(instantPattern) {
                    instantBuyPrice = group("coins").formatDouble()
                }
            }
        }
    }

    @SubscribeEvent
    fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
        if (!isEnabled()) return
        if (!BazaarApi.inBazaarInventory) return
        // I would use BazaarAPI for price info, but as soon as NEU's data goes out of date, it will be wrong
        if (BazaarApi.currentlyOpenedProduct == null) {
            buyOrderPrice = null
            instantBuyPrice = null
            return
        }
        if (buyOrderPrice == null && instantBuyPrice == null) {
            getPrices()
        }

        val currentPurse = PurseAPI.getPurse()
        val buyOrders = buyOrderPrice?.let {
            (currentPurse / it).toInt()
        } ?: 0
        val buyInstant = instantBuyPrice?.let {
            (currentPurse / it).toInt()
        } ?: 0

        config.maxPurseItemsPosition.renderStrings(
            listOf(
                "§7Max items with purse",
                "§7Buy order +0.1: §e${buyOrders.addSeparators()}x",
                "§7Instant buy: §e${buyInstant.addSeparators()}x"
            ), posLabel = "Max Items With Purse"
        )
    }

    fun isEnabled(): Boolean {
        return LorenzUtils.inSkyBlock && config.maxPurseItems
    }
}