67 lines
2.0 KiB
Kotlin
67 lines
2.0 KiB
Kotlin
package nl.voidcorp.mainplugin
|
|
|
|
import nl.voidcorp.mainplugin.messaging.Message
|
|
import nl.voidcorp.mainplugin.messaging.MessageType
|
|
import okio.Okio
|
|
import org.jetbrains.exposed.sql.Database
|
|
import java.io.File
|
|
|
|
class VoidPlugin(
|
|
override val comment: String = "What are we going to do today?"
|
|
) : VoidPluginBase() {
|
|
lateinit var conf: Config
|
|
var db: Database? = null
|
|
|
|
override fun enable() {
|
|
|
|
send(name, MessageType.GET_CONFIG)
|
|
send(name, MessageType.POST_CONFIG, moshi.adapter<Config>().toJson(conf))
|
|
if (db == null)
|
|
db = Database.connect(
|
|
conf.databaseUrl,
|
|
driver = conf.driver
|
|
)
|
|
|
|
}
|
|
|
|
override fun disable() {
|
|
send(name, MessageType.POST_CONFIG, moshi.adapter<Config>().toJson(conf))
|
|
}
|
|
|
|
override fun recieve(message: Message) {
|
|
when (message.messageType) {
|
|
MessageType.POST_CONFIG -> {
|
|
writeConfig(message.from, message.content)
|
|
logger.info("Saved config for ${message.from}")
|
|
}
|
|
MessageType.GET_CONFIG -> {
|
|
if (message.from == name) {
|
|
conf = moshi.adapter<Config>().fromJson(readConfig(name))!!
|
|
} else {
|
|
send(message.from, MessageType.GET_CONFIG, readConfig(message.from))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun readConfig(filename: String): String {
|
|
val config = File(dataFolder.apply { mkdirs() }, "$filename.json")
|
|
logger.info(config.path)
|
|
if (!config.exists()) {
|
|
config.createNewFile()
|
|
config.bufferedWriter().apply {
|
|
write("{}")
|
|
close()
|
|
}
|
|
return "{}"
|
|
}
|
|
return config.readText()
|
|
}
|
|
|
|
private fun writeConfig(filename: String, configString: String) {
|
|
val config = File(dataFolder.apply { mkdirs() }, "$filename.json")
|
|
val wr = config.bufferedWriter()
|
|
wr.write(configString)
|
|
wr.close()
|
|
}
|
|
} |