aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt
blob: 2b749c3a69508ab29ac92021a982a7930509d402 (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
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.CollectionAPI
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
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.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.NumberUtil.isFormatNumber
import at.hannibal2.skyhanni.utils.NumberUtil.percentWithColorCode
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.Collections

class CollectionTracker {

    private val RECENT_GAIN_TIME = 1_500

    companion object {

        private var display = emptyList<List<Any>>()

        private var itemName = ""
        private var internalName: NEUInternalName? = null
        private var itemAmount = -1L
        private var goalAmount = -1L

        private var lastAmountInInventory = -1

        private var recentGain = 0
        private var lastGainTime = -1L

        fun command(args: Array<String>) {
            if (args.isEmpty()) {
                if (internalName == null) {
                    ChatUtils.userError("/shtrackcollection <item name> [goal amount]")
                    return
                }
                ChatUtils.chat("Stopped collection tracker.")
                resetData()
                return
            }

            val lastArg = args.last()

            val nameArgs = if (lastArg.isFormatNumber()) {
                val goal = lastArg.formatLong()
                if (goal <= 0) {
                    ChatUtils.chat("Invalid Amount for Goal.")
                    return
                }
                goalAmount = goal
                args.dropLast(1).toTypedArray()
            } else {
                goalAmount = -1L
                args
            }

            val rawName = fixTypo(nameArgs.joinToString(" ").lowercase().replace("_", " "))
            if (rawName == "gemstone") {
                ChatUtils.userError("Gemstone collection is not supported!")
                return
            } else if (rawName == "mushroom") {
                ChatUtils.userError("Mushroom collection is not supported!")
                return
            }

            val foundInternalName = NEUInternalName.fromItemNameOrNull(rawName)
            if (foundInternalName == null) {
                ChatUtils.userError("Item '$rawName' does not exist!")
                return
            }

            val stack = foundInternalName.getItemStackOrNull()
            if (stack == null) {
                ChatUtils.userError("Item '$rawName' does not exist!")
                return
            }
            setNewCollection(foundInternalName, stack.name.removeColor())
        }

        private fun fixTypo(rawName: String) = when (rawName) {
            "carrots" -> "carrot"
            "melons" -> "melon"
            "seed" -> "seeds"
            "iron" -> "iron ingot"
            "gold" -> "gold ingot"
            "sugar" -> "sugar cane"
            "cocoa bean", "cocoa" -> "cocoa beans"
            "lapis" -> "lapis lazuli"
            "cacti" -> "cactus"
            "pumpkins" -> "pumpkin"
            "potatoes" -> "potato"
            "nether warts", "wart", "warts" -> "nether wart"
            "stone" -> "cobblestone"
            "red mushroom", "brown mushroom", "mushrooms" -> "mushroom"
            "gemstones" -> "gemstone"
            "caducous" -> "caducous stem"
            "agaricus" -> "agaricus cap"
            "quartz" -> "nether quartz"
            "glowstone" -> "glowstone dust"

            else -> rawName
        }

        private fun setNewCollection(internalName: NEUInternalName, name: String) {
            val foundAmount = CollectionAPI.getCollectionCounter(internalName)
            if (foundAmount == null) {
                ChatUtils.userError("$name collection not found. Try to open the collection inventory!")
                return
            }
            this.internalName = internalName
            itemName = name
            itemAmount = foundAmount

            lastAmountInInventory = countCurrentlyInInventory()
            updateDisplay()
            ChatUtils.chat("Started tracking $itemName §ecollection.")
        }

        private fun resetData() {
            itemAmount = -1L
            goalAmount = -1L
            internalName = null

            lastAmountInInventory = -1
            display = emptyList()

            recentGain = 0
        }

        private fun updateDisplay() {
            val format = itemAmount.addSeparators()

            var gainText = ""
            if (recentGain != 0) {
                gainText = "§a+" + recentGain.addSeparators()
            }

            if (goalAmount != -1L && itemAmount >= goalAmount) {
                ChatUtils.chat("Collection goal of §a${goalAmount.addSeparators()} reached!")
                goalAmount = -1L
            }

            val goal = if (goalAmount == -1L) "" else " §f/ §b${goalAmount.addSeparators()} §f(§a${
                itemAmount.percentWithColorCode(goalAmount, 1)
            }§f)"

            display = Collections.singletonList(buildList {
                internalName?.let {
                    add(it.getItemStack())
                }
                add("$itemName collection: §e$format$goal $gainText")
            })
        }

        private fun countCurrentlyInInventory(): Int {
            val cactus = "CACTUS".asInternalName()
            val cactusGreen = "INK_SACK-2".asInternalName()
            return InventoryUtils.countItemsInLowerInventory {
                if (internalName == cactus && it.getInternalName() == cactusGreen) {
                    return@countItemsInLowerInventory true
                }
                it.getInternalName() == internalName
            }
        }

        fun handleTabComplete(command: String): List<String>? {
            if (command != "shtrackcollection") return null

            return CollectionAPI.collectionValue.keys.mapNotNull { it.getItemStackOrNull() }
                .map { it.displayName.removeColor().replace(" ", "_") }
        }
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        val thePlayer = Minecraft.getMinecraft().thePlayer ?: return
        thePlayer.worldObj ?: return

        compareInventory()
        updateGain()
    }

    private fun compareInventory() {
        if (lastAmountInInventory == -1) return
        if (Minecraft.getMinecraft().currentScreen != null) return

        val currentlyInInventory = countCurrentlyInInventory()
        val diff = currentlyInInventory - lastAmountInInventory
        if (diff != 0 && diff > 0) {
            gainItems(diff)
        }

        lastAmountInInventory = currentlyInInventory
    }

    private fun updateGain() {
        if (recentGain != 0 && System.currentTimeMillis() > lastGainTime + RECENT_GAIN_TIME) {
            recentGain = 0
            updateDisplay()
        }
    }

    private fun gainItems(amount: Int) {
        itemAmount += amount

        if (System.currentTimeMillis() > lastGainTime + RECENT_GAIN_TIME) {
            recentGain = 0
        }
        lastGainTime = System.currentTimeMillis()
        recentGain += amount

        updateDisplay()
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) {
        if (!LorenzUtils.inSkyBlock) return

        SkyHanniMod.feature.misc.collectionCounterPos.renderStringsAndItems(display, posLabel = "Collection Tracker")
    }
}