Create PluginMessages.kt

This commit is contained in:
bluefireoly
2020-11-02 17:22:21 +01:00
parent 8c43ff6b87
commit 763af52fdf

View File

@@ -0,0 +1,56 @@
package net.axay.kspigot.pluginmessages
import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.entity.Player
import java.io.ByteArrayOutputStream
import java.io.DataOutputStream
import java.io.IOException
/**
* Chooses a random player to send the message.
* Be careful to not send a player specific message
* using this method!
* @see sendPluginMessageToBungeeCord
* @return True, if a player was found to send the message.
*/
fun sendPluginMessageToBungeeCordRandomPlayer(
subChannel: String,
content: List<String>? = null
): Boolean {
val randomPlayer = onlinePlayers.randomOrNull()
return if (randomPlayer != null) {
sendPluginMessageToBungeeCord(randomPlayer, subChannel, content)
true
} else false
}
/**
* Sends a plugin message on the "BungeeCord" channel.
* Specify your sub channel and if necessary add the
* required content.
*/
fun sendPluginMessageToBungeeCord(
player: Player,
subChannel: String,
content: List<String>? = null
) {
val msgbytes = ByteArrayOutputStream()
val msgout = DataOutputStream(msgbytes)
try {
msgout.writeUTF(subChannel)
if (content != null)
for (messagePart in content)
msgout.writeUTF(messagePart)
} catch (e: IOException) {
e.printStackTrace()
}
player.sendPluginMessage(KSpigotMainInstance, "BungeeCord", msgbytes.toByteArray())
}