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

master
ALI Hamza 2019-10-13 18:15:40 +07:00
parent 0cb7f55d63
commit e9d5730300
No known key found for this signature in database
GPG Key ID: 7C608266BF384ADC
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") 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 voteNew by message("vote.new")
val voteWarn by message("vote.warn") val voteWarn by message("vote.warn")
val voteTimeout by message("vote.timeout") val voteTimeout by message("vote.timeout")

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

@ -1,4 +1,4 @@
package com.terraocean.plugin.afk package com.terraocean.plugin.listeners
import com.terraocean.plugin.bridge.reportPlayerActivity import com.terraocean.plugin.bridge.reportPlayerActivity
import org.bukkit.event.player.PlayerMoveEvent 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)
}