diff --git a/Server/src/main/content/global/skill/construction/decoration/kitchen/AbstractContainer.kt b/Server/src/main/content/global/skill/construction/decoration/kitchen/AbstractContainer.kt new file mode 100644 index 000000000..cd3426c7a --- /dev/null +++ b/Server/src/main/content/global/skill/construction/decoration/kitchen/AbstractContainer.kt @@ -0,0 +1,79 @@ +package content.global.skill.construction.decoration.kitchen + +import core.api.addItem +import core.api.hasSpaceFor +import core.api.sendDialogue +import core.game.dialogue.DialogueFile +import core.game.dialogue.Topic +import core.game.node.entity.player.Player +import core.tools.END_DIALOGUE + +abstract class AbstractContainer(containerId: Int, containers: Map>>, private val containerName: String) : DialogueFile() { + + private val container = containers[containerId]!! + private val containerItemsNames = container.map { it.first } + private val containerItemsIds = container.map { it.second } + private val size = container.size + private val itemsPerPage = 4 + + private fun checkItem(player: Player, idx : Int){ + val item = containerItemsIds[idx] + 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 } + } + } + + private fun createTopics(page: Int): MutableList> { + val startIndex = (page - 1) * itemsPerPage + val endIndex = (startIndex + itemsPerPage).coerceAtMost(size) + + val topics = containerItemsNames.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 < size || page > 1) { + topics.add(Topic("More", endIndex + 1, skipPlayer = true)) + } + + return topics + } + + + + override fun handle(componentID: Int, buttonID: Int) { + if (player!!.inventory.freeSlot() < 1) { + sendDialogue(player!!, "You need at least one free inventory space to take from the $containerName.").also { stage = END_DIALOGUE } + return + } + val itemsPerPage = 4 + + when (stage) { + 0, size + 1 -> { // Initial stage or final more button clicked + val topics = createTopics(1) + showTopics(*topics.toTypedArray()).also { stage = 1 } + } + in 1..size -> { + val currentPage = (stage-1) / itemsPerPage + val startIndex = currentPage * itemsPerPage + + if (buttonID == itemsPerPage + 1) { // "More" button clicked (but not the last one) + val topics = createTopics(currentPage + 1) + showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 } + } else { // Handle item selection + val itemIndex = startIndex + (buttonID - 1) + if (itemIndex < size) { + checkItem(player!!, itemIndex) + } + } + } + else -> end() + } + } + + +} \ No newline at end of file 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 7df0c7dfe..57761e0c6 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 @@ -12,138 +12,85 @@ import kotlin.math.min class ShelfListener : InteractionListener { + companion object { + // Source for text https://www.youtube.com/watch?v=SNuqelEft3Y + + private val woodshelf_1_items = listOf( + "Kettle" to Items.KETTLE_7688, + "Teapot" to Items.TEAPOT_7702, + "Cup" to Items.EMPTY_CUP_7728 + ) + + private val woodshelf_2_items = woodshelf_1_items + listOf( + "Beer glass" to Items.BEER_GLASS_1919 + ) + + private val woodshelf_3_items = woodshelf_2_items + listOf( + "Cake tin" to Items.CAKE_TIN_1887 + ) + + private val oakshelf_1_items = woodshelf_3_items + listOf( + "Bowl" to Items.BOWL_1923, + ) + + private val oakshelf_2_items = oakshelf_1_items.map { (name, itemId) -> + when (name) { + "Cup" -> { + "Porcelain cup" to Items.PORCELAIN_CUP_7732 + } + + "Teapot" -> { + "Teapot" to Items.TEAPOT_7714 + } + + else -> { + name to itemId + } + } + } + listOf("Pie dish" to Items.PIE_DISH_2313) + + private val teakshelf_1_items = oakshelf_2_items + listOf( + "Pot" to Items.EMPTY_POT_1931 + ) + + private val teakshelf_2_items = teakshelf_1_items.map { (name, itemId) -> + when (name) { + "Porcelain cup" -> { + "Porcelain cup" to Items.PORCELAIN_CUP_7735 + } + + "Teapot" -> { + "Teapot" to Items.TEAPOT_7726 + } + + else -> { + name to itemId + } + } + } + listOf("Chef's Hat" to Items.CHEFS_HAT_1949) + + val shelves = mapOf( + Scenery.SHELVES_13545 to woodshelf_1_items, + Scenery.SHELVES_13546 to woodshelf_2_items, + Scenery.SHELVES_13547 to woodshelf_3_items, + Scenery.SHELVES_13548 to oakshelf_1_items, + Scenery.SHELVES_13549 to oakshelf_2_items, + Scenery.SHELVES_13550 to teakshelf_1_items, + Scenery.SHELVES_13551 to teakshelf_2_items, + ) + } override fun defineListeners() { - on(ShelfDialogue.shelves.keys.toIntArray(), IntType.SCENERY, "Search") {player, node -> + on(shelves.keys.toIntArray(), IntType.SCENERY, "Search") { player, node -> searchShelf(player, node.id) return@on true } } private fun searchShelf(player : Player, shelfId: Int){ - openDialogue(player, ShelfDialogue(shelfId)) + openDialogue(player, ShelfDialogue(shelfId, shelves)) } - class ShelfDialogue(shelfId: Int) : DialogueFile() { + class ShelfDialogue(shelfId: Int, shelves : Map>>) : AbstractContainer(shelfId, shelves, "shelf") - companion object{ - - const val NEXT = 5 - const val PREV = -5 - - // Source for text https://www.youtube.com/watch?v=SNuqelEft3Y - - private val woodshelf_1_items = listOf( - "Kettle" to Items.KETTLE_7688, - "Teapot" to Items.TEAPOT_7702, - "Cup" to Items.EMPTY_CUP_7728 - ) - - private val woodshelf_2_items = woodshelf_1_items + listOf( - "Beer glass" to Items.BEER_GLASS_1919 - ) - - private val woodshelf_3_items = woodshelf_2_items + listOf( - "Cake tin" to Items.CAKE_TIN_1887 - ) - - private val oakshelf_1_items = woodshelf_3_items + listOf( - "Bowl" to Items.BOWL_1923, - ) - - private val oakshelf_2_items = oakshelf_1_items.map { (name, itemId) -> - when (name) { - "Cup" -> { "Porcelain cup" to Items.PORCELAIN_CUP_7732 } - "Teapot" -> { "Teapot" to Items.TEAPOT_7714 } - else -> { name to itemId} - } - } + listOf( "Pie dish" to Items.PIE_DISH_2313 ) - - private val teakshelf_1_items = oakshelf_2_items + listOf( - "Pot" to Items.EMPTY_POT_1931 - ) - - private val teakshelf_2_items = teakshelf_1_items.map { (name, itemId) -> - when (name) { - "Porcelain cup" -> { "Porcelain cup" to Items.PORCELAIN_CUP_7735 } - "Teapot" -> { "Teapot" to Items.TEAPOT_7726 } - else -> { name to itemId } - } - } + listOf( "Chef's Hat" to Items.CHEFS_HAT_1949 ) - - val shelves = mapOf( - Scenery.SHELVES_13545 to woodshelf_1_items, - Scenery.SHELVES_13546 to woodshelf_2_items, - Scenery.SHELVES_13547 to woodshelf_3_items, - Scenery.SHELVES_13548 to oakshelf_1_items, - Scenery.SHELVES_13549 to oakshelf_2_items, - Scenery.SHELVES_13550 to teakshelf_1_items, - Scenery.SHELVES_13551 to teakshelf_2_items, - ) - } - - 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] - 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 } - } - } - - private fun createTopics(page: Int, itemsPerPage: Int): MutableList> { - val startIndex = (page - 1) * itemsPerPage - 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 < size || page > 1) { - topics.add(Topic("More", endIndex + 1, skipPlayer = true)) - } - - return topics - } - - - - override fun handle(componentID: Int, buttonID: Int) { - 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 - - when (stage) { - 0, size + 1 -> { // Initial stage or final more button clicked - val topics = createTopics(1, itemsPerPage) - showTopics(*topics.toTypedArray()).also { stage = 1 } - } - in 1..size -> { - val currentPage = (stage-1) / itemsPerPage - val startIndex = currentPage * itemsPerPage - - if (buttonID == itemsPerPage + 1) { // "More" button clicked (but not the last one) - val topics = createTopics(currentPage + 1, itemsPerPage) - showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 } - } else { // Handle item selection - val itemIndex = startIndex + (buttonID - 1) - if (itemIndex < size) { - checkItem(player!!, itemIndex) - } - } - } - else -> end() - } - } - } } \ No newline at end of file