aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/command/TestChatCommand.kt
blob: 38db097a28bbfb35a32ca0546a7ac9dffb1466b7 (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
package at.hannibal2.skyhanni.test.command

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.StringUtils.stripHypixelMessage
import net.minecraft.util.ChatComponentText
import net.minecraft.util.IChatComponent

object TestChatCommand {

    fun command(args: Array<String>) {
        if (args.isEmpty()) {
            ChatUtils.userError("Specify a chat message to test!")
            return
        }

        SkyHanniMod.launchCoroutine {
            val mutArgs = args.toMutableList()
            val multiLines = mutArgs.remove("-lines")
            val isComplex = mutArgs.remove("-complex")
            // cant use multi lines without clipboard
            val isClipboard = mutArgs.remove("-clipboard") || multiLines
            val isHidden = mutArgs.remove("-s")
            val text = if (isClipboard) {
                OSUtils.readFromClipboard()
                    ?: return@launchCoroutine ChatUtils.userError("Clipboard does not contain a string!")
            } else mutArgs.joinToString(" ")
            if (multiLines) {
                for (line in text.split("\n")) {
                    extracted(isComplex, line, isHidden)
                }
            } else {
                extracted(isComplex, text, isHidden)
            }
        }
    }

    private fun extracted(isComplex: Boolean, text: String, isHidden: Boolean) {
        val component =
            if (isComplex)
                try {
                    IChatComponent.Serializer.jsonToComponent(text)
                } catch (ex: Exception) {
                    ChatUtils.userError("Please provide a valid JSON chat component (either in the command or via -clipboard)")
                    return
                }
            else ChatComponentText(text.replace("&", "§"))
        if (!isHidden) ChatUtils.chat("Testing message: §7${component.formattedText}", prefixColor = "§a")
        test(component, isHidden)
    }

    private fun test(componentText: IChatComponent, isHidden: Boolean) {
        val message = componentText.formattedText.stripHypixelMessage()
        val event = LorenzChatEvent(message, componentText)
        event.postAndCatch()

        if (event.blockedReason != "") {
            if (!isHidden) {
                ChatUtils.chat("§cChat blocked: ${event.blockedReason}")
            }
        } else {
            val finalMessage = event.chatComponent
            if (finalMessage.formattedText.stripHypixelMessage() != message) {
                if (!isHidden) {
                    ChatUtils.chat("§eChat modified!")
                }
            }
            ChatUtils.chat(finalMessage)
        }
    }
}