Added plugin message response callbacks

This commit is contained in:
bluefireoly
2020-11-02 18:11:35 +01:00
parent 0b63192dbf
commit 6bdde93c09
2 changed files with 65 additions and 3 deletions

View File

@@ -0,0 +1,49 @@
package net.axay.kspigot.pluginmessages
import net.axay.kspigot.runnables.taskRunLater
import org.bukkit.entity.Player
import org.bukkit.plugin.messaging.PluginMessageListener
import java.io.ByteArrayInputStream
import java.io.DataInputStream
internal data class BungeePluginMessageResponseCallback(
val subChannel: String,
val player: Player,
val timeoutSeconds: Int,
val onResponse: (message: DataInputStream) -> Unit
) {
init {
BungeePluginMessageReceiver.registered += this
taskRunLater(20L * timeoutSeconds) {
BungeePluginMessageReceiver.registered -= this
}
}
}
private object BungeePluginMessageReceiver : PluginMessageListener {
val registered = ArrayList<BungeePluginMessageResponseCallback>()
override fun onPluginMessageReceived(channel: String, player: Player, message: ByteArray) {
if (channel != "BungeeCord") return
val msgbytes = ByteArrayInputStream(message)
val msgin = DataInputStream(msgbytes)
val subChannel = msgin.readUTF()
val callback = registered.find { it.subChannel == subChannel && it.player == player }
if (callback != null) {
registered -= callback
callback.onResponse.invoke(msgin)
}
}
}

View File

@@ -4,6 +4,7 @@ import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.entity.Player
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.IOException
@@ -16,11 +17,13 @@ import java.io.IOException
*/
fun sendPluginMessageToBungeeCordRandomPlayer(
subChannel: String,
content: List<String>? = null
content: List<String>? = null,
responseTimeout: Int = 20,
onResponse: ((message: DataInputStream) -> Unit)? = null
): Boolean {
val randomPlayer = onlinePlayers.randomOrNull()
return if (randomPlayer != null) {
sendPluginMessageToBungeeCord(randomPlayer, subChannel, content)
sendPluginMessageToBungeeCord(randomPlayer, subChannel, content, responseTimeout, onResponse)
true
} else false
}
@@ -29,11 +32,18 @@ fun sendPluginMessageToBungeeCordRandomPlayer(
* Sends a plugin message on the "BungeeCord" channel.
* Specify your sub channel and if necessary add the
* required content.
* @param player The player which should be used to send the message.
* @param subChannel The channel, where the message should be send. (Usually the channel defines the behaviour.)
* @param content The optional content of the message.
* @param responseTimeout The time in seconds after which the response counts as "not arrived".
* @param onResponse The optional reponse callback.
*/
fun sendPluginMessageToBungeeCord(
player: Player,
subChannel: String,
content: List<String>? = null
content: List<String>? = null,
responseTimeout: Int = 20,
onResponse: ((message: DataInputStream) -> Unit)? = null
) {
val msgbytes = ByteArrayOutputStream()
@@ -51,6 +61,9 @@ fun sendPluginMessageToBungeeCord(
e.printStackTrace()
}
if (onResponse != null)
BungeePluginMessageResponseCallback(subChannel, player, responseTimeout, onResponse)
player.sendPluginMessage(KSpigotMainInstance, "BungeeCord", msgbytes.toByteArray())
}