aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionCounter.kt
blob: 6d8003f7cf74e11adab00f93eabaa8db532dabbf (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
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.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class CollectionCounter {

    private val RECENT_GAIN_TIME = 1_500

    companion object {

        private var display = ""

        private var itemName = ""
        private var itemApiName = ""
        private var itemAmount = -1L

        private var lastAmountInInventory = -1

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

        fun command(args: Array<String>) {
            if (args.isEmpty()) {
                if (itemName == "") {
                    LorenzUtils.chat("§c/shtrackcollection <item name>")
                    return
                }
                LorenzUtils.chat("§e[SkyHanni] Stopped collection tracker.")
                resetData()
                return
            }

            val name = args.joinToString(" ")
            val pair = CollectionAPI.getCollectionCounter(name)
            if (pair == null) {
                LorenzUtils.chat("§c[SkyHanni] Item $name is not in the collection data! (API disabled or open the collection inventory)")
                return
            }

            itemName = pair.first
            itemApiName = if (itemName == "Mushroom" || itemName == "Gemstone") {
                LorenzUtils.chat("§7Mushroom and Gemstone items are not fully supported for the counter!")
                ""
            } else {
                NEUItems.getInternalName(itemName)
            }

            itemAmount = pair.second

            lastAmountInInventory = countCurrentlyInInventory()
            updateDisplay()
            LorenzUtils.chat("§e[SkyHanni] Started tracking $itemName collection.")
        }

        private fun resetData() {
            itemAmount = -1
            itemName = ""
            itemApiName = ""

            lastAmountInInventory = -1
            display = ""

            recentGain = 0
        }

        private fun updateDisplay() {
            val format = LorenzUtils.formatInteger(itemAmount)

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

            display = "$itemName collection: §e$format $gainText"
        }

        private fun countCurrentlyInInventory() =
            InventoryUtils.countItemsInLowerInventory { it.getInternalName() == itemApiName }
    }

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        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) {
            if (diff > 0) {
                gainItems(diff)
            }
        }

        lastAmountInInventory = currentlyInInventory
    }

    private fun updateGain() {
        if (recentGain != 0) {
            if (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.GameOverlayRenderEvent) {
        if (!LorenzUtils.inSkyBlock) return

        SkyHanniMod.feature.misc.collectionCounterPos.renderString(display, posLabel = "Collection Counter")
    }
}