forked from chanbakjsd/TerraOceanPlugin
85 lines
2.6 KiB
Kotlin
85 lines
2.6 KiB
Kotlin
package com.terraocean.plugin.bridge
|
|
|
|
import com.terraocean.plugin.instance
|
|
import io.ktor.client.HttpClient
|
|
import io.ktor.client.features.websocket.DefaultClientWebSocketSession
|
|
import io.ktor.client.features.websocket.WebSockets
|
|
import io.ktor.client.features.websocket.ws
|
|
import io.ktor.http.cio.websocket.Frame
|
|
import io.ktor.http.cio.websocket.readText
|
|
import io.ktor.util.KtorExperimentalAPI
|
|
import org.bukkit.Bukkit
|
|
|
|
@KtorExperimentalAPI
|
|
val client = HttpClient {
|
|
install(WebSockets)
|
|
}
|
|
|
|
lateinit var webSocketSession: DefaultClientWebSocketSession
|
|
|
|
@KtorExperimentalAPI
|
|
suspend fun establishConnection() {
|
|
client.ws(
|
|
host = "localhost",
|
|
port = 8080,
|
|
path = "/ws"
|
|
) {
|
|
webSocketSession = this
|
|
while (true) {
|
|
val message = incoming.receive()
|
|
if (message is Frame.Text) {
|
|
processMessage(message.readText().split(' '))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fun processMessage(msg: List<String>) {
|
|
when (msg[0]) {
|
|
"leave" -> {
|
|
Bukkit.getPlayer(msg[1])?.kickPlayer(
|
|
instance.config.getString("voicechannel")
|
|
)
|
|
}
|
|
"vote" -> {
|
|
for (p in Bukkit.getOnlinePlayers()) {
|
|
p.sendMessage(
|
|
instance.config.getString("vote.new") ?: "Message not found: vote.new"
|
|
)
|
|
}
|
|
}
|
|
"votewarn" -> {
|
|
Bukkit.getPlayer(msg[1])?.sendMessage(
|
|
instance.config.getString("vote.warn") ?: "Message not found: vote.warn"
|
|
)
|
|
}
|
|
"votetimeout" -> {
|
|
Bukkit.getPlayer(msg[1])?.kickPlayer(
|
|
instance.config.getString("vote.timeout") ?: "Message not found: vote.timeout"
|
|
)
|
|
}
|
|
"kick" -> {
|
|
Bukkit.getPlayer(msg[1])?.kickPlayer(
|
|
instance.config.getString("kick.votekick") ?: "Message not found: kick.votekick"
|
|
)
|
|
}
|
|
"whitelistadd" -> {
|
|
instance.server.run {
|
|
getOfflinePlayer(msg[1]).isWhitelisted = true
|
|
}
|
|
}
|
|
"whitelistremove" -> {
|
|
instance.server.run {
|
|
getPlayer(msg[1])?.kickPlayer(
|
|
instance.config.getString("kick.removedFromWhitelist") ?:
|
|
"Message not found: kick.removedFromWhitelist"
|
|
)
|
|
getOfflinePlayer(msg[1]).isWhitelisted = false
|
|
}
|
|
}
|
|
else -> instance.logger.warning(
|
|
"Potentially out of date plugin. Unknown message: " + msg.joinToString(" ")
|
|
)
|
|
}
|
|
}
|