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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import at.hannibal2.skyhanni.utils.SoundUtils.playSound
import at.hannibal2.skyhanni.utils.StringUtils
import net.minecraft.client.Minecraft
import net.minecraft.client.audio.ISound
import net.minecraft.client.audio.PositionedSound
import net.minecraft.entity.item.EntityArmorStand
import net.minecraft.util.ResourceLocation
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class BarnFishingTimer {

    private val barnLocation = LorenzVec(108, 89, -252)

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

    private var sound = object : PositionedSound(ResourceLocation("random.orb")) {
        init {
            volume = 50f
            repeat = false
            repeatDelay = 0
            attenuationType = ISound.AttenuationType.NONE
        }
    }

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (event.phase != TickEvent.Phase.START) return

        if (!LorenzUtils.inSkyBlock) return
        if (!SkyHanniMod.feature.fishing.barnTimer) return

        tick++

        if (tick % 60 == 0) checkIsland()

        if (!rightLocation) return

        if (tick % 5 == 0) checkMobs()
        if (tick % 7 == 0) tryPlaySound()
    }

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

        val duration = System.currentTimeMillis() - startTime
        val barnTimerAlertTime = SkyHanniMod.feature.fishing.barnTimerAlertTime * 1_000
        if (duration > barnTimerAlertTime && duration < barnTimerAlertTime + 3_000) {
            sound.playSound()
        }
    }

    private fun checkMobs() {
        val newCount = countMobs()
        if (currentCount == 0) {
            if (newCount > 0) {
                startTime = System.currentTimeMillis()
            }
        }

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

    private fun countMobs() = Minecraft.getMinecraft().theWorld.loadedEntityList
        .filterIsInstance<EntityArmorStand>()
        .map { it.name }
        .count { it.endsWith("§c❤") }

    private fun checkIsland() {
        if (LorenzUtils.skyBlockIsland == IslandType.THE_FARMING_ISLANDS) {
            rightLocation = false
            return
        }

        rightLocation = LocationUtils.playerLocation().distance(barnLocation) < 50
    }

    @SubscribeEvent
    fun onRenderOverlay(event: RenderGameOverlayEvent.Post) {
        if (event.type != RenderGameOverlayEvent.ElementType.ALL) return
        if (!LorenzUtils.inSkyBlock) return
        if (!SkyHanniMod.feature.fishing.barnTimer) return
        if (!rightLocation) return
        if (currentCount == 0) return

        val duration = System.currentTimeMillis() - startTime
        val barnTimerAlertTime = SkyHanniMod.feature.fishing.barnTimerAlertTime * 1_000
        val color = if (duration > barnTimerAlertTime) "§c" else "§e"
        val timeFormat = StringUtils.formatDuration(duration / 1000, decimalFormat = true)
        val name = if (currentCount == 1) "sea creature" else "sea creatures"
        val text = "$color$timeFormat §8(§e$currentCount §b$name§8)"

        SkyHanniMod.feature.fishing.barnTimerPos.renderString(text)
    }
}