aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
blob: cb3b2103197eb7d2c3a760b1df66e4c072cd7e1b (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
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.PurseChangeCause
import at.hannibal2.skyhanni.events.PurseChangeEvent
import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class PurseAPI {
    // TODO USE SH-REPO
    private val pattern = "(Piggy|Purse): §6(?<coins>[\\d,]*).*".toPattern()
    private var currentPurse = 0.0
    private var inventoryCloseTime = 0L

    @SubscribeEvent
    fun onInventoryClose(event: InventoryCloseEvent) {
        inventoryCloseTime = System.currentTimeMillis()
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {

        for (line in ScoreboardData.sidebarLinesFormatted) {
            val newPurse = pattern.matchMatcher(line) {
                group("coins").formatNumber().toDouble()
            } ?: continue
            val diff = newPurse - currentPurse
            if (diff == 0.0) continue
            currentPurse = newPurse

            PurseChangeEvent(diff, getCause(diff)).postAndCatch()
        }
    }

    // TODO add more causes in the future (e.g. ah/bz/bank)
    private fun getCause(diff: Double): PurseChangeCause {
        if (diff > 0) {
            if (diff == 1.0) {
                return PurseChangeCause.GAIN_TALISMAN_OF_COINS
            }
            if (Minecraft.getMinecraft().currentScreen == null) {
                val timeDiff = System.currentTimeMillis() - inventoryCloseTime
                if (timeDiff > 2_000) {
                    return PurseChangeCause.GAIN_MOB_KILL
                }

            }
            return PurseChangeCause.GAIN_UNKNOWN
        } else {
            val timeDiff = System.currentTimeMillis() - SlayerAPI.questStartTime
            if (timeDiff < 1500) {
                return PurseChangeCause.LOSE_SLAYER_QUEST_STARTED
            }

            return PurseChangeCause.LOSE_UNKNOWN
        }
    }
}