|
|
@ -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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|