manueldidonna / pokemon-save-editor-android

A work-in-progress pokémon save editor for Android
GNU General Public License v3.0
0 stars 0 forks source link

Inventory: Bulk Operations #12

Open manueldidonna opened 4 years ago

manueldidonna commented 4 years ago

Maximize current items quantity

fun Inventory.applyToCurrentItems(quantity: Int = maxAllowedQuantity) {
    val reusableItem = object : Inventory.Item {
        override var index = 0
        override var id = 0
        override val quantity = quantity
    }
    val mutateItem = fun (index: Int, id: Int, quantity: Int) -> Inventory.Item {  
        reusableItem.index = index
        reusableItem.id = id
        return reusableItem
    }
    for (index in 0 until itemCounts) {
        setItem(selectItem(index, mapTo = mutateItem))
    }
}

Give max quantity of all items

fun Inventory.giveAllItems() {
    val itemsCount = supportedItemIds.size.coerceAtMost(capacity)
    val reusableItem = object : Inventory.Item {
        override var index = 0
        override var id = 0
        override val quantity = maxAllowedQuantity
    }
    for (i in 0 until maxSupportedCount) {
        reusableItem.index = i
        reusableItem.id = supportedItemIds[i]
        setItem(reusableItem)
    }
}

Sort items by id

fun Inventory.sortItemsById() {
    val items = MutableList(size) { getItem(it) }
    items.sortBy { it.id }
    items.forEachIndexed { index, item ->
        setItem(item, index)
    }
}