forked from chanbakjsd/TerraOceanPlugin
kotlin: refactor entire plugin to use mc-kutils, use an object-based configuration system, and convert to okhttp
parent
caf4dbeb4f
commit
45297d2c64
@ -0,0 +1,49 @@
|
||||
package com.terraocean.plugin
|
||||
|
||||
import hazae41.minecraft.kutils.bukkit.ConfigFile
|
||||
import hazae41.minecraft.kutils.get
|
||||
import java.io.File
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
object Settings : TerraConfig(instance.dataFolder["config.yml"]) {
|
||||
val socketURL by item("socket.url", "ws://localhost:8080/ws")
|
||||
}
|
||||
|
||||
object Strings : TerraConfig(instance.dataFolder["strings.yml"]) {
|
||||
val voteNew by message("vote.new")
|
||||
val voteWarn by message("vote.warn")
|
||||
val voteTimeout by message("vote.timeout")
|
||||
|
||||
val kickVoiceChannel by message("kick.voiceChannelLeave")
|
||||
val voteKick by message("kick.voteKick")
|
||||
val kickRemovedFromWhitelist by message("kick.removedFromWhitelist")
|
||||
}
|
||||
|
||||
open class TerraConfig(file: File) : ConfigFile(file) {
|
||||
inner class message(val path: String) {
|
||||
init {
|
||||
if (!config.contains(path)) {
|
||||
set(path, "Message not found: $path")
|
||||
}
|
||||
}
|
||||
|
||||
operator fun getValue(ref: Any?, prop: KProperty<*>): String =
|
||||
config.getString(path, "Message not found: $path")!!
|
||||
|
||||
operator fun setValue(ref: Any?, prop: KProperty<*>, value: String) = set(path, value)
|
||||
}
|
||||
|
||||
inner class item<T>(private val path: String, private val default: T) {
|
||||
init {
|
||||
if (!config.contains(path)) {
|
||||
set(path, default)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
operator fun getValue(ref: Any?, prop: KProperty<*>): T =
|
||||
config.get(path, default) as? T ?: run {
|
||||
throw IllegalStateException("Path $path [value ${config["path"]}]] does not conform to type required")
|
||||
}
|
||||
}
|
||||
}
|
@ -1,26 +1,21 @@
|
||||
package com.terraocean.plugin
|
||||
|
||||
import com.terraocean.plugin.afk.onPlayerMovement
|
||||
import com.terraocean.plugin.bridge.establishConnection
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
import java.io.File
|
||||
import hazae41.minecraft.kutils.bukkit.*
|
||||
|
||||
internal lateinit var instance: TerraOceanPlugin
|
||||
|
||||
class TerraOceanPlugin: JavaPlugin() {
|
||||
@KtorExperimentalAPI
|
||||
class TerraOceanPlugin: BukkitPlugin() {
|
||||
override fun onEnable() {
|
||||
instance = this
|
||||
config.load(File("plugin/terraocean.yml"))
|
||||
MainScope().launch {
|
||||
Strings // Initialize the config classes
|
||||
Settings
|
||||
|
||||
listen(callback = ::onPlayerMovement)
|
||||
|
||||
schedule(async = true) {
|
||||
establishConnection()
|
||||
}
|
||||
logger.info("TerraOcean plugin has started.")
|
||||
}
|
||||
override fun onDisable() {
|
||||
logger.info("TerraOcean plugin has stopped.")
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.terraocean.plugin.bridge
|
||||
|
||||
import io.ktor.http.cio.websocket.send
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.launch
|
||||
import com.terraocean.plugin.instance
|
||||
import hazae41.minecraft.kutils.bukkit.schedule
|
||||
|
||||
fun reportPlayerActivity(username: String, activity: Long) {
|
||||
MainScope().launch {
|
||||
webSocketSession.send("active $username $activity")
|
||||
instance.schedule(async = true) {
|
||||
socket.send("active $username $activity")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue