Update PlayerInput utils
This commit is contained in:
@@ -9,6 +9,7 @@ import net.axay.kspigot.chat.input.implementations.PlayerInputChat
|
|||||||
import net.axay.kspigot.event.unregister
|
import net.axay.kspigot.event.unregister
|
||||||
import net.axay.kspigot.runnables.sync
|
import net.axay.kspigot.runnables.sync
|
||||||
import net.axay.kspigot.runnables.taskRunLater
|
import net.axay.kspigot.runnables.taskRunLater
|
||||||
|
import net.axay.kspigot.utils.mark
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
|
|
||||||
@@ -49,9 +50,7 @@ fun Player.awaitAnvilInput(
|
|||||||
fun Player.awaitBookInputAsString(
|
fun Player.awaitBookInputAsString(
|
||||||
timeoutSeconds: Int = 1 * 60,
|
timeoutSeconds: Int = 1 * 60,
|
||||||
callback: (PlayerInputResult<String>) -> Unit,
|
callback: (PlayerInputResult<String>) -> Unit,
|
||||||
) {
|
) = PlayerInputBookComprehensive(this, callback, timeoutSeconds).bookItemStack
|
||||||
PlayerInputBookComprehensive(this, callback, timeoutSeconds)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a book and uses the text the player inserted
|
* Opens a book and uses the text the player inserted
|
||||||
@@ -62,14 +61,13 @@ fun Player.awaitBookInputAsString(
|
|||||||
fun Player.awaitBookInputAsList(
|
fun Player.awaitBookInputAsList(
|
||||||
timeoutSeconds: Int = 1 * 60,
|
timeoutSeconds: Int = 1 * 60,
|
||||||
callback: (PlayerInputResult<List<String>>) -> Unit,
|
callback: (PlayerInputResult<List<String>>) -> Unit,
|
||||||
) {
|
) = PlayerInputBookPaged(this, callback, timeoutSeconds).bookItemStack
|
||||||
PlayerInputBookPaged(this, callback, timeoutSeconds)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param input The input the player gave. Null on timeout or invalid input.
|
* @param input The input the player gave. Null on timeout or invalid input.
|
||||||
*/
|
*/
|
||||||
class PlayerInputResult<T> internal constructor(val input: T?)
|
class PlayerInputResult<T> internal constructor(val input: T?)
|
||||||
|
|
||||||
internal abstract class PlayerInput<T>(
|
internal abstract class PlayerInput<T>(
|
||||||
protected val player: Player,
|
protected val player: Player,
|
||||||
private val callback: (PlayerInputResult<T>) -> Unit,
|
private val callback: (PlayerInputResult<T>) -> Unit,
|
||||||
|
@@ -37,12 +37,14 @@ internal abstract class PlayerInputBook<T>(
|
|||||||
) : PlayerInput<T>(player, callback, timeoutSeconds) {
|
) : PlayerInput<T>(player, callback, timeoutSeconds) {
|
||||||
private val id = getID()
|
private val id = getID()
|
||||||
|
|
||||||
init {
|
val bookItemStack = itemStack(Material.WRITABLE_BOOK) {
|
||||||
player.openBook(itemStack(Material.WRITABLE_BOOK) {
|
|
||||||
meta {
|
meta {
|
||||||
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
|
persistentDataContainer[idKey, PersistentDataType.INTEGER] = id
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
player.inventory.addItem(bookItemStack)
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun loadBookContent(bookMeta: BookMeta): T
|
abstract fun loadBookContent(bookMeta: BookMeta): T
|
||||||
@@ -53,6 +55,8 @@ internal abstract class PlayerInputBook<T>(
|
|||||||
if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) {
|
if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) {
|
||||||
onReceive(loadBookContent(meta))
|
onReceive(loadBookContent(meta))
|
||||||
usedIDs -= id
|
usedIDs -= id
|
||||||
|
it.isCancelled = true
|
||||||
|
player.inventory.removeItem(bookItemStack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user