From 6bdde93c093d670a40db0144c56a094dc75eb529 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Mon, 2 Nov 2020 18:11:35 +0100 Subject: [PATCH] Added plugin message response callbacks --- .../BungeePluginMessageReceiver.kt | 49 +++++++++++++++++++ .../kspigot/pluginmessages/PluginMessages.kt | 19 +++++-- 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt new file mode 100644 index 00000000..be89ceff --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt @@ -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() + + 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) + } + + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt index 0e88adc4..1c0a2607 100644 --- a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt @@ -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? = null + content: List? = 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? = null + content: List? = 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()) } \ No newline at end of file