From 7468902b43ffa626962c8b3beabb697e2b4dba7a Mon Sep 17 00:00:00 2001 From: whatsy Date: Tue, 1 Dec 2026 18:54:57 -0500 Subject: [PATCH] sort button implementation --- .../mod/common/api/bag/BagCategory.kt | 30 +++++----- .../mod/common/api/bag/BagSortComparators.kt | 16 ++++- .../client/gui/bag/BagCycleSortButton.kt | 60 +++++++++++++++++++ .../client/gui/bag/BagSortAscendingButton.kt | 58 ++++++++++++++++++ .../client/gui/bag/CobblemonBagScreen.kt | 47 ++++++++++++++- .../net/bag/UpdateBagMenuDataHandler.kt | 1 + .../mod/common/item/bag/CobblemonBagMenu.kt | 55 +++++++++-------- .../client/bag/UpdateBagMenuDataPacket.kt | 5 +- .../assets/cobblemon/lang/en_us.json | 5 ++ .../cobblemon/bag_categories/held_evo.json | 6 +- .../data/cobblemon/bag_categories/tms.json | 5 +- 11 files changed, 243 insertions(+), 45 deletions(-) create mode 100644 common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagCycleSortButton.kt create mode 100644 common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagSortAscendingButton.kt diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagCategory.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagCategory.kt index 0d6f6d4c06..3fe8c50a9a 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagCategory.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagCategory.kt @@ -9,8 +9,6 @@ package com.cobblemon.mod.common.api.bag import com.cobblemon.mod.common.util.readIdentifier -import com.cobblemon.mod.common.util.readList -import com.cobblemon.mod.common.util.writeCollection import com.cobblemon.mod.common.util.writeIdentifier import net.minecraft.core.registries.Registries import net.minecraft.network.RegistryFriendlyByteBuf @@ -29,13 +27,9 @@ class BagCategory( val whiteListTag: TagKey, val blackListTag: TagKey, val maxCount: Int, - val sortingMethods: MutableList? + val defaultSort: ResourceLocation?, + val sortMethods: List? ): StringRepresentable { - data class SortingComparator( - val id: ResourceLocation, - val reversed: Boolean = false - ) - @Transient var identifier: ResourceLocation = identifier internal set @@ -50,6 +44,13 @@ class BagCategory( return false } + fun getAllSortMethods(): List { + val returnList = mutableListOf() + returnList.add(defaultSort ?: BagSortComparators.DEFAULT_SORT) + sortMethods?.let { returnList.addAll(it) } + return returnList + } + fun encode(buffer: RegistryFriendlyByteBuf) { buffer.writeIdentifier(identifier) buffer.writeInt(order) @@ -57,10 +58,10 @@ class BagCategory( buffer.writeIdentifier(whiteListTag.location) buffer.writeIdentifier(blackListTag.location) buffer.writeInt(maxCount) - buffer.writeCollection(sortingMethods) { buffer, method -> - buffer.writeIdentifier(method.id) - buffer.writeBoolean(method.reversed) - } + buffer.writeIdentifier(defaultSort ?: BagSortComparators.DEFAULT_SORT) + val sortMethods = sortMethods.orEmpty() + buffer.writeInt(sortMethods.size) + buffer.writeCollection(sortMethods) { buf, method -> buf.writeIdentifier(method) } } companion object { @@ -72,9 +73,8 @@ class BagCategory( TagKey.create(Registries.ITEM, buffer.readIdentifier()), TagKey.create(Registries.ITEM, buffer.readIdentifier()), buffer.readInt(), - buffer.readList { - SortingComparator(buffer.readIdentifier(), buffer.readBoolean()) - }.toMutableList() + buffer.readIdentifier(), + buffer.readList { it.readIdentifier() } ) } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagSortComparators.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagSortComparators.kt index c647ce6132..591e7d7618 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagSortComparators.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/bag/BagSortComparators.kt @@ -4,13 +4,18 @@ import com.cobblemon.mod.common.api.events.CobblemonEvents import com.cobblemon.mod.common.util.cobblemonResource import net.minecraft.resources.ResourceLocation import net.minecraft.world.item.CreativeModeTabs +import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack object BagSortComparators { private val SORT_METHODS = mutableMapOf>() + + val INPUT_ORDER = cobblemonResource("input_order") val ALPHABETICAL = cobblemonResource("alphabetical") val CREATIVE_MENU = cobblemonResource("creative_menu") + val DEFAULT_SORT = CREATIVE_MENU + fun registerComparator(type: ResourceLocation, comparator: Comparator) { SORT_METHODS[type] = comparator } @@ -19,9 +24,18 @@ object BagSortComparators { return SORT_METHODS[type] } + private val creativeItemOrder: Map by lazy { + buildMap { + CreativeModeTabs.searchTab().displayItems.forEachIndexed { index, stack -> + getOrPut(stack.item) { index } + } + } + } + fun setupComparators() { registerComparator(ALPHABETICAL, compareBy { it.displayName.toString() }) - registerComparator(CREATIVE_MENU, compareBy { CreativeModeTabs.searchTab().displayItems.indexOf(it) } ) + registerComparator(CREATIVE_MENU, compareBy { creativeItemOrder[it.item] ?: Int.MAX_VALUE }) + registerComparator(INPUT_ORDER, compareBy { null }) CobblemonEvents.BAG_SORTING_REGISTRATION.post(BagSortComparatorRegistryEvent()) { event -> event.comparators.forEach { (type, comparator) -> registerComparator(type, comparator) } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagCycleSortButton.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagCycleSortButton.kt new file mode 100644 index 0000000000..a2bda1d827 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagCycleSortButton.kt @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.client.gui.bag + +import com.cobblemon.mod.common.api.bag.BagCategories +import com.cobblemon.mod.common.api.gui.blitk +import com.cobblemon.mod.common.client.gui.CobblemonRenderable +import com.cobblemon.mod.common.util.cobblemonResource +import com.cobblemon.mod.common.util.lang +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.GuiGraphics +import net.minecraft.client.gui.components.Button +import net.minecraft.client.sounds.SoundManager +import net.minecraft.network.chat.Component + +class BagCycleSortButton( + x: Int, + y: Int, + val parent: CobblemonBagScreen, + val action: OnPress +): Button(x, y, WIDTH, HEIGHT, Component.empty(), action, DEFAULT_NARRATION), CobblemonRenderable { + companion object { + private const val WIDTH = 12 + private const val HEIGHT = 10 + + private val LEFT_BUTTON = cobblemonResource("textures/gui/bag/button_left.png") + } + + var enabled = false + + override fun renderWidget(context: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) { + if (!enabled) return + blitk( + matrixStack = context.pose(), + texture = LEFT_BUTTON, + x = x / 0.5F, y = y / 0.5F, + width = WIDTH * 2, height = HEIGHT * 2, + textureHeight = 40, + vOffset = if (enabled) 20 else 0, + scale = 0.5f + ) + + if (isHovered) { + val currentCategory = BagCategories.getOrderedCategoriesList().getOrNull(parent.menu.currentlySelectedCategory) + val currentSort = currentCategory?.getAllSortMethods()[parent.menu.currentlySelectedSort] + currentSort?.let { + val lang = lang("bag.sort.${it.path}") + context.renderTooltip(Minecraft.getInstance().font, lang, mouseX, mouseY) + } + } + } + + override fun playDownSound(handler: SoundManager) {} +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagSortAscendingButton.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagSortAscendingButton.kt new file mode 100644 index 0000000000..042dae7d3d --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/BagSortAscendingButton.kt @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2023 Cobblemon Contributors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package com.cobblemon.mod.common.client.gui.bag + +import com.cobblemon.mod.common.api.gui.blitk +import com.cobblemon.mod.common.client.gui.CobblemonRenderable +import com.cobblemon.mod.common.util.cobblemonResource +import com.cobblemon.mod.common.util.lang +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.GuiGraphics +import net.minecraft.client.gui.components.Button +import net.minecraft.client.sounds.SoundManager +import net.minecraft.network.chat.Component + +class BagSortAscendingButton( + x: Int, + y: Int, + val parent: CobblemonBagScreen, + val action: OnPress +): Button(x, y, WIDTH, HEIGHT, Component.empty(), action, DEFAULT_NARRATION), CobblemonRenderable { + companion object { + private const val WIDTH = 12 + private const val HEIGHT = 10 + + private val LEFT_BUTTON = cobblemonResource("textures/gui/bag/button_left.png") + } + + var enabled = false + + override fun renderWidget(context: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) { + if (!enabled) return + blitk( + matrixStack = context.pose(), + texture = LEFT_BUTTON, + x = x / 0.5F, y = y / 0.5F, + width = WIDTH * 2, height = HEIGHT * 2, + textureHeight = 40, + vOffset = if (enabled) 20 else 0, + scale = 0.5f + ) + + if (isHovered) { + if (parent.menu.ascending) { + context.renderTooltip(Minecraft.getInstance().font, lang("bag.sort.ascending"), mouseX, mouseY) + } else { + context.renderTooltip(Minecraft.getInstance().font, lang("bag.sort.descending"), mouseX, mouseY) + } + } + } + + override fun playDownSound(handler: SoundManager) {} +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/CobblemonBagScreen.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/CobblemonBagScreen.kt index 7f6bc71789..7bd92f4f8a 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/CobblemonBagScreen.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/bag/CobblemonBagScreen.kt @@ -74,6 +74,8 @@ class CobblemonBagScreen( lateinit var allItemsCategoryWidget: BagCategoryButton lateinit var leftCategoryPageButton: BagCategoryPageButton lateinit var rightCategoryPageButton: BagCategoryPageButton + lateinit var cycleCategorySortButton: BagCycleSortButton + lateinit var ascendingButton: BagSortAscendingButton var currentCategoryPage = 0 var lastCategory = menu.currentlySelectedCategory var lastScroll = menu.scroll @@ -94,6 +96,7 @@ class CobblemonBagScreen( BagCategoryButton(leftPos + 76 + i * 33, topPos, this, i) { if (menu.currentlySelectedCategory != i) { menu.changeCategory(i) + updateSortButtonState(false) playSound(CobblemonSounds.ITEM_BAG_CLICK) } }.also { addRenderableWidget(it) } @@ -104,6 +107,7 @@ class CobblemonBagScreen( allItemsCategoryWidget = BagCategoryButton(leftPos + 214, topPos, this, -1) { if (menu.currentlySelectedCategory != -1) { menu.changeCategory(-1) + updateSortButtonState(false) playSound(CobblemonSounds.ITEM_BAG_CLICK) } }.also { addRenderableWidget(it) } @@ -114,12 +118,27 @@ class CobblemonBagScreen( playSound(CobblemonSounds.ITEM_BAG_CLICK) } }.also { addRenderableWidget(it) } + leftCategoryPageButton.enabled = false rightCategoryPageButton = BagCategoryPageButton(leftPos + 229, topPos + 32, this, false) { if (rightCategoryPageButton.enabled) { cycleCategoryPage(true) playSound(CobblemonSounds.ITEM_BAG_CLICK) } }.also { addRenderableWidget(it) } + cycleCategorySortButton = BagCycleSortButton( + leftPos + 200, topPos + 32, this + ) { + updateSortButtonState(true) + }.also { addRenderableWidget(it) } + ascendingButton = BagSortAscendingButton( + leftPos + 180, topPos + 32, this + ) { + if (ascendingButton.enabled) { + playSound(CobblemonSounds.ITEM_BAG_CLICK) + menu.ascending = !menu.ascending + menu.setupSlotsBasedOnContent() + } + }.also { addRenderableWidget(it) } playSound(CobblemonSounds.ITEM_BAG_OPEN) } @@ -334,12 +353,38 @@ class CobblemonBagScreen( BagCategoryButton(leftPos + 76 + i * 33, topPos, this, adjustedIndex) { playSound(CobblemonSounds.ITEM_BAG_CLICK) menu.changeCategory(adjustedIndex) + updateSortButtonState(false) }.also { addRenderableWidget(it) } ) } else break } leftCategoryPageButton.enabled = currentCategoryPage > 0 rightCategoryPageButton.enabled = currentCategoryPage < (orderedCategories.size - 1) / 4 + updateSortButtonState(false) + } + + /** Updates the state of [cycleCategorySortButton]. If cycle = true, will cycle to the next sort type. */ + private fun updateSortButtonState(cycle: Boolean) { + val currentCategory = BagCategories.getOrderedCategoriesList().getOrNull(menu.currentlySelectedCategory) + if (menu.currentlySelectedCategory == -1) { + cycleCategorySortButton.enabled = false + ascendingButton.enabled = false + return + } + + cycleCategorySortButton.enabled = currentCategory != null && currentCategory.getAllSortMethods().size > 1 + ascendingButton.enabled = currentCategory != null && currentCategory.getAllSortMethods().size > 1 + + if (cycle && cycleCategorySortButton.enabled) { + val currentCategory = BagCategories.getOrderedCategoriesList().getOrNull(menu.currentlySelectedCategory) ?: return + + playSound(CobblemonSounds.ITEM_BAG_CLICK) + if (menu.currentlySelectedSort == currentCategory.getAllSortMethods().size-1) { + menu.currentlySelectedSort = 0 + } else { menu.currentlySelectedSort++ } + menu.setupSlotsBasedOnContent() + sendToServer(UpdateBagMenuDataPacket(menu.currentlySelectedCategory, menu.scroll, menu.currentlySelectedSort)) + } } fun updateScrollValue(mouseY: Double) { @@ -358,7 +403,7 @@ class CobblemonBagScreen( if (slotId in CobblemonBagMenu.BAG_SLOTS && (lastCategory != menu.currentlySelectedCategory || lastScroll != menu.scroll)) { lastCategory = menu.currentlySelectedCategory lastScroll = menu.scroll - sendToServer(UpdateBagMenuDataPacket(menu.currentlySelectedCategory, menu.scroll)) // Synchronize this data + sendToServer(UpdateBagMenuDataPacket(menu.currentlySelectedCategory, menu.scroll, menu.currentlySelectedSort)) // Synchronize this data } super.slotClicked(slot, slotId, mouseButton, type) } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/net/bag/UpdateBagMenuDataHandler.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/net/bag/UpdateBagMenuDataHandler.kt index 895b829c94..c5fc66d35c 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/net/bag/UpdateBagMenuDataHandler.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/net/bag/UpdateBagMenuDataHandler.kt @@ -25,6 +25,7 @@ object UpdateBagMenuDataHandler : ServerNetworkPacketHandler() + var currentlySelectedSort = 0 + var ascending = true constructor(containerId: Int, playerInventory: Inventory): super(CobblemonMenuType.COBBLEMON_BAG, containerId) { this.playerInventory = playerInventory @@ -80,6 +82,23 @@ class CobblemonBagMenu: AbstractContainerMenu { calculateCategoriesItemCounts() } + private fun sort(items: MutableList, category: BagCategory?): List { + if (category == null) return items + + val sortMethod = category.getAllSortMethods().getOrNull(currentlySelectedSort) + ?: category.defaultSort + ?: BagSortComparators.DEFAULT_SORT + val comparator = BagSortComparators.getComparator(sortMethod) + ?: BagSortComparators.getComparator(category.defaultSort ?: BagSortComparators.DEFAULT_SORT) + ?: return items + + return if (ascending) { + items.sortedWith(comparator) + } else { + items.sortedWith(comparator).asReversed() + } + } + private fun initializeSlots(playerInventory: Inventory) { for ((index, _) in BAG_SLOTS.withIndex()) { val i = index / 8 @@ -122,35 +141,22 @@ class CobblemonBagMenu: AbstractContainerMenu { } } + val items = bagContainer.items + val indexByStack = mutableMapOf() + items.forEachIndexed { i, stack -> indexByStack[stack] = i } + val orderedCategories = BagCategories.getOrderedCategoriesList() + val category = if (currentlySelectedCategory == -1) null else orderedCategories[currentlySelectedCategory] + val sortedItems = sort(items, category) var slotId = BAG_SLOTS.first - 8 * scroll - val orderedList = bagContainer.items.toMutableList(); - if (currentlySelectedCategory != -1) { - val comparatorList = BagCategories.getOrderedCategoriesList()[currentlySelectedCategory].sortingMethods - if (comparatorList != null && comparatorList.isNotEmpty()) { - var finalComparator: Comparator? = null - comparatorList.forEach { - var comparator = BagSortComparators.getComparator(it.id) - if (comparator != null) { - if (it.reversed) comparator = comparator.reversed() - finalComparator = if (finalComparator == null) comparator - else finalComparator.thenComparing(comparator) - } - } - - if (finalComparator != null) { - orderedList.sortWith(finalComparator) - } - } - } - - orderedList.forEach { itemStack -> - if (currentlySelectedCategory == -1 || BagCategories.getOrderedCategoriesList()[currentlySelectedCategory].itemIsValid(itemStack)) { + sortedItems.forEach { itemStack -> + val index = indexByStack[itemStack] ?: return@forEach + if (category == null || category.itemIsValid(itemStack)) { val stackCount = ceil(itemStack.count.toFloat() / itemStack.maxStackSize).toInt() for (i in 0.. { +class UpdateBagMenuDataPacket(val categoryId: Int, val scroll: Int, val sort: Int): NetworkPacket { companion object { val ID = cobblemonResource("update_bag_menu_data") fun decode(buffer: RegistryFriendlyByteBuf): UpdateBagMenuDataPacket { - return UpdateBagMenuDataPacket(buffer.readInt(), buffer.readInt()) + return UpdateBagMenuDataPacket(buffer.readInt(), buffer.readInt(), buffer.readInt()) } } @@ -24,5 +24,6 @@ class UpdateBagMenuDataPacket(val categoryId: Int, val scroll: Int): NetworkPack override fun encode(buffer: RegistryFriendlyByteBuf) { buffer.writeInt(categoryId) buffer.writeInt(scroll) + buffer.writeInt(sort) } } \ No newline at end of file diff --git a/common/src/main/resources/assets/cobblemon/lang/en_us.json b/common/src/main/resources/assets/cobblemon/lang/en_us.json index 9b3a64542c..87426d68d2 100644 --- a/common/src/main/resources/assets/cobblemon/lang/en_us.json +++ b/common/src/main/resources/assets/cobblemon/lang/en_us.json @@ -1208,6 +1208,11 @@ "cobblemon.bag_category.tms": "Technical Machines", "cobblemon.bag_category.currency": "Currency", "cobblemon.bag_categories.all_items": "Bag", + "cobblemon.bag.sort.alphabetical": "Alphabetical", + "cobblemon.bag.sort.input_order": "Input Order", + "cobblemon.bag.sort.creative_menu": "Creative Order", + "cobblemon.bag.sort.ascending": "Ascending", + "cobblemon.bag.sort.descending": "Descending", "cobblemon.ui.pokenav.title": "PokéNav", "cobblemon.ui.pokenav.narrator.backbutton": "PokéNav button pressed", diff --git a/common/src/main/resources/data/cobblemon/bag_categories/held_evo.json b/common/src/main/resources/data/cobblemon/bag_categories/held_evo.json index d9880a3181..a064ab687b 100644 --- a/common/src/main/resources/data/cobblemon/bag_categories/held_evo.json +++ b/common/src/main/resources/data/cobblemon/bag_categories/held_evo.json @@ -3,5 +3,9 @@ "icon": "cobblemon:textures/item/held_items/exp_share.png", "whiteListTag": "cobblemon:bag/whitelist_held_evo", "blackListTag": "cobblemon:bag/blacklist_held_evo", - "maxCount": 128 + "maxCount": 128, + "sortMethods": [ + "cobblemon:input_order", + "cobblemon:creative_menu" + ] } \ No newline at end of file diff --git a/common/src/main/resources/data/cobblemon/bag_categories/tms.json b/common/src/main/resources/data/cobblemon/bag_categories/tms.json index 6e96ced4a1..3561893799 100644 --- a/common/src/main/resources/data/cobblemon/bag_categories/tms.json +++ b/common/src/main/resources/data/cobblemon/bag_categories/tms.json @@ -3,5 +3,8 @@ "icon": "cobblemon:textures/item/blank_disc.png", "whiteListTag": "cobblemon:bag/whitelist_tms", "blackListTag": "cobblemon:bag/blacklist_tms", - "maxCount": 64 + "maxCount": 64, + "sortMethods": [ + "cobblemon:input_order" + ] } \ No newline at end of file