aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
blob: a4482247fae1f9e040161e208be4ec763fb6c504 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.utils.LorenzUtils.matchRegex
import at.hannibal2.skyhanni.utils.LorenzUtils.removeColor
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.item.ItemStack
import java.util.*

object ItemUtils {
    private val gson = GsonBuilder().setPrettyPrinting().create()

    fun ItemStack.cleanName() = this.displayName.removeColor()

    fun getItemsInOpenChest(): List<ItemStack> {
        val list = mutableListOf<ItemStack>()
        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(stack)
            }
            i++
            if (i == skipAt) break
        }
        return list
    }

    fun isSack(name: String): Boolean = name.endsWith(" Sack")//TODO change

    fun ItemStack.getLore() = getLoree(this)


    fun getLoree(`is`: ItemStack): List<String> {
        val tagCompound = `is`.tagCompound ?: return emptyList()
        val tagList = tagCompound.getCompoundTag("display").getTagList("Lore", 8)
        val list: MutableList<String> = ArrayList()
        for (i in 0 until tagList.tagCount()) {
            list.add(tagList.getStringTagAt(i))
        }
        return list
    }

    fun isCoopSoulBound(stack: ItemStack): Boolean =
        stack.getLore().any {
            it == "§8§l* §8Co-op Soulbound §8§l*" || it == "§8§l* §8Soulbound §8§l*"
        }

    fun isSoulBound(stack: ItemStack): Boolean =
        stack.getLore().any { it == "§8§l* §8Soulbound §8§l*" }

    fun isRecombobulated(stack: ItemStack): Boolean = stack.getLore().any { it.contains("§k") }//TODO use item api

    fun isPet(name: String): Boolean = name.matchRegex("\\[Lvl (.*)] (.*)") && !listOf(
        "Archer",
        "Berserk",
        "Mage",
        "Tank",
        "Healer",
        "➡",
    ).any { name.contains(it) }

    fun maxPetLevel(name: String) = if (name.contains("Golden Dragon")) 200 else 100

    fun getItemsInInventory(withCursorItem: Boolean = false): List<ItemStack> {
        val list: LinkedList<ItemStack> = LinkedList()
        val player = Minecraft.getMinecraft().thePlayer
        if (player == null) {
            LorenzUtils.warning("getItemsInInventoryWithSlots: player is null!")
            return list
        }
        for (slot in player.openContainer.inventorySlots) {
            if (slot.hasStack) {
                list.add(slot.stack)
            }
        }

        if (withCursorItem) {
            if (player.inventory != null) {
                if (player.inventory.itemStack != null) {
                    list.add(player.inventory.itemStack)
                }
            }
        }

        return list
    }

    fun getItemsInInventoryWithSlots(withCursorItem: Boolean = false): Map<ItemStack, Int> {
        val map: LinkedHashMap<ItemStack, Int> = LinkedHashMap()
        val player = Minecraft.getMinecraft().thePlayer
        if (player == null) {
            LorenzUtils.warning("getItemsInInventoryWithSlots: player is null!")
            return map
        }
        for (slot in player.openContainer.inventorySlots) {
            if (slot.hasStack) {
                map[slot.stack] = slot.slotNumber
            }
        }

        if (withCursorItem) {
            if (player.inventory != null) {
                if (player.inventory.itemStack != null) {
                    map[player.inventory.itemStack] = -1
                }
            }
        }

        return map
    }

    fun hasAttributes(stack: ItemStack): Boolean {
        if (stack.hasTagCompound()) {
            val tagCompound = stack.tagCompound
            if (tagCompound.hasKey("ExtraAttributes")) {
                val extraAttributes = tagCompound.getCompoundTag("ExtraAttributes")
                try {
                    val json = GsonBuilder().create().fromJson(extraAttributes.toString(), JsonObject::class.java)
                    if (json.has("attributes")) {
                        return true
                    }
                } catch (_: Exception) {
                }
            }
        }
        return false
    }

    fun ItemStack.getInternalName(): String {
        val tag = tagCompound
        if (tag == null || !tag.hasKey("ExtraAttributes", 10)) return ""

        val extraAttributes = tag.getCompoundTag("ExtraAttributes")
        var internalName = if (extraAttributes.hasKey("id", 8)) {
            extraAttributes.getString("id").replace(":".toRegex(), "-")
        } else {
            return ""
        }

        if (internalName == "PET") {
            val petInfo = extraAttributes.getString("petInfo")
            if (petInfo.isNotEmpty()) {
                val petInfoObject: JsonObject = gson.fromJson(petInfo, JsonObject::class.java)
                internalName = petInfoObject["type"].asString
                return when (petInfoObject["tier"].asString) {
                    "COMMON" -> "$internalName;0"
                    "UNCOMMON" -> "$internalName;1"
                    "RARE" -> "$internalName;2"
                    "EPIC" -> "$internalName;3"
                    "LEGENDARY" -> "$internalName;4"
                    "MYTHIC" -> "$internalName;5"
                    else -> internalName
                }
            }
        }

        if (internalName == "ENCHANTED_BOOK" && extraAttributes.hasKey("enchantments", 10)) {
            val enchants = extraAttributes.getCompoundTag("enchantments")
            for (enchantment in enchants.keySet) {
                return enchantment.uppercase(Locale.getDefault()) + ";" + enchants.getInteger(enchantment)
            }
        }

        if (internalName == "RUNE" && extraAttributes.hasKey("runes", 10)) {
            val rune = extraAttributes.getCompoundTag("runes")
            for (enchantment in rune.keySet) {
                return enchantment.uppercase(Locale.getDefault()) + "_RUNE" + ";" + rune.getInteger(enchantment)
            }
        }

        if (internalName == "PARTY_HAT_CRAB" && extraAttributes.getString("party_hat_color") != null) {
            val crabHat = extraAttributes.getString("party_hat_color")
            return "PARTY_HAT_CRAB" + "_" + crabHat.uppercase(Locale.getDefault())
        }

        return internalName
    }
}