Update PlayerInput utils

This commit is contained in:
Jakob K
2021-05-29 01:51:55 +02:00
parent 837353827e
commit fe24bb9dda
2 changed files with 13 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ import net.axay.kspigot.chat.input.implementations.PlayerInputChat
import net.axay.kspigot.event.unregister
import net.axay.kspigot.runnables.sync
import net.axay.kspigot.runnables.taskRunLater
import net.axay.kspigot.utils.mark
import org.bukkit.entity.Player
import org.bukkit.event.Listener
@@ -49,9 +50,7 @@ fun Player.awaitAnvilInput(
fun Player.awaitBookInputAsString(
timeoutSeconds: Int = 1 * 60,
callback: (PlayerInputResult<String>) -> Unit,
) {
PlayerInputBookComprehensive(this, callback, timeoutSeconds)
}
) = PlayerInputBookComprehensive(this, callback, timeoutSeconds).bookItemStack
/**
* Opens a book and uses the text the player inserted
@@ -62,14 +61,13 @@ fun Player.awaitBookInputAsString(
fun Player.awaitBookInputAsList(
timeoutSeconds: Int = 1 * 60,
callback: (PlayerInputResult<List<String>>) -> Unit,
) {
PlayerInputBookPaged(this, callback, timeoutSeconds)
}
) = PlayerInputBookPaged(this, callback, timeoutSeconds).bookItemStack
/**
* @param input The input the player gave. Null on timeout or invalid input.
*/
class PlayerInputResult<T> internal constructor(val input: T?)
internal abstract class PlayerInput<T>(
protected val player: Player,
private val callback: (PlayerInputResult<T>) -> Unit,

View File

@@ -37,12 +37,14 @@ internal abstract class PlayerInputBook<T>(
) : PlayerInput<T>(player, callback, timeoutSeconds) {
private val id = getID()
val bookItemStack = itemStack(Material.WRITABLE_BOOK) {
meta {
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
}
}
init {
player.openBook(itemStack(Material.WRITABLE_BOOK) {
meta {
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
}
})
player.inventory.addItem(bookItemStack)
}
abstract fun loadBookContent(bookMeta: BookMeta): T
@@ -53,6 +55,8 @@ internal abstract class PlayerInputBook<T>(
if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) {
onReceive(loadBookContent(meta))
usedIDs -= id
it.isCancelled = true
player.inventory.removeItem(bookItemStack)
}
}
)