Added PlayerInputResult to allow different result types
This commit is contained in:
@@ -18,12 +18,13 @@ import org.bukkit.event.Listener
|
|||||||
import org.bukkit.event.inventory.InventoryClickEvent
|
import org.bukkit.event.inventory.InventoryClickEvent
|
||||||
import org.bukkit.event.player.AsyncPlayerChatEvent
|
import org.bukkit.event.player.AsyncPlayerChatEvent
|
||||||
import org.bukkit.event.player.PlayerEditBookEvent
|
import org.bukkit.event.player.PlayerEditBookEvent
|
||||||
|
import org.bukkit.inventory.meta.BookMeta
|
||||||
import org.bukkit.persistence.PersistentDataType
|
import org.bukkit.persistence.PersistentDataType
|
||||||
|
|
||||||
fun Player.awaitChatInput(
|
fun Player.awaitChatInput(
|
||||||
question: String = "Type your input in the chat!",
|
question: String = "Type your input in the chat!",
|
||||||
timeoutSeconds: Int = 1 * 60,
|
timeoutSeconds: Int = 1 * 60,
|
||||||
callback: (String?) -> Unit
|
callback: (PlayerInputResult<String>) -> Unit
|
||||||
) {
|
) {
|
||||||
PlayerInputChat(this, callback, timeoutSeconds, question)
|
PlayerInputChat(this, callback, timeoutSeconds, question)
|
||||||
}
|
}
|
||||||
@@ -36,21 +37,23 @@ fun Player.awaitAnvilInput(
|
|||||||
"submit your input!"
|
"submit your input!"
|
||||||
),
|
),
|
||||||
timeoutSeconds: Int = 1 * 60,
|
timeoutSeconds: Int = 1 * 60,
|
||||||
callback: (String?) -> Unit
|
callback: (PlayerInputResult<String>) -> Unit
|
||||||
) {
|
) {
|
||||||
PlayerInputAnvilInv(this, callback, timeoutSeconds, invTitle, startText, renameItemDescription)
|
PlayerInputAnvilInv(this, callback, timeoutSeconds, invTitle, startText, renameItemDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Player.awaitBookInput(
|
fun Player.awaitBookInput(
|
||||||
timeoutSeconds: Int = 1 * 60,
|
timeoutSeconds: Int = 1 * 60,
|
||||||
callback: (String?) -> Unit
|
callback: (PlayerInputResult<String>) -> Unit
|
||||||
) {
|
) {
|
||||||
PlayerInputBook(this, callback, timeoutSeconds)
|
PlayerInputBookComprehensive(this, callback, timeoutSeconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal abstract class PlayerInput(
|
class PlayerInputResult<T> internal constructor(val input: T?)
|
||||||
|
|
||||||
|
internal abstract class PlayerInput<T>(
|
||||||
protected val player: Player,
|
protected val player: Player,
|
||||||
private val callback: (String?) -> Unit,
|
private val callback: (PlayerInputResult<T>) -> Unit,
|
||||||
timeoutSeconds: Int
|
timeoutSeconds: Int
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@@ -58,11 +61,11 @@ internal abstract class PlayerInput(
|
|||||||
|
|
||||||
protected abstract val inputListeners: List<Listener>
|
protected abstract val inputListeners: List<Listener>
|
||||||
|
|
||||||
protected fun onReceive(input: String?) {
|
protected fun onReceive(input: T?) {
|
||||||
if (!received) {
|
if (!received) {
|
||||||
inputListeners.forEach { it.unregister() }
|
inputListeners.forEach { it.unregister() }
|
||||||
received = true
|
received = true
|
||||||
callback.invoke(input)
|
callback.invoke(PlayerInputResult(input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,10 +82,10 @@ internal abstract class PlayerInput(
|
|||||||
|
|
||||||
internal class PlayerInputChat(
|
internal class PlayerInputChat(
|
||||||
player: Player,
|
player: Player,
|
||||||
callback: (String?) -> Unit,
|
callback: (PlayerInputResult<String>) -> Unit,
|
||||||
timeoutSeconds: Int,
|
timeoutSeconds: Int,
|
||||||
question: String
|
question: String
|
||||||
) : PlayerInput(player, callback, timeoutSeconds) {
|
) : PlayerInput<String>(player, callback, timeoutSeconds) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
player.sendMessage("${KColors.ORANGERED}$question")
|
player.sendMessage("${KColors.ORANGERED}$question")
|
||||||
@@ -101,12 +104,12 @@ internal class PlayerInputChat(
|
|||||||
|
|
||||||
internal class PlayerInputAnvilInv(
|
internal class PlayerInputAnvilInv(
|
||||||
player: Player,
|
player: Player,
|
||||||
callback: (String?) -> Unit,
|
callback: (PlayerInputResult<String>) -> Unit,
|
||||||
timeoutSeconds: Int,
|
timeoutSeconds: Int,
|
||||||
invTitle: String,
|
invTitle: String,
|
||||||
startText: String,
|
startText: String,
|
||||||
renameItemDescription: List<String>
|
renameItemDescription: List<String>
|
||||||
) : PlayerInput(player, callback, timeoutSeconds) {
|
) : PlayerInput<String>(player, callback, timeoutSeconds) {
|
||||||
|
|
||||||
private val anvilInv =
|
private val anvilInv =
|
||||||
AnvilGUI.Builder().plugin(KSpigotMainInstance)
|
AnvilGUI.Builder().plugin(KSpigotMainInstance)
|
||||||
@@ -142,16 +145,23 @@ internal class PlayerInputAnvilInv(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class PlayerInputBook(
|
internal class PlayerInputBookComprehensive(
|
||||||
player: Player,
|
player: Player,
|
||||||
callback: (String?) -> Unit,
|
callback: (PlayerInputResult<String>) -> Unit,
|
||||||
timeoutSeconds: Int
|
timeoutSeconds: Int
|
||||||
) : PlayerInput(player, callback, timeoutSeconds) {
|
) : PlayerInputBook<String>(player, callback, timeoutSeconds) {
|
||||||
|
override fun loadBookContent(bookMeta: BookMeta) = bookMeta.content
|
||||||
|
}
|
||||||
|
|
||||||
|
internal abstract class PlayerInputBook<T>(
|
||||||
|
player: Player,
|
||||||
|
callback: (PlayerInputResult<T>) -> Unit,
|
||||||
|
timeoutSeconds: Int
|
||||||
|
) : PlayerInput<T>(player, callback, timeoutSeconds) {
|
||||||
|
|
||||||
private val id = getID()
|
private val id = getID()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|
||||||
player.openBook(itemStack(Material.WRITABLE_BOOK) {
|
player.openBook(itemStack(Material.WRITABLE_BOOK) {
|
||||||
meta {
|
meta {
|
||||||
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
|
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
|
||||||
@@ -159,11 +169,13 @@ internal class PlayerInputBook(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract fun loadBookContent(bookMeta: BookMeta): T
|
||||||
|
|
||||||
override val inputListeners = listOf(
|
override val inputListeners = listOf(
|
||||||
listen<PlayerEditBookEvent> {
|
listen<PlayerEditBookEvent> {
|
||||||
val meta = it.newBookMeta
|
val meta = it.newBookMeta
|
||||||
if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) {
|
if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) {
|
||||||
onReceive(meta.content)
|
onReceive(loadBookContent(meta))
|
||||||
usedIDs -= id
|
usedIDs -= id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user