aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-01-03 11:12:54 +0700
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-01-03 11:12:54 +0700
commit487709996c22fb0dbcac792076be799a09865600 (patch)
tree5ab6f27a54d673fb057fd3eafd9a5e27816f898c /src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt
parentdf895d122a8a2ea249e686578e026c1d0a8b8c47 (diff)
downloadChatting-487709996c22fb0dbcac792076be799a09865600.tar.gz
Chatting-487709996c22fb0dbcac792076be799a09865600.tar.bz2
Chatting-487709996c22fb0dbcac792076be799a09865600.zip
Chattils -> Chatting (1.1.0)
Update images (ty Mo2men) update screenshot line tooltip (ty Mo2men)
Diffstat (limited to 'src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt')
-rw-r--r--src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt b/src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt
new file mode 100644
index 0000000..f72967f
--- /dev/null
+++ b/src/main/kotlin/cc/woverflow/chatting/chat/ChatShortcuts.kt
@@ -0,0 +1,66 @@
+package cc.woverflow.chatting.chat
+
+import cc.woverflow.chatting.Chatting
+import cc.woverflow.chatting.utils.ListenableArrayList
+import com.google.gson.JsonObject
+import com.google.gson.JsonParser
+import java.io.File
+
+object ChatShortcuts {
+ private val shortcutsFile = File(Chatting.modDir, "chatshortcuts.json")
+ private val PARSER = JsonParser()
+
+ private var initialized = false
+
+ val shortcuts = ListenableArrayList<Pair<String, String>>({
+ it.sortWith(comparator)
+ })
+ private val comparator = Comparator<Pair<String, String>> { o1, o2 ->
+ return@Comparator o2.first.length.compareTo(o1.first.length)
+ }
+
+
+ fun initialize() {
+ if (initialized) {
+ return
+ } else {
+ initialized = true
+ }
+ if (!shortcutsFile.exists()) {
+ shortcutsFile.createNewFile()
+ shortcutsFile.writeText(
+ JsonObject().toString()
+ )
+ } else {
+ val jsonObj = PARSER.parse(shortcutsFile.readText()).asJsonObject
+ for (shortcut in jsonObj.entrySet()) {
+ shortcuts.add(shortcut.key to shortcut.value.asString)
+ }
+ }
+ }
+
+ 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
+ }
+} \ No newline at end of file