
walkmc.extensions.ClientMessages.kt Maven / Gradle / Ivy
package walkmc.extensions
import com.google.common.io.*
import org.bukkit.entity.*
import org.bukkit.plugin.messaging.*
/**
* Builds a data output used to send to players using [sendClientMessage].
*/
inline fun buildClientMessage(block: ByteArrayDataOutput.() -> Unit): ByteArray {
return ByteStreams.newDataOutput().apply(block).toByteArray()
}
/**
* Converts this byte array to a [ByteArrayDataInput].
*/
fun ByteArray.toDataInput(): ByteArrayDataInput = ByteStreams.newDataInput(this)
/**
* Converts this byte array to a [ByteArrayDataOutput].
*/
fun ByteArray.toDataOutput(): ByteArrayDataOutput {
val data = ByteStreams.newDataOutput(size)
data.write(this)
return data
}
/**
* Sends a client message in this server.
*
* @param allPlayers if the message must be sended to all online players,
* otherwise will only be sended for the first player.
*/
fun sendClientMessage(channel: String, message: ByteArray, allPlayers: Boolean = true) {
if (allPlayers) {
server.sendPluginMessage(channel, message)
} else {
val first = onlinePlayers.firstOrNull() ?: return
first.sendPluginMessage(channel, message)
}
}
/**
* Sends a bungeecord plugin message in this server.
*/
fun sendBungeeCordMessage(message: ByteArray, allPlayers: Boolean = false) {
sendClientMessage("BungeeCord", message, allPlayers)
}
/**
* Sends a bungeecord plugin message used to connect a player to the specified [server].
*/
fun Player.connectToServer(server: String) {
val data = buildClientMessage {
writeUTF("Connect")
writeUTF(server)
}
sendPluginMessage("BungeeCord", data)
}
/**
* Sends a bungeecord plugin message used to send a message from the proxy.
*
* @param player the player to receive the message, or null if the message must be received from all players.
*/
fun sendProxyMessage(message: String, player: String? = null) {
val data = buildClientMessage {
writeUTF("Message")
writeUTF(player ?: "ALL")
writeUTF(message)
}
sendBungeeCordMessage(data)
}
/**
* Sends a bungeecord plugin message used to get the player count of a server.
*
* The response must be implemented by the plugin.
*/
fun sendPlayerCountMessage(server: String) {
val data = buildClientMessage {
writeUTF("PlayerCount")
writeUTF(server)
}
sendBungeeCordMessage(data)
}
/**
* Sends a bungeecord plugin message used to get the player list of a server.
*
* The response must be implemented by the plugin.
*/
fun sendPlayerListMessage(server: String) {
val data = buildClientMessage {
writeUTF("PlayerList")
writeUTF(server)
}
sendBungeeCordMessage(data)
}
/**
* Sends a bungeecord plugin message used to get all servers configured.
*
* The response must be implemented by the plugin.
*/
fun sendGetServersMessage() {
val data = buildClientMessage {
writeUTF("GetServers")
}
sendBungeeCordMessage(data)
}
/**
* Register a message channel from the client.
*
* @param type the type of channel will be registered.
*
* [Channel.RECEIVE] - receive messages from the client
*
* [Channel.SEND] - send message to the client
*
* @param channel the channel to be registered.
*/
fun walkmc.Plugin.registerChannel(
channel: String,
type: Channel = Channel.BOTH,
listener: PluginMessageListener = this
) {
val messenger = server.messenger
when (type) {
Channel.RECEIVE -> messenger.registerIncomingPluginChannel(this, channel, listener)
Channel.SEND -> messenger.registerOutgoingPluginChannel(this, channel)
Channel.BOTH -> {
messenger.registerIncomingPluginChannel(this, channel, listener)
messenger.registerOutgoingPluginChannel(this, channel)
}
}
}
enum class Channel {
RECEIVE, SEND, BOTH
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy