-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWarpModelUtils.kt
More file actions
60 lines (55 loc) · 1.92 KB
/
Copy pathWarpModelUtils.kt
File metadata and controls
60 lines (55 loc) · 1.92 KB
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
package com.mairwunnx.projectessentials.warps.models
import com.mairwunnx.projectessentialscore.helpers.MOD_CONFIG_FOLDER
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import org.apache.logging.log4j.LogManager
import java.io.File
@UseExperimental(UnstableDefault::class)
object WarpModelUtils {
private val warpsConfig = MOD_CONFIG_FOLDER + File.separator + "warps.json"
private val logger = LogManager.getLogger()
var warpModel = WarpModel()
private val json = Json(
JsonConfiguration(
strictMode = false,
allowStructuredMapKeys = true,
prettyPrint = true
)
)
fun loadData() {
logger.info("Loading world warps data ...")
logger.debug("Setup json configuration for parsing ...")
if (!File(warpsConfig).exists()) {
logger.warn("Warps config not exist! creating it now!")
createConfigDirs(
MOD_CONFIG_FOLDER
)
val defaultConfig = json.stringify(
WarpModel.serializer(),
warpModel
)
File(warpsConfig).writeText(defaultConfig)
}
val warpsConfigRaw = File(warpsConfig).readText()
warpModel = json.parse(
WarpModel.serializer(), warpsConfigRaw)
logger.info("Warps config loaded: $warpModel")
}
fun saveData() {
createConfigDirs(
MOD_CONFIG_FOLDER
)
val spawnConfig = json.stringify(
WarpModel.serializer(),
warpModel
)
File(warpsConfig).writeText(spawnConfig)
}
@Suppress("SameParameterValue")
private fun createConfigDirs(path: String) {
logger.info("Creating config directory for warps ($path)")
val configDirectory = File(path)
if (!configDirectory.exists()) configDirectory.mkdirs()
}
}