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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.FishingBobberCastEvent
import at.hannibal2.skyhanni.events.FishingBobberInWaterEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.features.fishing.FishingAPI.isBait
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.SoundUtils
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.entity.item.EntityItem
import net.minecraft.entity.projectile.EntityFishHook
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

class FishingBaitWarnings {

    private val config get() = SkyHanniMod.feature.fishing.fishingBaitWarnings

    @SubscribeEvent
    fun onBobberThrow(event: FishingBobberCastEvent) {
    }

    private var lastBait: String? = null
    private var wasUsingBait = true

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        lastBait = null
        wasUsingBait = true
    }

    @SubscribeEvent
    fun onBobberInWater(event: FishingBobberInWaterEvent) {
        DelayedRun.runDelayed(500.milliseconds) {
            checkBobber()
        }
    }

    private fun checkBobber() {
        val bobber = FishingAPI.bobber ?: return
        val bait = detectBait(bobber)
        if (bait == null) {
            if (config.noBaitWarning) {
                if (!wasUsingBait) {
                    showNoBaitWarning()
                }
            }
        } else {
            if (config.baitChangeWarning) {
                lastBait?.let {
                    if (it != bait) {
                        showBaitChangeWarning(it, bait)
                    }
                }
            }
        }
        wasUsingBait = bait != null
        lastBait = bait
    }

    private fun detectBait(bobber: EntityFishHook): String? {
        for (entity in EntityUtils.getEntitiesNearby<EntityItem>(bobber.getLorenzVec(), 6.0)) {
            val itemStack = entity.entityItem ?: continue
            if (!itemStack.isBait()) continue
            val ticksExisted = entity.ticksExisted
            if (ticksExisted in 6..15) {
                return itemStack.name
            }

            val distance = "distance: ${entity.getDistanceToEntity(bobber).addSeparators()}"
            ChatUtils.debug("fishing bait: ticksExisted: $ticksExisted, $distance")
        }
        return null
    }

    private fun showBaitChangeWarning(before: String, after: String) {
        SoundUtils.playClickSound()
        LorenzUtils.sendTitle("§eBait changed!", 2.seconds)
        ChatUtils.chat("Fishing Bait changed: $before §e-> $after")
    }

    private fun showNoBaitWarning() {
        SoundUtils.playErrorSound()
        LorenzUtils.sendTitle("§cNo bait is used!", 2.seconds)
        ChatUtils.chat("You're not using any fishing baits!")
    }

    private fun isEnabled() = LorenzUtils.inSkyBlock && FishingAPI.isFishing() && !LorenzUtils.inKuudraFight
}