aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerQuestWarning.kt
blob: 389c68ce8c9a8c1262c8150b0ebc4710c908fa46 (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
package at.hannibal2.skyhanni.features.slayer

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.data.SlayerAPI
import at.hannibal2.skyhanni.data.TitleUtils
import at.hannibal2.skyhanni.events.EntityHealthUpdateEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.entity.EntityLivingBase
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class SlayerQuestWarning {
    private val config get() = SkyHanniMod.feature.slayer
    private var needSlayerQuest = false
    private var lastWarning = 0L
    private var currentReason = ""
    private var dirtySidebar = false
    private var tick = 0
    private var activeSlayer: SlayerType? = null

    //TODO add check if player has clicked on an item, before mobs around you gets damage

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

        val message = event.message

        //died
        if (message == "  §r§c§lSLAYER QUEST FAILED!") {
            needNewQuest("The old slayer quest has failed!")
        }
        if (message == "§eYour unsuccessful quest has been cleared out!") {
            needSlayerQuest = false
        }

        //no auto slayer
        if (message.matchRegex("   §r§5§l» §r§7Talk to Maddox to claim your (.+) Slayer XP!")) {
            needNewQuest("You have no Auto-Slayer active!")
        }
        if (message == "  §r§a§lSLAYER QUEST COMPLETE!") {
            needSlayerQuest = false
        }

        if (message == "§aYour Slayer Quest has been cancelled!") {
            activeSlayer = null
            needSlayerQuest = false
        }

        //TODO auto slayer disabled bc of no more money in bank or purse
    }

    private fun needNewQuest(reason: String) {
        currentReason = reason
        needSlayerQuest = true
    }

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

        if (dirtySidebar) {
            if (tick++ % 60 == 0) {
                checkSidebar()
            }
        }
    }

    private fun checkSidebar() {
        var loaded = false

        var slayerQuest = false
        var bossSlain = false
        var slayBoss = false
        var slayerTypeName = ""
        var nextIsType = false
        for (line in ScoreboardData.sidebarLinesFormatted) {
            if (nextIsType) {
                slayerTypeName = line.removeColor()
                nextIsType = false
            }
            if (line == "Slayer Quest") {
                slayerQuest = true
                nextIsType = true
            }
            if (line == "§aBoss slain!") {
                bossSlain = true
            }
            if (line == "§eSlay the boss!") {
                slayBoss = true
            }
            if (line == "§ewww.hypixel.net" || line == "§ewww.alpha.hypixel.net") {
                loaded = true
            }
        }

        activeSlayer = SlayerType.getByDisplayName(slayerTypeName)

        if (loaded) {
            dirtySidebar = false
            if (slayerQuest && !needSlayerQuest) {
                if (bossSlain) {
                    needNewQuest("You have no Auto-Slayer active!")
                } else if (slayBoss) {
                    needNewQuest("You probably switched the server during an active boss and now hypixel doesn't know what to do.")
                }
            }
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        if (!needSlayerQuest) {
            dirtySidebar = true
        }
    }

    private fun tryWarn() {
        if (!needSlayerQuest) return
        warn("New Slayer Quest!", "Start a new slayer quest! $currentReason")
    }

    private fun warn(titleMessage: String, chatMessage: String) {
        if (!config.questWarning) return
        if (lastWarning + 10_000 > System.currentTimeMillis()) return

        lastWarning = System.currentTimeMillis()
        LorenzUtils.chat("§e[SkyHanni] $chatMessage")

        if (config.questWarningTitle) {
            TitleUtils.sendTitle("§e$titleMessage", 2_000)
        }
    }

    @SubscribeEvent
    fun onEntityHealthUpdate(event: EntityHealthUpdateEvent) {
        if (!(LorenzUtils.inSkyBlock)) return

        val entity = event.entity
        if (entity.getLorenzVec().distanceToPlayer() < 6) {
            if (isSlayerMob(entity)) {
                tryWarn()
            }
        }
    }

    private fun isSlayerMob(entity: EntityLivingBase): Boolean {
        val area = LorenzUtils.skyBlockArea
        val slayerType = SlayerType.getByArea(area) ?: return false

        if (activeSlayer != null) {
            val activeSlayer = activeSlayer!!
            if (slayerType != activeSlayer) {
                val activeSlayerName = activeSlayer.displayName
                val slayerName = slayerType.displayName
                SlayerAPI.latestWrongAreaWarning = System.currentTimeMillis()
                warn(
                    "Wrong Slayer!",
                    "Wrong slayer selected! You have $activeSlayerName selected and are in the $slayerName area!"
                )
            }
        }

        return slayerType.clazz.isInstance(entity)
    }

}