aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/ItemUtil.kt
blob: 73f50796413baba81368b286d68f08c177f2f5fc (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package at.hannibal2.skyhanni.utils

import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.nbt.NBTTagList
import net.minecraft.nbt.NBTTagString
import net.minecraftforge.common.util.Constants
import java.util.*

object ItemUtil {
    private val PET_PATTERN = "§7\\[Lvl \\d+] (?<color>§[0-9a-fk-or]).+".toRegex()
    const val NBT_INTEGER = 3
    private const val NBT_STRING = 8
    private const val NBT_LIST = 9
    private const val NBT_COMPOUND = 10

    /**
     * Returns the display name of a given item
     * @author Mojang
     * @param item the Item to get the display name of
     * @return the display name of the item
     */
    @JvmStatic
    fun getDisplayName(item: ItemStack): String {
        var s = item.item.getItemStackDisplayName(item)
        if (item.tagCompound != null && item.tagCompound.hasKey("display", 10)) {
            val nbtTagCompound = item.tagCompound.getCompoundTag("display")
            if (nbtTagCompound.hasKey("Name", 8)) {
                s = nbtTagCompound.getString("Name")
            }
        }
        return s
    }

    /**
     * Returns the Skyblock Item ID of a given Skyblock item
     *
     * @author BiscuitDevelopment
     * @param item the Skyblock item to check
     * @return the Skyblock Item ID of this item or `null` if this isn't a valid Skyblock item
     */
    @JvmStatic
    fun getSkyBlockItemID(item: ItemStack?): String? {
        if (item == null) {
            return null
        }
        val extraAttributes = getExtraAttributes(item) ?: return null
        return if (!extraAttributes.hasKey("id", NBT_STRING)) {
            null
        } else extraAttributes.getString("id")
    }

    /**
     * Returns the `ExtraAttributes` compound tag from the item's NBT data.
     *
     * @author BiscuitDevelopment
     * @param item the item to get the tag from
     * @return the item's `ExtraAttributes` compound tag or `null` if the item doesn't have one
     */
    @JvmStatic
    fun getExtraAttributes(item: ItemStack?): NBTTagCompound? {
        return if (item == null || !item.hasTagCompound()) {
            null
        } else item.getSubCompound("ExtraAttributes", false)
    }

    /**
     * Returns the Skyblock Item ID of a given Skyblock Extra Attributes NBT Compound
     *
     * @author BiscuitDevelopment
     * @param extraAttributes the NBT to check
     * @return the Skyblock Item ID of this item or `null` if this isn't a valid Skyblock NBT
     */
    @JvmStatic
    fun getSkyBlockItemID(extraAttributes: NBTTagCompound?): String? {
        if (extraAttributes != null) {
            val itemId = extraAttributes.getString("id")
            if (itemId.isNotEmpty()) {
                return itemId
            }
        }
        return null
    }

    /**
     * Returns a string list containing the nbt lore of an ItemStack, or
     * an empty list if this item doesn't have a lore. The returned lore
     * list is unmodifiable since it has been converted from an NBTTagList.
     *
     * @author BiscuitDevelopment
     * @param itemStack the ItemStack to get the lore from
     * @return the lore of an ItemStack as a string list
     */
    @JvmStatic
    fun getItemLore(itemStack: ItemStack): List<String> {
        if (itemStack.hasTagCompound() && itemStack.tagCompound.hasKey("display", NBT_COMPOUND)) {
            val display = itemStack.tagCompound.getCompoundTag("display")
            if (display != null) {
                if (display.hasKey("Lore", NBT_LIST)) {
                    val lore = display.getTagList("Lore", NBT_STRING)
                    val loreAsList = ArrayList<String>(lore.tagCount())
                    for (lineNumber in 0 until lore.tagCount()) {
                        loreAsList.add(lore.getStringTagAt(lineNumber))
                    }
                    return Collections.unmodifiableList(loreAsList)
                }
            }
        }
        return emptyList()
    }

//            @JvmStatic
//            fun hasRightClickAbility(itemStack: ItemStack): Boolean {
//                for (line in getItemLore(itemStack)) {
//                    val stripped = line.stripControlCodes()
//                    if (stripped.startsWith("Item Ability:") && stripped.endsWith("RIGHT CLICK")) return true
//                }
//                return false
//            }

//            /**
//             * Returns the rarity of a given Skyblock item
//             * Modified
//             * @author BiscuitDevelopment
//             * @param item the Skyblock item to check
//             * @return the rarity of the item if a valid rarity is found, `null` if no rarity is found, `null` if item is `null`
//             */
//            fun getRarity(item: ItemStack?): ItemRarity {
//                if (item == null || !item.hasTagCompound()) {
//                    return ItemRarity.NONE
//                }
//                val display = item.getSubCompound("display", false)
//                if (display == null || !display.hasKey("Lore")) {
//                    return ItemRarity.NONE
//                }
//                val lore = display.getTagList("Lore", Constants.NBT.TAG_STRING)
//                val name = display.getString("Name")
//
//                // Determine the item's rarity
//                for (i in (lore.tagCount() - 1) downTo 0) {
//                    val currentLine = lore.getStringTagAt(i)
//                    val rarityMatcher = RARITY_PATTERN.find(currentLine)
//                    if (rarityMatcher != null) {
//                        val rarity = rarityMatcher.groups["rarity"]?.value ?: continue
//                        ItemRarity.values().find {
//                            it.rarityName == rarity.stripControlCodes().substringAfter("SHINY ")
//                        }?.let {
//                            return it
//                        }
//                    }
//                }
//                val petRarityMatcher = PET_PATTERN.find(name)
//                if (petRarityMatcher != null) {
//                    val color = petRarityMatcher.groupValues.getOrNull(1) ?: return ItemRarity.NONE
//                    return ItemRarity.byBaseColor(color) ?: ItemRarity.NONE
//                }
//
//                // If the item doesn't have a valid rarity, return null
//                return ItemRarity.NONE
//            }

    fun isPet(item: ItemStack?): Boolean {
        if (item == null || !item.hasTagCompound()) {
            return false
        }
        val display = item.getSubCompound("display", false)
        if (display == null || !display.hasKey("Lore")) {
            return false
        }
        val name = display.getString("Name")

        return PET_PATTERN.matches(name)
    }

    fun setSkullTexture(item: ItemStack, texture: String, SkullOwner: String): ItemStack {
        val textureTagCompound = NBTTagCompound()
        textureTagCompound.setString("Value", texture)

        val textures = NBTTagList()
        textures.appendTag(textureTagCompound)

        val properties = NBTTagCompound()
        properties.setTag("textures", textures)

        val skullOwner = NBTTagCompound()
        skullOwner.setString("Id", SkullOwner)
        skullOwner.setTag("Properties", properties)

        val nbtTag = NBTTagCompound()
        nbtTag.setTag("SkullOwner", skullOwner)

        item.tagCompound = nbtTag
        return item
    }

    fun getSkullTexture(item: ItemStack): String? {
        if (item.item != Items.skull) return null
        val nbt = item.tagCompound
        if (!nbt.hasKey("SkullOwner")) return null
        return nbt.getCompoundTag("SkullOwner").getCompoundTag("Properties")
            .getTagList("textures", Constants.NBT.TAG_COMPOUND).getCompoundTagAt(0).getString("Value")
    }

    fun ItemStack.setLore(lines: List<String>): ItemStack {
        setTagInfo("display", getSubCompound("display", true).apply {
            setTag("Lore", NBTTagList().apply {
                for (line in lines) appendTag(NBTTagString(line))
            })
        })
        return this
    }

    fun NBTTagList.asStringSet() = (0..tagCount()).mapTo(hashSetOf()) { getStringTagAt(it) }
}