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

import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.features.bazaar.BazaarApi
import at.hannibal2.skyhanni.features.slayer.SlayerType
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.nameWithEnchantment
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.nextAfter
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import com.google.common.cache.CacheBuilder
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit

object SlayerAPI {

    private var nameCache =
        CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build<Pair<String, Int>, Pair<String, Double>>()

    var questStartTime = 0L
    var isInSlayerArea = false
    var latestSlayerCategory = ""
    private var latestProgressChangeTime = 0L
    var latestWrongAreaWarning = 0L
    private var latestSlayerProgress = ""

    fun hasActiveSlayerQuest() = latestSlayerCategory != ""

    fun getLatestProgressChangeTime() = if (latestSlayerProgress == "§eSlay the boss!") {
        System.currentTimeMillis()
    } else latestProgressChangeTime


    // TODO use repo
    fun ignoreSlayerDrop(name: String) = when (name.removeColor()) {
        // maybe everywhere?
        "Stone" -> true
        "Head" -> true

        // Spider
        "Cobweb" -> true
        "String" -> true
        "Spider Eye" -> true
        "Bone" -> true

        // Blaze
        "Water Bottle" -> true

        else -> false
    }

    fun getItemNameAndPrice(stack: ItemStack): Pair<String, Double> {
        val internalName = stack.getInternalName()
        val amount = stack.stackSize
        val key = internalName to amount
        nameCache.getIfPresent(key)?.let {
            return it
        }

        val amountFormat = if (amount != 1) "§7${amount}x §r" else ""
        val displayName = getNameWithEnchantmentFor(internalName)

        val price = NEUItems.getPrice(internalName)
        val npcPrice = BazaarApi.getBazaarDataByInternalName(internalName)?.npcPrice ?: 0.0
        val maxPrice = npcPrice.coerceAtLeast(price)
        val totalPrice = maxPrice * amount

        val format = NumberUtil.format(totalPrice)
        val priceFormat = " §7(§6$format coins§7)"

        val result = "$amountFormat$displayName$priceFormat" to totalPrice
        nameCache.put(key, result)
        return result
    }

    fun getNameWithEnchantmentFor(internalName: String): String? {
        if (internalName == "WISP_POTION") {
            return "§fWisp's Ice-Flavored Water"
        }
        return NEUItems.getItemStack(internalName).nameWithEnchantment
    }

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!LorenzUtils.inSkyBlock) return

        if (event.message.contains("§r§5§lSLAYER QUEST STARTED!")) {
            questStartTime = System.currentTimeMillis()
        }

        if (event.message == "  §r§a§lSLAYER QUEST COMPLETE!") {
            SlayerQuestCompleteEvent().postAndCatch()
        }
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.inSkyBlock) return

        // wait with sending SlayerChangeEvent until profile is detected
        if (ProfileStorageData.profileSpecific == null) return

        val slayerQuest = ScoreboardData.sidebarLinesFormatted.nextAfter("Slayer Quest") ?: ""
        if (slayerQuest != latestSlayerCategory) {
            val old = latestSlayerCategory
            latestSlayerCategory = slayerQuest
            SlayerChangeEvent(old, latestSlayerCategory).postAndCatch()
        }

        val slayerProgress = ScoreboardData.sidebarLinesFormatted.nextAfter("Slayer Quest", 2) ?: ""
        if (latestSlayerProgress != slayerProgress) {
            SlayerProgressChangeEvent(latestSlayerProgress, slayerProgress).postAndCatch()
            latestSlayerProgress = slayerProgress
            latestProgressChangeTime = System.currentTimeMillis()
        }

        if (event.isMod(5)) {
            isInSlayerArea = SlayerType.getByArea(LorenzUtils.skyBlockArea) != null
        }
    }
}