diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt b/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt index 1af07329..a4cfc0a2 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt @@ -2,26 +2,14 @@ package net.axay.kspigot.chat.input -import net.axay.kspigot.chat.KColors -import net.axay.kspigot.event.listen +import net.axay.kspigot.chat.input.implementations.PlayerInputAnvilInv +import net.axay.kspigot.chat.input.implementations.PlayerInputBookComprehensive +import net.axay.kspigot.chat.input.implementations.PlayerInputBookPaged +import net.axay.kspigot.chat.input.implementations.PlayerInputChat import net.axay.kspigot.event.unregister -import net.axay.kspigot.extensions.bukkit.closeForViewers -import net.axay.kspigot.extensions.bukkit.content -import net.axay.kspigot.items.itemStack -import net.axay.kspigot.items.meta -import net.axay.kspigot.main.KSpigotMainInstance import net.axay.kspigot.runnables.taskRunLater -import net.wesjd.anvilgui.AnvilGUI -import org.bukkit.Material -import org.bukkit.NamespacedKey import org.bukkit.entity.Player -import org.bukkit.event.EventPriority import org.bukkit.event.Listener -import org.bukkit.event.inventory.InventoryClickEvent -import org.bukkit.event.player.AsyncPlayerChatEvent -import org.bukkit.event.player.PlayerEditBookEvent -import org.bukkit.inventory.meta.BookMeta -import org.bukkit.persistence.PersistentDataType /** * Asks the player a question and uses the next @@ -109,133 +97,4 @@ internal abstract class PlayerInput( } } -} - -internal class PlayerInputChat( - player: Player, - callback: (PlayerInputResult) -> Unit, - timeoutSeconds: Int, - question: String -) : PlayerInput(player, callback, timeoutSeconds) { - - init { - player.sendMessage("${KColors.ORANGERED}$question") - } - - override val inputListeners = listOf( - listen(EventPriority.LOWEST) { - if (it.player == player) { - it.isCancelled = true - onReceive(it.message) - } - } - ) - -} - -internal class PlayerInputAnvilInv( - player: Player, - callback: (PlayerInputResult) -> Unit, - timeoutSeconds: Int, - invTitle: String, - startText: String, - renameItemDescription: List -) : PlayerInput(player, callback, timeoutSeconds) { - - private val anvilInv = - AnvilGUI.Builder().plugin(KSpigotMainInstance) - .onClose { onReceive(null) } - .onComplete { _, text -> - return@onComplete if (text.isNotEmpty()) { - onReceive(text) - AnvilGUI.Response.close() - } else - AnvilGUI.Response.text("Type here...") - } - .title("${KColors.ORANGERED}$invTitle") - .item( - itemStack(Material.PAPER) { - meta { - lore = renameItemDescription.map { "${KColors.INDIANRED}$it" } - } - } - ) - .text("${KColors.ORANGERED}$startText") - .open(player) - - override val inputListeners = listOf( - listen { - if (it.clickedInventory == anvilInv.inventory) - it.isCancelled = true - } - ) - - override fun onTimeout() { - anvilInv.inventory.closeForViewers() - } - -} - -internal class PlayerInputBookComprehensive( - player: Player, - callback: (PlayerInputResult) -> Unit, - timeoutSeconds: Int -) : PlayerInputBook(player, callback, timeoutSeconds) { - override fun loadBookContent(bookMeta: BookMeta) = bookMeta.content -} - -internal class PlayerInputBookPaged( - player: Player, - callback: (PlayerInputResult>) -> Unit, - timeoutSeconds: Int -) : PlayerInputBook>(player, callback, timeoutSeconds) { - override fun loadBookContent(bookMeta: BookMeta): List = bookMeta.pages -} - -internal abstract class PlayerInputBook( - player: Player, - callback: (PlayerInputResult) -> Unit, - timeoutSeconds: Int -) : PlayerInput(player, callback, timeoutSeconds) { - - private val id = getID() - - init { - player.openBook(itemStack(Material.WRITABLE_BOOK) { - meta { - persistentDataContainer[idKey, PersistentDataType.INTEGER] = id - } - }) - } - - abstract fun loadBookContent(bookMeta: BookMeta): T - - override val inputListeners = listOf( - listen { - val meta = it.newBookMeta - if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) { - onReceive(loadBookContent(meta)) - usedIDs -= id - } - } - ) - - override fun onTimeout() { - player.closeInventory() - usedIDs -= id - } - - companion object { - - val idKey = NamespacedKey(KSpigotMainInstance, "kspigot_bookinput_id") - - internal val usedIDs = ArrayList() - fun getID(): Int { - var returnID = (0..Int.MAX_VALUE).random() - while (usedIDs.contains(returnID)) returnID = (0..Int.MAX_VALUE).random() - return returnID - } - - } - } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt new file mode 100644 index 00000000..1c6f5d26 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt @@ -0,0 +1,57 @@ +package net.axay.kspigot.chat.input.implementations + +import net.axay.kspigot.chat.KColors +import net.axay.kspigot.chat.input.PlayerInput +import net.axay.kspigot.chat.input.PlayerInputResult +import net.axay.kspigot.event.listen +import net.axay.kspigot.extensions.bukkit.closeForViewers +import net.axay.kspigot.items.itemStack +import net.axay.kspigot.items.meta +import net.axay.kspigot.main.KSpigotMainInstance +import net.wesjd.anvilgui.AnvilGUI +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.event.inventory.InventoryClickEvent + +internal class PlayerInputAnvilInv( + player: Player, + callback: (PlayerInputResult) -> Unit, + timeoutSeconds: Int, + invTitle: String, + startText: String, + renameItemDescription: List +) : PlayerInput(player, callback, timeoutSeconds) { + + private val anvilInv = + AnvilGUI.Builder().plugin(KSpigotMainInstance) + .onClose { onReceive(null) } + .onComplete { _, text -> + return@onComplete if (text.isNotEmpty()) { + onReceive(text) + AnvilGUI.Response.close() + } else + AnvilGUI.Response.text("Type here...") + } + .title("${KColors.ORANGERED}$invTitle") + .item( + itemStack(Material.PAPER) { + meta { + lore = renameItemDescription.map { "${KColors.INDIANRED}$it" } + } + } + ) + .text("${KColors.ORANGERED}$startText") + .open(player) + + override val inputListeners = listOf( + listen { + if (it.clickedInventory == anvilInv.inventory) + it.isCancelled = true + } + ) + + override fun onTimeout() { + anvilInv.inventory.closeForViewers() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt new file mode 100644 index 00000000..c5d4f37b --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt @@ -0,0 +1,79 @@ +package net.axay.kspigot.chat.input.implementations + +import net.axay.kspigot.chat.input.PlayerInput +import net.axay.kspigot.chat.input.PlayerInputResult +import net.axay.kspigot.event.listen +import net.axay.kspigot.extensions.bukkit.content +import net.axay.kspigot.items.itemStack +import net.axay.kspigot.items.meta +import net.axay.kspigot.main.KSpigotMainInstance +import org.bukkit.Material +import org.bukkit.NamespacedKey +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerEditBookEvent +import org.bukkit.inventory.meta.BookMeta +import org.bukkit.persistence.PersistentDataType + +internal class PlayerInputBookComprehensive( + player: Player, + callback: (PlayerInputResult) -> Unit, + timeoutSeconds: Int +) : PlayerInputBook(player, callback, timeoutSeconds) { + override fun loadBookContent(bookMeta: BookMeta) = bookMeta.content +} + +internal class PlayerInputBookPaged( + player: Player, + callback: (PlayerInputResult>) -> Unit, + timeoutSeconds: Int +) : PlayerInputBook>(player, callback, timeoutSeconds) { + override fun loadBookContent(bookMeta: BookMeta): List = bookMeta.pages +} + +internal abstract class PlayerInputBook( + player: Player, + callback: (PlayerInputResult) -> Unit, + timeoutSeconds: Int +) : PlayerInput(player, callback, timeoutSeconds) { + + private val id = getID() + + init { + player.openBook(itemStack(Material.WRITABLE_BOOK) { + meta { + persistentDataContainer[idKey, PersistentDataType.INTEGER] = id + } + }) + } + + abstract fun loadBookContent(bookMeta: BookMeta): T + + override val inputListeners = listOf( + listen { + val meta = it.newBookMeta + if (meta.persistentDataContainer[idKey, PersistentDataType.INTEGER] == id) { + onReceive(loadBookContent(meta)) + usedIDs -= id + } + } + ) + + override fun onTimeout() { + player.closeInventory() + usedIDs -= id + } + + companion object { + + val idKey = NamespacedKey(KSpigotMainInstance, "kspigot_bookinput_id") + + internal val usedIDs = ArrayList() + fun getID(): Int { + var returnID = (0..Int.MAX_VALUE).random() + while (usedIDs.contains(returnID)) returnID = (0..Int.MAX_VALUE).random() + return returnID + } + + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt new file mode 100644 index 00000000..3f6a52b7 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt @@ -0,0 +1,31 @@ +package net.axay.kspigot.chat.input.implementations + +import net.axay.kspigot.chat.KColors +import net.axay.kspigot.chat.input.PlayerInput +import net.axay.kspigot.chat.input.PlayerInputResult +import net.axay.kspigot.event.listen +import org.bukkit.entity.Player +import org.bukkit.event.EventPriority +import org.bukkit.event.player.AsyncPlayerChatEvent + +internal class PlayerInputChat( + player: Player, + callback: (PlayerInputResult) -> Unit, + timeoutSeconds: Int, + question: String +) : PlayerInput(player, callback, timeoutSeconds) { + + init { + player.sendMessage("${KColors.ORANGERED}$question") + } + + override val inputListeners = listOf( + listen(EventPriority.LOWEST) { + if (it.player == player) { + it.isCancelled = true + onReceive(it.message) + } + } + ) + +} \ No newline at end of file