This repository has been archived by the owner on May 29, 2021. It is now read-only.
/
ConfigModel.kt
46 lines (41 loc) · 1.69 KB
/
ConfigModel.kt
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
package com.github.insanusmokrassar.ConfigsRemapper
import com.github.insanusmokrassar.IObjectK.extensions.remap
import com.github.insanusmokrassar.IObjectK.interfaces.CommonIObject
import com.github.insanusmokrassar.IObjectK.interfaces.IObject
import com.github.insanusmokrassar.IObjectK.realisations.SimpleIObject
import com.github.insanusmokrassar.IObjectK.utils.plus
import com.github.insanusmokrassar.IObjectKRealisations.readIObject
import com.github.insanusmokrassar.IObjectKRealisations.toObject
private fun IObject<Any>.toCommandReceiver(): Receiver {
return toObject(CommandHandlerConfig::class.java).run {
(paramsObjectInstance<Any>() ?: params) ?.let {
extract<Receiver>(classPath, it)
} ?:let {
extract<Receiver>(classPath)
}
}
}
class ConfigModel {
val receiver: IObject<Any> = SimpleIObject()
val commands: List<String> = emptyList()
val defaultParams: IObject<Any>? = null
val mapRules: IObject<Any>? = null
private var receiverCachedObject: Receiver? = null
val receiverObject: Receiver
get() = receiverCachedObject ?: receiver.toCommandReceiver().also {
receiverCachedObject = it
}
fun makeParamsObject(config: CommonIObject<String, Any>): IObject<Any> {
return SimpleIObject(
mapRules ?.let {
mapRules ->
config + (defaultParams ?. toString() ?. byteInputStream() ?. readIObject() /* safe copy */ ?: SimpleIObject()).also {
mapRules.remap(
config,
it
)
}
} ?: config
)
}
}