aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt
blob: b343b878a30f393b1d0d0d6639f184e69719f44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package at.hannibal2.skyhanni.utils

import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
import net.minecraft.inventory.Slot
import net.minecraft.item.ItemStack

object InventoryUtils {

    //TODO use this method more widely
    fun currentlyOpenInventory(): String {
        val screen = Minecraft.getMinecraft().currentScreen
        if (screen !is GuiChest) return ""
        val inventorySlots = screen.inventorySlots
        val chest = inventorySlots as ContainerChest

        return chest.getInventoryName()
    }

    fun getItemsInOpenChest(): List<Slot> {
        val list = mutableListOf<Slot>()
        val guiChest = Minecraft.getMinecraft().currentScreen as GuiChest
        val inventorySlots = guiChest.inventorySlots.inventorySlots
        val skipAt = inventorySlots.size - 9 * 4
        var i = 0
        for (slot in inventorySlots) {
            val stack = slot.stack
            if (stack != null) {
                list.add(slot)
            }
            i++
            if (i == skipAt) break
        }
        return list
    }

    fun openInventoryName(): String {
        val guiChest = Minecraft.getMinecraft().currentScreen
        val chestName = if (guiChest is GuiChest) {
            val chest = guiChest.inventorySlots as ContainerChest
            chest.getInventoryName()
        } else ""
        return chestName
    }
    
    fun ContainerChest.getInventoryName() = this.lowerChestInventory.displayName.unformattedText.trim()

    fun getItemsInOwnInventory(): MutableList<ItemStack> {
        val list = mutableListOf<ItemStack>()
        for (itemStack in Minecraft.getMinecraft().thePlayer.inventory.mainInventory) {
            itemStack?.let {
                list.add(it)
            }
        }

        return list
    }

    fun countItemsInLowerInventory(predicate: (ItemStack) -> Boolean) =
        getItemsInOwnInventory().filter { predicate(it) }.sumOf { it.stackSize }

    fun getArmor(): Array<ItemStack?> =
        Minecraft.getMinecraft().thePlayer.inventory.armorInventory

    fun inStorage() = openInventoryName().let { it.contains("Storage") || it.contains("Ender Chest") || it.contains("Backpack") }
}