aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/HotmDesires.kt
blob: ef95eff3f901b2e587d719ba28c00521b8a12e26 (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
/*
 * Copyright (C) 2024 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.miscfeatures

import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.core.util.StringUtils
import io.github.moulberry.notenoughupdates.events.SlotClickEvent
import io.github.moulberry.notenoughupdates.events.TabListChangeEvent
import io.github.moulberry.notenoughupdates.miscfeatures.tablisttutorial.TablistAPI
import io.github.moulberry.notenoughupdates.util.ItemUtils
import io.github.moulberry.notenoughupdates.util.SBInfo
import io.github.moulberry.notenoughupdates.util.Utils
import io.github.moulberry.notenoughupdates.util.kotlin.KSerializable
import io.github.moulberry.notenoughupdates.util.kotlin.useMatcher
import net.minecraftforge.event.entity.player.ItemTooltipEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@NEUAutoSubscribe
object HotmDesires {

    val powderRequirementText = "§.([0-9,]+) .* Powder".toPattern()
    val youArePoorText = "§cYou don't have enough (.*) Powder!".toPattern()

    val lastPowder = mutableMapOf<String, Int>()

    @KSerializable
    data class Desire(
        val name: String,
        val powderRequirement: Int,
    )

    val desires: MutableMap<String, Desire>?
        get() = NotEnoughUpdates.INSTANCE.config.profileSpecific?.hotmDesires

    @JvmStatic
    fun appendDesireForType(powderType: String): String {
        val desire = desires?.get(powderType) ?: return ""
        return "§7/§c" + StringUtils.formatNumber(desire.powderRequirement)
    }

    val tablistPowderLine = " (.*): ([0-9,]+)".toPattern()

    @SubscribeEvent
    fun onTabListChange(event: TabListChangeEvent) {
        val desireMap = desires ?: return
        if (!isEnabled()) {
            desireMap.clear()
            return
        }
        if (SBInfo.getInstance().getLocation() !in setOf("mining_3", "crystal_hollows", "mineshaft")) {
            return
        }
        for (line in TablistAPI.getWidgetLines(TablistAPI.WidgetNames.POWDER)) {
            val (powderKind, powderCount) = tablistPowderLine.useMatcher(StringUtils.cleanColour(line)) {
                val powderKind = group(1)
                val powderCount = group(2).replace(",", "").toInt()
                powderKind to powderCount
            } ?: continue
            if (lastPowder[powderKind] == powderCount) continue
            lastPowder[powderKind] = powderCount
            val goal = desireMap[powderKind] ?: continue
            if (goal.powderRequirement > powderCount) continue
            desireMap.remove(powderKind)
            Utils.addClickableChatMessage(
                "§e[NEU] You have enough $powderKind powder to upgrade ${goal.name}§e!",
                "/hotm",
                "§eClick to open your Heart of the Mountain to select the next upgrade."
            )
        }
    }

    fun isEnabled() = NotEnoughUpdates.INSTANCE.config.mining.powderTodo

    @SubscribeEvent
    fun onClickHotmItemThatYouCannotUpgrade(event: SlotClickEvent) {
        if (Utils.getOpenChestName() != "Heart of the Mountain" || !isEnabled())
            return
        val name = ItemUtils.getDisplayName(event.slot.stack) ?: return
        val lore = ItemUtils.getLore(event.slot.stack)
        val missingPowderText = lore.lastOrNull() ?: return
        val powderKind = youArePoorText.useMatcher(missingPowderText) {
            group(1)
        } ?: return
        val powderCount =
            lore.firstNotNullOfOrNull { powderRequirementText.useMatcher(it) { group(1).replace(",", "").toInt() } }
                ?: return
        val desireMap = desires ?: return
        if (desireMap[powderKind]?.name != name) {
            desireMap[powderKind] = Desire(name, powderCount)
        } else {
            desireMap.remove(powderKind)
        }
    }


    @SubscribeEvent
    fun onAfterGuiDraw(event: ItemTooltipEvent) {
        if (Utils.getOpenChestName() != "Heart of the Mountain" || !isEnabled())
            return
        val name = ItemUtils.getDisplayName(event.itemStack) ?: return
        if (desires?.values?.any { it.name == name } != true) return
        event.toolTip.add("§e[NEU] Selected this perk as your next goal.")
        event.toolTip.add("§e[NEU] Click again to deselect.")
    }

    @JvmStatic
    fun wantsPowderInfo(): Boolean {
        return desires?.isNotEmpty() == true && isEnabled()
    }

}