aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/bingo/BingoCardDisplay.kt
blob: 2666cab01d5391f93fa5a1677b72acf0a029d689 (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
package at.hannibal2.skyhanni.features.bingo

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.features.bingo.card.CommunityGoal
import at.hannibal2.skyhanni.features.bingo.card.PersonalGoal
import at.hannibal2.skyhanni.utils.InventoryUtils.getInventoryName
import at.hannibal2.skyhanni.utils.ItemUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiChat
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.regex.Pattern

class BingoCardDisplay {

    private val MAX_PERSONAL_GOALS = 20
    private val MAX_COMMUNITY_GOALS = 5

    private var tick = 0
    private val display = mutableListOf<String>()

    companion object {
        val personalGoals = mutableListOf<PersonalGoal>()
        private val communityGoals = mutableListOf<CommunityGoal>()

        private var dirty = true

        fun command() {
            reload()
        }

        private fun reload() {
            personalGoals.clear()
            communityGoals.clear()
            dirty = true
        }
    }

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

        tick++
        if (tick % 5 != 0) return

        val gui = Minecraft.getMinecraft().currentScreen
        if (gui !is GuiChest) {
            dirty = true
            return
        }

        val chest = gui.inventorySlots as ContainerChest
        if (chest.getInventoryName() == "Bingo Card") {
            if (dirty) {
                readBingoCard(gui)
            }
        }
    }

    private fun readBingoCard(gui: GuiChest) {
        personalGoals.clear()
        communityGoals.clear()

        for (slot in gui.inventorySlots.inventorySlots) {
            val stack = slot?.stack ?: continue
            val personalGoal = stack.getLore().any { it.endsWith("Personal Goal") }
            val communityGoal = stack.getLore().any { it.endsWith("Community Goal") }
            if (!personalGoal && !communityGoal) continue
            val name = stack.name?.removeColor() ?: continue
            val lore = stack.getLore()
            var index = 0
            val builder = StringBuilder()
            for (s in lore) {
                if (index > 1) {
                    if (s == "") break
                    builder.append(s)
                    builder.append(" ")
                }
                index++
            }
            var description = builder.toString()
            if (description.endsWith(" ")) {
                description = description.substring(0, description.length - 1)
            }
            if (description.startsWith("§7§7")) {
                description = description.substring(2)
            }

            val done = stack.getLore().any { it.contains("GOAL REACHED") }
            if (personalGoal) {
                personalGoals.add(PersonalGoal(name, description, done))
            } else {
                communityGoals.add(CommunityGoal(name, description, done))
            }
        }

        val a = personalGoals.size
        val b = communityGoals.size
        if (a == MAX_PERSONAL_GOALS && b == MAX_COMMUNITY_GOALS) {
            dirty = false

            update()
        }
    }

    private fun update() {
        display.clear()

        display.add("Community Goals")
        communityGoals.mapTo(display) { "  " + it.description + if (it.done) " §aDONE" else "" }

        val todo = personalGoals.filter { !it.done }
        val done = MAX_PERSONAL_GOALS - todo.size
        display.add(" ")
        display.add("Personal Goals: ($done/$MAX_PERSONAL_GOALS done)")
        todo.mapTo(display) { "  " + it.description }
    }

    private var lastSneak = false
    private var displayMode = 0

    @SubscribeEvent
    fun onRenderOverlay(event: RenderGameOverlayEvent.Post) {
        if (event.type != RenderGameOverlayEvent.ElementType.ALL) return
        if (!LorenzUtils.isBingoProfile) return
        if (!SkyHanniMod.feature.bingo.bingoCard) return

        val stack = Minecraft.getMinecraft().thePlayer.heldItem //TODO into ItemUtils or InventoryUtils
        if (ItemUtils.isSkyBlockMenuItem(stack)) {
            val sneaking = Minecraft.getMinecraft().thePlayer.isSneaking
            if (lastSneak != sneaking) {
                lastSneak = sneaking
                if (sneaking) {
                    displayMode++
                    if (displayMode == 3) {
                        displayMode = 0
                    }
                }
            }
        }
        if (displayMode == 0) {
            if (Minecraft.getMinecraft().currentScreen !is GuiChat) {
                SkyHanniMod.feature.bingo.bingoCardPos.renderStrings(display)
            }
        } else if (displayMode == 1) {
            SkyHanniMod.feature.bingo.bingoCardPos.renderStrings(BingoNextStepHelper.currentHelp)
        }
    }

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!LorenzUtils.isBingoProfile) return

        val message = event.message
        //§6§lBINGO GOAL COMPLETE! §r§eRaw Salmon Collector
        val pattern = Pattern.compile("§6§lBINGO GOAL COMPLETE! §r§e(.*)")

        val matcher = pattern.matcher(message)

        if (matcher.matches()) {
            val name = matcher.group(0)
            println("name: '$name'")
            for (goal in personalGoals) {
                println("goal: '" + goal.displayName + "'")
            }
            personalGoals.filter { it.displayName == name }
                .forEach {
                    it.done = true
                    update()
                }
        }
    }
}