From 932f1770ff74c4b0c4e011353945d075b07caa0d Mon Sep 17 00:00:00 2001 From: gregf36665 Date: Sun, 22 Dec 2024 20:05:42 +0000 Subject: [PATCH] First draft at a procedural More options --- .../decoration/kitchen/ShelfListener.kt | 84 +++++++++++++------ 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/Server/src/main/content/global/skill/construction/decoration/kitchen/ShelfListener.kt b/Server/src/main/content/global/skill/construction/decoration/kitchen/ShelfListener.kt index f7fbab423..57b38be55 100644 --- a/Server/src/main/content/global/skill/construction/decoration/kitchen/ShelfListener.kt +++ b/Server/src/main/content/global/skill/construction/decoration/kitchen/ShelfListener.kt @@ -24,7 +24,7 @@ class ShelfListener : InteractionListener { openDialogue(player, ShelfDialogue(shelfId, 0)) } - class ShelfDialogue(val shelfId: Int, val offset: Int) : DialogueFile() { + class ShelfDialogue(shelfId: Int, val offset: Int) : DialogueFile() { companion object{ @@ -46,10 +46,7 @@ class ShelfListener : InteractionListener { ) private val oakshelf_1_items = woodshelf_2_items + listOf( - "More Options" to NEXT, - "Cake tin" to Items.CAKE_TIN_1887, "Bowl" to Items.BOWL_1923, - "More Options" to PREV ) val shelves = mapOf( @@ -60,35 +57,70 @@ class ShelfListener : InteractionListener { ) } - val end = min(shelves[shelfId]!!.size, 5) - val shelfItems = shelves[shelfId]!!.slice(offset until end) - val shelfItemsNames = shelfItems.map { it.first } - val shelfItemsIds = shelfItems.map { it.second } + private val shelfItemsNames = shelves[shelfId]!!.map { it.first } + private val shelfItemsIds = shelves[shelfId]!!.map { it.second } - fun checkItem(player: Player, shelfId: Int, offset: Int, button : Int){ - val item = shelfItemsIds[button] - if (item < 1000){ - openDialogue(player, ShelfDialogue(shelfId, offset+item)) + private fun checkItem(player: Player, idx : Int){ + val item = shelfItemsIds[idx] + if (hasSpaceFor(player, item, 1)){ + addItem(player, item) + end() } else{ - if (hasSpaceFor(player, item, 1)){ - addItem(player, item) - end() - } - else{ - sendDialogue(player, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE } - } + sendDialogue(player, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE } } - } + private fun createTopics(page: Int, itemsPerPage: Int): MutableList> { + val startIndex = (page - 1) * itemsPerPage + val endIndex = (startIndex + itemsPerPage).coerceAtMost(shelfItemsNames.size) + + val topics = shelfItemsNames.subList(startIndex, endIndex) + .mapIndexed { index, item -> Topic(item, startIndex + index + 1, skipPlayer = true) } + .toMutableList() + + // Add "More" button if there's another page or to loop back + if (endIndex < shelfItemsNames.size || page > 1) { + topics.add(Topic("More", endIndex + 1, skipPlayer = true)) + } + + return topics + } + + + override fun handle(componentID: Int, buttonID: Int) { - println("Stage: $stage") - when(stage){ - 0 -> showTopics( - *shelfItemsNames.toTypedArray().mapIndexed { index, item -> Topic(item, index+1, skipPlayer = true) }.toTypedArray() - ) - in 1 .. 5 -> checkItem(player!!, shelfId, offset, stage - 1).also { stage = 6 } + if (player!!.inventory.freeSlot() < 1) { + sendDialogue(player!!, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE } + return + } + val itemsPerPage = 4 + val totalPages = (shelfItemsNames.size + itemsPerPage - 1) / itemsPerPage // Total number of pages + + when (stage) { + 0, shelfItemsNames.size + 1 -> { // Initial stage or final more button clicked + val topics = createTopics(1, itemsPerPage) + showTopics(*topics.toTypedArray()).also { stage = 1 } + } + in 1..shelfItemsNames.size -> { // Specific stages for each page + val currentPage = stage / (itemsPerPage + 1) + val startIndex = currentPage * itemsPerPage + + if (buttonID == itemsPerPage + 1) { // "More" button clicked + if (currentPage < totalPages) { + val topics = createTopics(currentPage + 1, itemsPerPage) + showTopics(*topics.toTypedArray()).also { stage = currentPage*4 + 1 } + } else { // Last page, loop back to the first page + val topics = createTopics(1, itemsPerPage) + showTopics(*topics.toTypedArray()).also { stage = 1 } + } + } else { // Handle item selection + val itemIndex = startIndex + (buttonID - 1) + if (itemIndex < shelfItemsNames.size) { + checkItem(player!!, itemIndex) + } + } + } else -> end() } }