|
1 | 1 | package com.mairwunnx.projectessentials.chat.models |
2 | 2 |
|
3 | | -import com.mairwunnx.projectessentialscore.helpers.MOD_CONFIG_FOLDER |
4 | | -import kotlinx.serialization.UnstableDefault |
5 | | -import kotlinx.serialization.json.Json |
6 | | -import kotlinx.serialization.json.JsonConfiguration |
| 3 | +import com.mairwunnx.projectessentials.core.helpers.MOD_CONFIG_FOLDER |
| 4 | +import com.mairwunnx.projectessentials.core.helpers.jsonInstance |
7 | 5 | import org.apache.logging.log4j.LogManager |
8 | 6 | import java.io.File |
9 | 7 |
|
10 | | -@UseExperimental(UnstableDefault::class) |
11 | 8 | object ChatModelUtils { |
12 | 9 | private val warpsConfig = MOD_CONFIG_FOLDER + File.separator + "chat.json" |
13 | 10 | private val logger = LogManager.getLogger() |
14 | 11 | var chatModel = ChatModel() |
15 | | - private val json = Json( |
16 | | - JsonConfiguration( |
17 | | - strictMode = false, |
18 | | - allowStructuredMapKeys = true, |
19 | | - prettyPrint = true |
20 | | - ) |
21 | | - ) |
22 | 12 |
|
23 | 13 | fun loadData() { |
24 | 14 | logger.info("Loading chat configuration") |
25 | 15 | if (!File(warpsConfig).exists()) { |
26 | 16 | logger.warn("Chat configuration file not exist! creating it now!") |
27 | | - createConfigDirs(MOD_CONFIG_FOLDER) |
28 | | - val defaultConfig = json.stringify( |
29 | | - ChatModel.serializer(), |
30 | | - chatModel |
| 17 | + File(MOD_CONFIG_FOLDER).mkdirs() |
| 18 | + val defaultConfig = jsonInstance.stringify( |
| 19 | + ChatModel.serializer(), chatModel |
31 | 20 | ) |
32 | 21 | File(warpsConfig).writeText(defaultConfig) |
33 | 22 | } |
34 | 23 | val warpsConfigRaw = File(warpsConfig).readText() |
35 | | - chatModel = json.parse(ChatModel.serializer(), warpsConfigRaw) |
36 | | - logger.info("Chat configuration loading done") |
| 24 | + chatModel = jsonInstance.parse(ChatModel.serializer(), warpsConfigRaw) |
37 | 25 | } |
38 | 26 |
|
39 | 27 | fun saveData() { |
40 | | - logger.info("Saving chat configuration to file") |
41 | | - createConfigDirs(MOD_CONFIG_FOLDER) |
42 | | - val spawnConfig = json.stringify( |
43 | | - ChatModel.serializer(), |
44 | | - chatModel |
| 28 | + File(MOD_CONFIG_FOLDER).mkdirs() |
| 29 | + val spawnConfig = jsonInstance.stringify( |
| 30 | + ChatModel.serializer(), chatModel |
45 | 31 | ) |
46 | 32 | File(warpsConfig).writeText(spawnConfig) |
47 | | - logger.info("Saving chat configuration done") |
48 | | - } |
49 | | - |
50 | | - @Suppress("SameParameterValue") |
51 | | - private fun createConfigDirs(path: String) { |
52 | | - logger.info("Creating config directory for chat configuration") |
53 | | - val configDirectory = File(path) |
54 | | - if (!configDirectory.exists()) configDirectory.mkdirs() |
55 | 33 | } |
56 | 34 | } |
0 commit comments