aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/misc/PeekCommand.kt
blob: 28d08048920ea4f6a460b9a3ad0c31e30501c0eb (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2023 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.commands.misc

import com.mojang.brigadier.arguments.StringArgumentType.string
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent
import io.github.moulberry.notenoughupdates.profileviewer.SkyblockProfiles
import io.github.moulberry.notenoughupdates.util.Utils
import io.github.moulberry.notenoughupdates.util.brigadier.*
import net.minecraft.client.Minecraft
import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting.*
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.apache.commons.lang3.text.WordUtils
import java.util.*
import java.util.concurrent.*

@NEUAutoSubscribe
class PeekCommand {

    var future: Future<*>? = null
    val executor = Executors.newScheduledThreadPool(1)

    fun executePeek(name: String) {
        val chatGui = Minecraft.getMinecraft().ingameGUI.chatGUI
        val id = Random().nextInt(Int.MAX_VALUE / 2) + Int.MAX_VALUE / 2
        fun deleteReply(text: String) {
            chatGui.printChatMessageWithOptionalDeletion(ChatComponentText(text), id)
        }

        deleteReply("$YELLOW[PEEK] Getting player information...")


        NotEnoughUpdates.profileViewer.loadPlayerByName(
            name
        ) { profile: SkyblockProfiles? ->
            if (profile == null) {
                deleteReply("$RED[PEEK] Unknown player or the Hypixel API is down.")
            } else {
                profile.resetCache()
                if (future?.isDone != true) {
                    Utils.addChatMessage(
                        "$RED[PEEK] New peek command was run, cancelling old one."
                    )
                    future?.cancel(true)
                }
                deleteReply("$YELLOW[PEEK] Getting the player's SkyBlock profile(s)...")
                val startTime = System.currentTimeMillis()
                future = ForkJoinPool.commonPool().submit(object : Runnable {
                    override fun run() {
                        if (System.currentTimeMillis() - startTime > 10 * 1000) {
                            deleteReply("$RED[PEEK] Getting profile info took too long, aborting.")
                            return
                        }
                        val g = GRAY.toString()
                        val profileInfo = profile.latestProfile.profileJson
                        if (profileInfo == null) {
                            future = executor.schedule(this, 200, TimeUnit.MILLISECONDS)
                            return
                        }
                        var overallScore = 0f
                        val isMe = name.equals("moulberry", ignoreCase = true)
                        val stats = profile.latestProfile.stats
                        if (stats == null) {
                            future = executor.schedule(this, 200, TimeUnit.MILLISECONDS)
                            return
                        }
                        val skyblockInfo = profile.latestProfile.levelingInfo
                        if (NotEnoughUpdates.INSTANCE.config.profileViewer.useSoopyNetworth) {
                            deleteReply("$YELLOW[PEEK] Getting the player's Skyblock networth...")
                            val countDownLatch = CountDownLatch(1)
                            profile.latestProfile.getSoopyNetworth { countDownLatch.countDown() }
                            try { // Wait for async network request
                                countDownLatch.await(10, TimeUnit.SECONDS)
                            } catch (_: InterruptedException) {}

                            // Now it's waited for network request the data should be cached (accessed in nw section)
                        }
                        deleteReply(
                            "$GREEN $STRIKETHROUGH-=-$RESET$GREEN ${
                                Utils.getElementAsString(
                                    profile.hypixelProfile!!["displayname"],
                                    name
                                )
                            }'s Info $STRIKETHROUGH-=-"
                        )
                        if (skyblockInfo == null || !profile.latestProfile.skillsApiEnabled()) {
                            Utils.addChatMessage(YELLOW.toString() + "Skills API disabled!")
                        } else {
                            var totalSkillLVL = 0f
                            var totalSkillCount = 0f
                            val skills: List<String> =
                                mutableListOf(
                                    "taming",
                                    "mining",
                                    "foraging",
                                    "enchanting",
                                    "farming",
                                    "combat",
                                    "fishing",
                                    "alchemy",
                                    "carpentry"
                                )
                            for (skillName in skills) {
                                totalSkillLVL += skyblockInfo[skillName]!!.level
                                totalSkillCount++
                            }
                            var combat = skyblockInfo["combat"]!!.level
                            var zombie = skyblockInfo["zombie"]!!.level
                            var spider = skyblockInfo["spider"]!!.level
                            var wolf = skyblockInfo["wolf"]!!.level
                            var enderman = skyblockInfo["enderman"]!!.level
                            var blaze = skyblockInfo["blaze"]!!.level
                            var avgSkillLVL = totalSkillLVL / totalSkillCount
                            if (isMe) {
                                avgSkillLVL = 6f
                                combat = 4f
                                zombie = 2f
                                spider = 1f
                                wolf = 2f
                                enderman = 0f
                                blaze = 0f
                            }
                            val combatPrefix =
                                if (combat > 20) (if (combat > 35) GREEN else YELLOW) else RED
                            val zombiePrefix =
                                if (zombie > 3) (if (zombie > 6) GREEN else YELLOW) else RED
                            val spiderPrefix =
                                if (spider > 3) (if (spider > 6) GREEN else YELLOW) else RED
                            val wolfPrefix =
                                if (wolf > 3) (if (wolf > 6) GREEN else YELLOW) else RED
                            val endermanPrefix =
                                if (enderman > 3) (if (enderman > 6) GREEN else YELLOW) else RED
                            val blazePrefix =
                                if (blaze > 3) (if (blaze > 6) GREEN else YELLOW) else RED
                            val avgPrefix =
                                if (avgSkillLVL > 20) (if (avgSkillLVL > 35) GREEN else YELLOW) else RED
                            overallScore += zombie * zombie / 81f
                            overallScore += spider * spider / 81f
                            overallScore += wolf * wolf / 81f
                            overallScore += enderman * enderman / 81f
                            overallScore += blaze * blaze / 81f
                            overallScore += avgSkillLVL / 20f
                            val cata = skyblockInfo["catacombs"]!!.level.toInt()
                            val cataPrefix =
                                if (cata > 15) (if (cata > 25) GREEN else YELLOW) else RED
                            overallScore += cata * cata / 2000f
                            Utils.addChatMessage(
                                g + "Combat: " + combatPrefix + Math.floor(combat.toDouble())
                                    .toInt() +
                                        (if (cata > 0) "$g - Cata: $cataPrefix$cata" else "") +
                                        g + " - AVG: " + avgPrefix + Math.floor(avgSkillLVL.toDouble())
                                    .toInt()
                            )
                            Utils.addChatMessage(
                                g + "Slayer: " + zombiePrefix + Math.floor(zombie.toDouble())
                                    .toInt() + g + "-" +
                                        spiderPrefix + Math.floor(spider.toDouble())
                                    .toInt() + g + "-" +
                                        wolfPrefix + Math.floor(wolf.toDouble()).toInt() + g + "-" +
                                        endermanPrefix + Math.floor(enderman.toDouble())
                                    .toInt() + g + "-" +
                                        blazePrefix + Math.floor(blaze.toDouble()).toInt()
                            )
                        }
                        val health =