Console ConfigSection.kt

This commit is contained in:
jiahua.liu 2020-01-25 17:02:43 +08:00
parent dcb8b0ac3d
commit a9f9618f3f
4 changed files with 125 additions and 13 deletions

View File

@ -1,13 +0,0 @@
plugins{
id("com.github.johnrengelman.shadow") version "5.2.0"
}
apply plugin: "kotlin"
apply plugin: "java"
dependencies {
api project(':mirai-core')
api project(':mirai-core-timpc')
runtimeOnly files('../mirai-core-timpc/build/classes/kotlin/jvm/main')
runtimeOnly files('../mirai-core/build/classes/kotlin/jvm/main')
// classpath is not set correctly by IDE
}

View File

@ -0,0 +1,32 @@
plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
id("kotlinx-serialization")
id("kotlin")
id("java")
}
val kotlinVersion: String by rootProject.ext
val atomicFuVersion: String by rootProject.ext
val coroutinesVersion: String by rootProject.ext
val kotlinXIoVersion: String by rootProject.ext
val coroutinesIoVersion: String by rootProject.ext
val klockVersion: String by rootProject.ext
val ktorVersion: String by rootProject.ext
val serializationVersion: String by rootProject.ext
fun kotlinx(id: String, version: String) = "org.jetbrains.kotlinx:kotlinx-$id:$version"
fun ktor(id: String, version: String) = "io.ktor:ktor-$id:$version"
dependencies {
api(project(":mirai-core"))
api(project(":mirai-core-timpc"))
runtimeOnly(files("../mirai-core-timpc/build/classes/kotlin/jvm/main"))
runtimeOnly(files("../mirai-core/build/classes/kotlin/jvm/main"))
api(kotlin("serialization"))
// classpath is not set correctly by IDE
}

View File

@ -0,0 +1,54 @@
package net.mamoe.mirai.plugin
import kotlinx.serialization.Serializable
import java.util.concurrent.ConcurrentHashMap
@Serializable
class ConfigSection() : ConcurrentHashMap<String, Any>() {
fun getString(key: String): String {
return (get(key) ?: error("ConfigSection does not contain $key ")).toString()
}
fun getInt(key: String): Int {
return (get(key) ?: error("ConfigSection does not contain $key ")).toString().toInt()
}
fun getFloat(key: String): Float {
return (get(key) ?: error("ConfigSection does not contain $key ")).toString().toFloat()
}
fun getDouble(key: String): Double {
return (get(key) ?: error("ConfigSection does not contain $key ")).toString().toDouble()
}
fun getLong(key: String): Long {
return (get(key) ?: error("ConfigSection does not contain $key ")).toString().toLong()
}
fun getConfigSection(key: String): ConfigSection {
return (get(key) ?: error("ConfigSection does not contain $key ")) as ConfigSection
}
fun getStringList(key: String): List<String> {
return ((get(key) ?: error("ConfigSection does not contain $key ")) as List<*>).map { it.toString() }
}
fun getIntList(key: String): List<Int> {
return ((get(key) ?: error("ConfigSection does not contain $key ")) as List<*>).map { it.toString().toInt() }
}
fun getFloatList(key: String): List<Float> {
return ((get(key) ?: error("ConfigSection does not contain $key ")) as List<*>).map { it.toString().toFloat() }
}
fun getDoubleList(key: String): List<Double> {
return ((get(key) ?: error("ConfigSection does not contain $key ")) as List<*>).map { it.toString().toDouble() }
}
fun getLongList(key: String): List<Long> {
return ((get(key) ?: error("ConfigSection does not contain $key ")) as List<*>).map { it.toString().toLong() }
}
}

View File

@ -1,6 +1,8 @@
package net.mamoe.mirai.plugin
import kotlinx.coroutines.*
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.io.encodeToString
@ -43,10 +45,47 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
}
/**
* 当任意指令被使用
*/
open fun onCommand(command: Command) {
}
internal fun enable() {
this.onEnable()
}
@UnstableDefault
fun loadConfig(fileName: String = "config.json"): ConfigSection {
var content = File(dataFolder.name + "/" + fileName)
.also {
if (!it.exists()) it.createNewFile()
}.readText()
if (content == "") {
content = "{}"
}
return Json.parse(
ConfigSection.serializer(),
content
)
}
@UnstableDefault
fun saveConfig(config: ConfigSection, fileName: String = "config.json") {
val content = Json.stringify(
ConfigSection.serializer(),
config
)
File(dataFolder.name + "/" + fileName)
.also {
if (!it.exists()) it.createNewFile()
}.writeText(content)
}
@JvmOverloads
internal fun disable(throwable: CancellationException? = null) {
this.coroutineContext[Job]!!.cancelChildren(throwable)