aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/ProfileStorageData.kt
blob: 9c2fff90dcb80294df8921a335284dd26830aa62 (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
175
176
177
178
179
180
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.Storage
import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object ProfileStorageData {
    var playerSpecific: Storage.PlayerSpecific? = null
    var profileSpecific: Storage.ProfileSpecific? = null
    var loaded = false
    var noTabListTime = -1L

    private var nextProfile: String? = null

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    fun onChat(event: LorenzChatEvent) {
        "§7Switching to profile (?<name>.*)\\.\\.\\.".toPattern().matchMatcher(event.message) {
            nextProfile = group("name").lowercase()
            loaded = false
            PreProfileSwitchEvent().postAndCatch()
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        val profileName = nextProfile ?: return
        nextProfile = null

        val playerSpecific = playerSpecific
        if (playerSpecific == null) {
            LorenzUtils.error("profileSpecific after profile swap can not be set: playerSpecific is null!")
            return
        }
        loadProfileSpecific(playerSpecific, profileName, "profile swap (chat message)")
        ConfigLoadEvent().postAndCatch()
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    fun onProfileJoin(event: ProfileJoinEvent) {
        val playerSpecific = playerSpecific
        if (playerSpecific == null) {
            LorenzUtils.error("playerSpecific is null in ProfileJoinEvent!")
            return
        }

        if (profileSpecific == null) {
            val profileName = event.name
            loadProfileSpecific(playerSpecific, profileName, "first join (chat message)")
        }
    }

    @SubscribeEvent
    fun onTabListUpdate(event: TabListUpdateEvent) {
        if (profileSpecific != null) return
        val playerSpecific = playerSpecific ?: return
        for (line in event.tabList) {
            val pattern = "§e§lProfile: §r§a(?<name>.*)".toPattern()
            pattern.matchMatcher(line) {
                val profileName = group("name").lowercase()
                loadProfileSpecific(playerSpecific, profileName, "tab list")
                nextProfile = null
                return
            }
        }

        if (LorenzUtils.inSkyBlock) {
            noTabListTime = System.currentTimeMillis()
        }
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (noTabListTime == -1L) return

        if (System.currentTimeMillis() > noTabListTime + 3_000) {
            noTabListTime = System.currentTimeMillis()
            LorenzUtils.chat(
                "§c[SkyHanni] Extra Information from Tab list not found! " +
                        "Enable it: SkyBlock Menu ➜ Settings ➜ Personal ➜ User Interface ➜ Player List Info"
            )
        }
    }

    private fun loadProfileSpecific(playerSpecific: Storage.PlayerSpecific, profileName: String, reason: String) {
        noTabListTime = -1
        profileSpecific = playerSpecific.profiles.getOrPut(profileName) { Storage.ProfileSpecific() }
        tryMigrateProfileSpecific()
        ConfigLoadEvent().postAndCatch()
        loaded = true
    }

    @SubscribeEvent
    fun onHypixelJoin(event: HypixelJoinEvent) {
        val playerUuid = LorenzUtils.getRawPlayerUuid()
        playerSpecific = SkyHanniMod.feature.storage.players.getOrPut(playerUuid) { Storage.PlayerSpecific() }
        migratePlayerSpecific()
        ConfigLoadEvent().postAndCatch()
    }

    private fun migratePlayerSpecific() {
        val oldHidden = SkyHanniMod.feature.hidden
        if (oldHidden.isMigrated) return

        SkyHanniMod.feature.storage.apiKey = oldHidden.apiKey

        SkyHanniMod.feature.storage?.let {
            it.gardenJacobFarmingContestTimes = oldHidden.gardenJacobFarmingContestTimes
        }
    }

    private fun tryMigrateProfileSpecific() {
        val oldHidden = SkyHanniMod.feature.hidden
        if (oldHidden.isMigrated) return

        profileSpecific?.let {
            it.currentPet = oldHidden.currentPet

            for ((rawLocation, minionName) in oldHidden.minionName) {
                val lastClick = oldHidden.minionLastClick[rawLocation] ?: -1
                val location = LorenzVec.decodeFromString(rawLocation)
                val minionConfig = Storage.ProfileSpecific.MinionConfig()
                minionConfig.displayName = minionName
                minionConfig.lastClicked = lastClick
                it.minions[location] = minionConfig
            }
        }

        profileSpecific?.crimsonIsle?.let {
            it.quests = oldHidden.crimsonIsleQuests
            it.latestTrophyFishInInventory = oldHidden.crimsonIsleLatestTrophyFishInInventory
            it.miniBossesDoneToday = oldHidden.crimsonIsleMiniBossesDoneToday
            it.kuudraTiersDone = oldHidden.crimsonIsleKuudraTiersDone
        }

        profileSpecific?.garden?.let {
            it.experience = oldHidden.gardenExp
            it.cropCounter = oldHidden.gardenCropCounter
            it.cropUpgrades = oldHidden.gardenCropUpgrades

            for ((crop, speed) in oldHidden.gardenCropsPerSecond) {
                if (speed != -1) {
                    it.cropsPerSecond[crop] = speed
                }
            }

            it.latestBlocksPerSecond = oldHidden.gardenLatestBlocksPerSecond
            it.latestTrueFarmingFortune = oldHidden.gardenLatestTrueFarmingFortune
            it.savedCropAccessory = oldHidden.savedCropAccessory
            it.dicerRngDrops = oldHidden.gardenDicerRngDrops
            it.informedAboutLowMatter = oldHidden.informedAboutLowMatter
            it.informedAboutLowFuel = oldHidden.informedAboutLowFuel
            it.visitorInterval = oldHidden.visitorInterval
            it.nextSixthVisitorArrival = oldHidden.nextSixthVisitorArrival
            it.farmArmorDrops = oldHidden.gardenFarmingArmorDrops
            it.composterUpgrades = oldHidden.gardenComposterUpgrades
            it.toolWithBountiful = oldHidden.gardenToolHasBountiful
            it.composterCurrentOrganicMatterItem = oldHidden.gardenComposterCurrentOrganicMatterItem
            it.composterCurrentFuelItem = oldHidden.gardenComposterCurrentFuelItem
        }

        profileSpecific?.garden?.visitorDrops?.let {
            val old = oldHidden.visitorDrops
            it.acceptedVisitors = old.acceptedVisitors
            it.deniedVisitors = old.deniedVisitors
            it.visitorRarities = old.visitorRarities
            it.copper = old.copper
            it.farmingExp = old.farmingExp
            it.coinsSpent = old.coinsSpent
            it.rewardsCount = old.rewardsCount
        }

        oldHidden.isMigrated = true
    }
}