kotlin: query user, kickall on WS fail, rename Strings class, and handle sync command #11

Manually merged
hamza merged 1 commits from hamza/TerraOceanPlugin:master into master 2019-10-13 11:36:21 +07:00
8 changed files with 74 additions and 30 deletions

@ -9,7 +9,9 @@ object Settings : TerraConfig(instance.dataFolder["config.yml"]) {
val socketURL by item("socket.url", "ws://localhost:8080/ws")
}
object Strings : TerraConfig(instance.dataFolder["strings.yml"]) {
object Messages : TerraConfig(instance.dataFolder["strings.yml"]) {
val botConnectionLost by message("errors.botConnectionLost")
val voteNew by message("vote.new")
val voteWarn by message("vote.warn")
val voteTimeout by message("vote.timeout")

@ -1,7 +1,9 @@
package com.terraocean.plugin
import com.terraocean.plugin.afk.onPlayerMovement
import com.terraocean.plugin.listeners.onPlayerMovement
import com.terraocean.plugin.bridge.establishConnection
import com.terraocean.plugin.listeners.onPlayerJoin
import com.terraocean.plugin.listeners.onPlayerPreLogin
import hazae41.minecraft.kutils.bukkit.*
internal lateinit var instance: TerraOceanPlugin
@ -9,14 +11,20 @@ internal lateinit var instance: TerraOceanPlugin
class TerraOceanPlugin: BukkitPlugin() {
override fun onEnable() {
instance = this
Strings // Initialize the config classes
Messages // Initialize the config classes
Settings
listen(callback = ::onPlayerMovement)
joinListeners()
schedule(async = true) {
instance.info("establishing connection to ${Settings.socketURL}...")
establishConnection()
}
}
private fun joinListeners() {
listen(callback = ::onPlayerMovement)
listen(callback = ::onPlayerPreLogin)
listen(callback = ::onPlayerJoin)
}
}

@ -1,10 +0,0 @@
package com.terraocean.plugin.bridge
import com.terraocean.plugin.instance
import hazae41.minecraft.kutils.bukkit.schedule
fun reportPlayerActivity(username: String, activity: Long) {
instance.schedule(async = true) {
socket.send("active $username $activity")
}
}

@ -0,0 +1,17 @@
package com.terraocean.plugin.bridge
import com.terraocean.plugin.instance
import hazae41.minecraft.kutils.bukkit.schedule
fun queryUserJoin(username: String) {
sendToSocket("query $username")
}
fun reportPlayerActivity(username: String, activity: Long) {
sendToSocket("active $username $activity")
}
private fun sendToSocket(msg: String) {
if (!socketActive) return
instance.schedule(async = true) { socket.send(msg) }
}

@ -1,9 +1,10 @@
package com.terraocean.plugin.bridge
import com.terraocean.plugin.Settings
import com.terraocean.plugin.Strings
import com.terraocean.plugin.Messages
import com.terraocean.plugin.instance
import hazae41.minecraft.kutils.bukkit.info
import hazae41.minecraft.kutils.bukkit.msg
import hazae41.minecraft.kutils.bukkit.schedule
import hazae41.minecraft.kutils.bukkit.warning
import okhttp3.*
@ -18,7 +19,8 @@ private var request = Request.Builder()
.url(Settings.socketURL)
.build()
lateinit var socket: WebSocket
var wsFails = 0
internal var socketActive = false
private var wsFails = 0
fun establishConnection() {
socket = client.newWebSocket(request, WSListener())
@ -27,33 +29,32 @@ fun establishConnection() {
fun processMessage(msg: List<String>) {
when (msg[0].toLowerCase()) {
"leave" -> {
Bukkit.getPlayer(msg[1])?.kickPlayer(Strings.kickVoiceChannel)
Bukkit.getPlayer(msg[1])?.kickPlayer(Messages.kickVoiceChannel)
}
"vote" -> {
Bukkit.getOnlinePlayers().forEach {
it.sendMessage(Strings.voteNew)
}
Bukkit.getOnlinePlayers().forEach { it.msg(Messages.voteNew) }
}
"votewarn" -> {
Bukkit.getPlayer(msg[1])?.sendMessage(Strings.voteWarn)
Bukkit.getPlayer(msg[1])?.sendMessage(Messages.voteWarn)
}
"votetimeout" -> {
Bukkit.getPlayer(msg[1])?.kickPlayer(Strings.voteTimeout)
Bukkit.getPlayer(msg[1])?.kickPlayer(Messages.voteTimeout)
}
"kick" -> {
Bukkit.getPlayer(msg[1])?.kickPlayer(Strings.voteKick)
Bukkit.getPlayer(msg[1])?.kickPlayer(Messages.voteKick)
}
"whitelistadd" -> {
instance.server.run {
getOfflinePlayer(msg[1]).isWhitelisted = true
}
instance.server.getOfflinePlayer(msg[1]).isWhitelisted = true
}
"whitelistremove" -> {
instance.server.run {
getPlayer(msg[1])?.kickPlayer(Strings.kickRemovedFromWhitelist)
getPlayer(msg[1])?.kickPlayer(Messages.kickRemovedFromWhitelist)
getOfflinePlayer(msg[1]).isWhitelisted = false
}
}
"sync" -> {
Bukkit.getOnlinePlayers().map { it.name }.forEach(::queryUserJoin)
}
else -> instance.warning(
"Potentially out of date plugin. Unknown message: " + msg.joinToString(" ")
)
@ -62,20 +63,27 @@ fun processMessage(msg: List<String>) {
class WSListener : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
processMessage(text.split(' '))
processMessage(text.split(' ', limit = 2))
}
override fun onOpen(webSocket: WebSocket, response: Response) {
instance.info("Socket connection established to $response.")
instance.info("Socket connection established to ${response.request.url}.")
socketActive = true
wsFails = 0
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Bukkit.getOnlinePlayers().forEach { it.kickPlayer(Messages.botConnectionLost) }
socketActive = false
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
if (wsFails < 5) {
wsFails++
instance.warning("WS Fail: ${t.message}")
instance.info("Reconnecting to socket...")
}
socketActive = false
instance.schedule(delay = 5, unit = TimeUnit.SECONDS) {
establishConnection()
}

@ -1,4 +1,4 @@
package com.terraocean.plugin.afk
package com.terraocean.plugin.listeners
import com.terraocean.plugin.bridge.reportPlayerActivity
import org.bukkit.event.player.PlayerMoveEvent

@ -0,0 +1,11 @@
package com.terraocean.plugin.listeners
import com.terraocean.plugin.Messages
import com.terraocean.plugin.bridge.socketActive
import org.bukkit.event.player.PlayerJoinEvent
fun onPlayerJoin(e: PlayerJoinEvent) {
if (!socketActive) {
e.player.kickPlayer(Messages.botConnectionLost)
}
}

@ -0,0 +1,8 @@
package com.terraocean.plugin.listeners
import com.terraocean.plugin.bridge.queryUserJoin
import org.bukkit.event.player.AsyncPlayerPreLoginEvent
fun onPlayerPreLogin(e: AsyncPlayerPreLoginEvent) {
queryUserJoin(e.name)
}