aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/org/polyfrost/chatting/chat/ChatShortcuts.kt
blob: 0c85553b2bc9435e3dfc5d21f6f116302067b818 (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
package org.polyfrost.chatting.chat

import cc.polyfrost.oneconfig.config.core.ConfigUtils
import org.polyfrost.chatting.Chatting
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import java.io.File

object ChatShortcuts {
    private val oldShortcutsFile = File(Chatting.oldModDir, "chatshortcuts.json")
    private val shortcutsFile = ConfigUtils.getProfileFile("chatshortcuts.json")
    private val PARSER = JsonParser()

    private var initialized = false

    val shortcuts = object : ArrayList<Pair<String, String>>() {
        private val comparator = Comparator<Pair<String, String>> { o1, o2 ->
            return@Comparator o2.first.length.compareTo(o1.first.length)
        }

        override fun add(element: Pair<String, String>): Boolean {
            val value = super.add(element)
            sortWith(comparator)
            return value
        }
    }

    fun initialize() {
        if (initialized) {
            return
        } else {
            initialized = true
        }
        if (shortcutsFile.exists()) {
            try {
                val jsonObj = PARSER.parse(shortcutsFile.readText()).asJsonObject
                for (shortcut in jsonObj.entrySet()) {
                    shortcuts.add(shortcut.key to shortcut.value.asString)
                }
                return
            } catch (_: Throwable) {
                shortcutsFile.renameTo(File(shortcutsFile.parentFile, "chatshortcuts.json.bak"))
            }
        }
        shortcutsFile.createNewFile()
        if (oldShortcutsFile.exists()) {
            shortcutsFile.writeText(
                oldShortcutsFile.readText()
            )
        } else {
            shortcutsFile.writeText(JsonObject().toString())
        }
    }

    fun removeShortcut(key: String) {
        shortcuts.removeIf { it.first == key }
        val jsonObj = PARSER.parse(shortcutsFile.readText()).asJsonObject
        jsonObj.remove(key)
        shortcutsFile.writeText(jsonObj.toString())
    }

    fun writeShortcut(key: String, value: String) {
        shortcuts.add(key to value)
        val jsonObj = PARSER.parse(shortcutsFile.readText()).asJsonObject
        jsonObj.addProperty(key, value)
        shortcutsFile.writeText(jsonObj.toString())
    }

    fun handleSentCommand(command: String): String {
        shortcuts.forEach {
            if (command == it.first || (command.startsWith(it.first) && command.substringAfter(it.first)
                    .startsWith(" "))
            ) {
                return command.replaceFirst(it.first, it.second)
            }
        }
        return command
    }
}