aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingTimer.kt
blob: fce359672c55529a855d1114e29314508cf5d46e (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
package at.hannibal2.skyhanni.features.fishing

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.utils.*
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import net.minecraft.entity.item.EntityArmorStand
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class FishingTimer {
    private val config get() = SkyHanniMod.feature.fishing
    private val barnLocation = LorenzVec(108, 89, -252)

    private var rightLocation = false
    private var currentCount = 0
    private var startTime = 0L
    private var inHollows = false

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

        if (event.repeatSeconds(3)) {
            rightLocation = isRightLocation()
        }

        if (!rightLocation) return

        if (event.isMod(5)) checkMobs()
        if (event.isMod(7)) tryPlaySound()
        if (OSUtils.isKeyHeld(config.manualResetTimer)) startTime = System.currentTimeMillis()
    }

    private fun tryPlaySound() {
        if (currentCount == 0) return

        val duration = System.currentTimeMillis() - startTime
        val barnTimerAlertTime = config.barnTimerAlertTime * 1_000
        if (duration > barnTimerAlertTime && duration < barnTimerAlertTime + 3_000) {
            SoundUtils.playBeepSound()
        }
    }

    private fun checkMobs() {
        val newCount = countMobs()

        if (currentCount == 0 && newCount > 0) {
            startTime = System.currentTimeMillis()
        }

        currentCount = newCount
        if (newCount == 0) {
            startTime = 0
        }

        if (inHollows && newCount >= 60 && config.wormLimitAlert) {
            SoundUtils.playBeepSound()
        }
    }

    private fun countMobs() = EntityUtils.getEntities<EntityArmorStand>()
        .map { entity ->
            val name = entity.name
            val isSummonedSoul = name.contains("'")
            val hasFishingMobName = SeaCreatureManager.allFishingMobNames.any { name.contains(it) }
            if (hasFishingMobName && !isSummonedSoul) {
                if (name == "Sea Emperor" || name == "Rider of the Deep") 2 else 1
            } else 0
        }.sum()

    private fun isRightLocation(): Boolean {
        inHollows = false

        if (config.barnTimerForStranded && LorenzUtils.isStrandedProfile) return true

        if (config.barnTimerCrystalHollows && IslandType.CRYSTAL_HOLLOWS.isInIsland()) {
            inHollows = true
            return true
        }

        if (!IslandType.THE_FARMING_ISLANDS.isInIsland()) {
            return LocationUtils.playerLocation().distance(barnLocation) < 50
        }

        return false
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!config.barnTimer) return
        if (!rightLocation) return
        if (currentCount == 0) return

        val duration = System.currentTimeMillis() - startTime
        val barnTimerAlertTime = config.barnTimerAlertTime * 1_000
        val color = if (duration > barnTimerAlertTime) "§c" else "§e"
        val timeFormat = TimeUtils.formatDuration(duration, biggestUnit = TimeUnit.MINUTE)
        val name = if (currentCount == 1) "sea creature" else "sea creatures"
        val text = "$color$timeFormat §8(§e$currentCount §b$name§8)"

        config.barnTimerPos.renderString(text, posLabel = "BarnTimer")
    }
}