summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/bingo/card/BingoCardDisplay.kt
blob: 94bd4f6dd8dc8a97962d4c41d8f17fc5f7b48fce (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package at.hannibal2.skyhanni.features.bingo.card

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.bingo.BingoCardUpdateEvent
import at.hannibal2.skyhanni.features.bingo.BingoAPI
import at.hannibal2.skyhanni.features.bingo.card.goals.BingoGoal
import at.hannibal2.skyhanni.features.bingo.card.nextstephelper.BingoNextStepHelper
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiChat
import net.minecraft.client.gui.inventory.GuiInventory
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.days

@SkyHanniModule
object BingoCardDisplay {

    private var display = emptyList<Renderable>()

    private var hasHiddenPersonalGoals = false

    private const val MAX_PERSONAL_GOALS = 20
    private const val MAX_COMMUNITY_GOALS = 5

    private val config get() = SkyHanniMod.feature.event.bingo.bingoCard
    private var displayMode = 0

    fun command() {
        reload()
    }

    private fun reload() {
        BingoAPI.bingoGoals.clear()
    }

    fun toggleCommand() {
        if (!LorenzUtils.isBingoProfile) {
            ChatUtils.userError("This command only works on a bingo profile!")
            return
        }
        if (!config.enabled) {
            ChatUtils.userError("Bingo Card is disabled in the config!")
            return
        }
        toggleMode()
    }

    private fun toggleMode() {
        displayMode++
        if (displayMode == 3) {
            displayMode = 0
        }
    }

    @SubscribeEvent
    fun onSecondPassed(event: SecondPassedEvent) {
        if (hasHiddenPersonalGoals) {
            update()
        }
    }

    private fun update() {
        display = drawDisplay()
    }

    private fun drawDisplay(): MutableList<Renderable> {
        val newList = mutableListOf<Renderable>()

        if (BingoAPI.bingoGoals.isEmpty()) {
            newList.add(Renderable.string("§6Bingo Goals:"))
            newList.add(Renderable.clickAndHover("§cOpen the §e/bingo §ccard.",
                listOf("Click to run §e/bingo"),
                onClick = {
                    HypixelCommands.bingo()
                }
            ))
        } else {
            if (!config.hideCommunityGoals.get()) {
                newList.addCommunityGoals()
            }
            newList.addPersonalGoals()
        }
        return newList
    }

    private fun MutableList<Renderable>.addCommunityGoals() {
        add(Renderable.string("§6Community Goals:"))
        val goals = BingoAPI.communityGoals.toMutableList()
        var hiddenGoals = 0
        for (goal in goals.toList()) {
            if (goal.hiddenGoalData.unknownTip) {
                hiddenGoals++
                goals.remove(goal)
            }
        }

        addGoals(goals) {
            val percentageFormat = percentageFormat(it)
            val name = it.description.removeColor()
            "$name$percentageFormat"
        }

        if (hiddenGoals > 0) {
            val name = StringUtils.pluralize(hiddenGoals, "goal")
            add(Renderable.string("§7+ $hiddenGoals more §cunknown §7community $name."))
        }
        add(Renderable.string(" "))
    }

    private fun percentageFormat(it: BingoGoal) = it.communtyGoalPercentage?.let {
        " " + BingoAPI.getCommunityPercentageColor(it)
    } ?: ""

    private fun MutableList<Renderable>.addPersonalGoals() {
        val todo = BingoAPI.personalGoals.filter { !it.done }.toMutableList()
        val done = MAX_PERSONAL_GOALS - todo.size
        add(Renderable.string("§6Personal Goals: ($done/$MAX_PERSONAL_GOALS done)"))

        var hiddenGoals = 0
        var nextTip = 14.days
        for (goal in todo.toList()) {
            val hiddenGoalData = goal.hiddenGoalData
            if (hiddenGoalData.unknownTip) {
                hiddenGoals++
                todo.remove(goal)
                hiddenGoalData.nextHintTime?.let {
                    if (it < nextTip) {
                        nextTip = it
                    }
                }
            }
        }

        addGoals(todo) { it.description.removeColor() }

        if (hiddenGoals > 0) {
            val name = StringUtils.pluralize(hiddenGoals, "goal")
            add(Renderable.string("§7+ $hiddenGoals more §cunknown §7$name."))
        }
        hasHiddenPersonalGoals = config.nextTipDuration.get() && nextTip != 14.days
        if (hasHiddenPersonalGoals) {
            val nextTipTime = BingoAPI.lastBingoCardOpenTime + nextTip
            if (nextTipTime.isInPast()) {
                add(Renderable.string("§eThe next hint got unlocked already!"))
                add(Renderable.string("§eOpen the bingo card to update!"))
            } else {
                val until = nextTipTime.timeUntil()
                add(Renderable.string("§eThe next hint will unlock in §b${until.format(maxUnits = 2)}"))
            }
        }
    }

    private fun MutableList<Renderable>.addGoals(goals: MutableList<BingoGoal>, format: (BingoGoal) -> String) {
        val editDisplay = canEditDisplay()
        val showOnlyHighlighted = goals.count { it.highlight } > 0

        val filter = showOnlyHighlighted && !editDisplay
        val finalGoal = if (filter) {
            goals.filter { it.highlight }
        } else goals

        finalGoal.mapTo(this) {
            val currentlyHighlighted = it.highlight
            val highlightColor = if (currentlyHighlighted && editDisplay) "§e" else "§7"
            val display = "  $highlightColor" + format(it)

            if (editDisplay) {
                val clickName = if (currentlyHighlighted) "remove" else "add"
                Renderable.clickAndHover(
                    display,
                    buildList {
                        add("§a" + it.displayName)
                        for (s in it.guide) {
                            add(s)
                        }
                        add("")
                        add("§eClick to $clickName this goal as highlight!")
                    },
                    onClick = {
                        it.highlight = !currentlyHighlighted
                        it.displayName
                        update()
                    }
                )
            } else {
                Renderable.string(display)
            }
        }
        if (filter) {
            val missing = goals.size - finalGoal.size
            add(Renderable.string("  §8+ $missing not highlighted goals."))
        }
    }

    private var lastSneak = false
    private var inventoryOpen = false

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent) {
        if (!LorenzUtils.isBingoProfile) return
        if (!config.enabled) return

        val currentlyOpen = canEditDisplay()
        if (inventoryOpen != currentlyOpen) {
            inventoryOpen = currentlyOpen
            update()
        }

        if (config.quickToggle && ItemUtils.isSkyBlockMenuItem(InventoryUtils.getItemInHand())) {
            val sneaking = Minecraft.getMinecraft().thePlayer.isSneaking
            if (lastSneak != sneaking) {
                lastSneak = sneaking
                if (sneaking) {
                    toggleMode()
                }
            }
        }
        if (!config.stepHelper && displayMode == 1) {
            displayMode = 2
        }
        if (displayMode == 0) {
            if (Minecraft.getMinecraft().currentScreen !is GuiChat) {
                config.bingoCardPos.renderRenderables(display, posLabel = "Bingo Card")
            }
        } else if (displayMode == 1) {
            config.bingoCardPos.renderStrings(BingoNextStepHelper.currentHelp, posLabel = "Bingo Card")
        }
    }

    private fun canEditDisplay() =
        Minecraft.getMinecraft().currentScreen is GuiInventory || InventoryUtils.openInventoryName() == "Bingo Card"

    @HandleEvent