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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.events.SeaCreatureFishEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.jsonobjects.SeaCreatureJson
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class SeaCreatureManager {

    private var doubleHook = false

    @SubscribeEvent
    fun onChatMessage(event: LorenzChatEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (doubleHookMessages.contains(event.message)) {
            if (SkyHanniMod.feature.fishing.compactDoubleHook) {
                event.blockedReason = "double_hook"
            }
            doubleHook = true
        } else {
            val seaCreature = getSeaCreature(event.message)
            if (seaCreature != null) {
                SeaCreatureFishEvent(seaCreature, event, doubleHook).postAndCatch()
            }
            doubleHook = false
        }
    }

    @SubscribeEvent
    fun onRepoReload(event: RepositoryReloadEvent) {
        seaCreatureMap.clear()
        allFishingMobs.clear()
        var counter = 0

        try {
            val data = event.getConstant<Map<String, SeaCreatureJson.Variant>>("SeaCreatures", SeaCreatureJson.TYPE) ?: return
            val allFishingMobs = mutableMapOf<String,SeaCreature>()

            for (variant in data.values) {
                val chatColor = variant.chat_color
                for ((displayName, seaCreature) in variant.sea_creatures) {
                    val chatMessage = seaCreature.chat_message
                    val fishingExperience = seaCreature.fishing_experience
                    val rarity = seaCreature.rarity
                    val rare = seaCreature.rare ?: false

                    val creature = SeaCreature(displayName, fishingExperience, chatColor, rare, rarity)
                    seaCreatureMap[chatMessage] = creature
                    allFishingMobs[displayName] = creature
                    counter++
                }
            }
            SeaCreatureManager.allFishingMobs = allFishingMobs
            LorenzUtils.debug("Loaded $counter sea creatures from repo")

        } catch (e: Exception) {
            e.printStackTrace()
            LorenzUtils.error("error in RepositoryReloadEvent")
        }
    }

    companion object {
        private val seaCreatureMap = mutableMapOf<String, SeaCreature>()
        var allFishingMobs = mutableMapOf<String, SeaCreature>()

        private val doubleHookMessages = setOf(
            "§eIt's a §r§aDouble Hook§r§e! Woot woot!",
            "§eIt's a §r§aDouble Hook§r§e!"
        )

        fun getSeaCreature(message: String): SeaCreature? {
            return seaCreatureMap.getOrDefault(message, null)
        }
    }
}