Check size of shelf in a better way

This commit is contained in:
gregf36665 2024-12-22 22:30:24 +00:00 committed by Syndromeramo
parent 32293edd1a
commit af02250f88

View file

@ -82,8 +82,10 @@ class ShelfListener : InteractionListener {
) )
} }
private val shelfItemsNames = shelves[shelfId]!!.map { it.first } private val shelf = shelves[shelfId]!!
private val shelfItemsIds = shelves[shelfId]!!.map { it.second } 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){ private fun checkItem(player: Player, idx : Int){
val item = shelfItemsIds[idx] val item = shelfItemsIds[idx]
@ -98,14 +100,14 @@ class ShelfListener : InteractionListener {
private fun createTopics(page: Int, itemsPerPage: Int): MutableList<Topic<Int>> { private fun createTopics(page: Int, itemsPerPage: Int): MutableList<Topic<Int>> {
val startIndex = (page - 1) * itemsPerPage val startIndex = (page - 1) * itemsPerPage
val endIndex = (startIndex + itemsPerPage).coerceAtMost(shelfItemsNames.size) val endIndex = (startIndex + itemsPerPage).coerceAtMost(size)
val topics = shelfItemsNames.subList(startIndex, endIndex) val topics = shelfItemsNames.subList(startIndex, endIndex)
.mapIndexed { index, item -> Topic(item, startIndex + index + 1, skipPlayer = true) } .mapIndexed { index, item -> Topic(item, startIndex + index + 1, skipPlayer = true) }
.toMutableList() .toMutableList()
// Add "More" button if there's another page or to loop back // 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)) topics.add(Topic("More", endIndex + 1, skipPlayer = true))
} }
@ -122,11 +124,11 @@ class ShelfListener : InteractionListener {
val itemsPerPage = 4 val itemsPerPage = 4
when (stage) { 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) val topics = createTopics(1, itemsPerPage)
showTopics(*topics.toTypedArray()).also { stage = 1 } showTopics(*topics.toTypedArray()).also { stage = 1 }
} }
in 1..shelfItemsNames.size -> { in 1..size -> {
val currentPage = (stage-1) / itemsPerPage val currentPage = (stage-1) / itemsPerPage
val startIndex = currentPage * itemsPerPage val startIndex = currentPage * itemsPerPage
@ -135,7 +137,7 @@ class ShelfListener : InteractionListener {
showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 } showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 }
} else { // Handle item selection } else { // Handle item selection
val itemIndex = startIndex + (buttonID - 1) val itemIndex = startIndex + (buttonID - 1)
if (itemIndex < shelfItemsNames.size) { if (itemIndex < size) {
checkItem(player!!, itemIndex) checkItem(player!!, itemIndex)
} }
} }