From af02250f88e58506dea5b6ded395c70488e3574b Mon Sep 17 00:00:00 2001 From: gregf36665 Date: Sun, 22 Dec 2024 22:30:24 +0000 Subject: [PATCH] Check size of shelf in a better way --- .../decoration/kitchen/ShelfListener.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 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 9c81b55ec..7df0c7dfe 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 @@ -82,8 +82,10 @@ class ShelfListener : InteractionListener { ) } - private val shelfItemsNames = shelves[shelfId]!!.map { it.first } - private val shelfItemsIds = shelves[shelfId]!!.map { it.second } + private val shelf = shelves[shelfId]!! + private val shelfItemsNames = shelf.map { it.first } + private val shelfItemsIds = shelf.map { it.second } + private val size = shelf.size private fun checkItem(player: Player, idx : Int){ val item = shelfItemsIds[idx] @@ -98,14 +100,14 @@ class ShelfListener : InteractionListener { private fun createTopics(page: Int, itemsPerPage: Int): MutableList> { val startIndex = (page - 1) * itemsPerPage - val endIndex = (startIndex + itemsPerPage).coerceAtMost(shelfItemsNames.size) + val endIndex = (startIndex + itemsPerPage).coerceAtMost(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) { + if (endIndex < size || page > 1) { topics.add(Topic("More", endIndex + 1, skipPlayer = true)) } @@ -122,11 +124,11 @@ class ShelfListener : InteractionListener { val itemsPerPage = 4 when (stage) { - 0, shelfItemsNames.size + 1 -> { // Initial stage or final more button clicked + 0, size + 1 -> { // Initial stage or final more button clicked val topics = createTopics(1, itemsPerPage) showTopics(*topics.toTypedArray()).also { stage = 1 } } - in 1..shelfItemsNames.size -> { + in 1..size -> { val currentPage = (stage-1) / itemsPerPage val startIndex = currentPage * itemsPerPage @@ -135,7 +137,7 @@ class ShelfListener : InteractionListener { showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 } } else { // Handle item selection val itemIndex = startIndex + (buttonID - 1) - if (itemIndex < shelfItemsNames.size) { + if (itemIndex < size) { checkItem(player!!, itemIndex) } }