diff options
author | Lorenz <lo.scherf@gmail.com> | 2022-07-29 16:14:04 +0200 |
---|---|---|
committer | Lorenz <lo.scherf@gmail.com> | 2022-07-29 16:14:04 +0200 |
commit | bc2ccc0c39495921aee82664266c1756f016b388 (patch) | |
tree | 5b4e931b96952673fc79822213b721cc8529afbb /src/main/java/at/hannibal2/skyhanni/utils | |
parent | 1cacb26349e3b8bbe6c82ee7fd26782bb98f49ba (diff) | |
download | skyhanni-bc2ccc0c39495921aee82664266c1756f016b388.tar.gz skyhanni-bc2ccc0c39495921aee82664266c1756f016b388.tar.bz2 skyhanni-bc2ccc0c39495921aee82664266c1756f016b388.zip |
added support for vanilla items from neu. allowing all vanilla items to be sold to npc
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ItemUtil.kt | 215 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 66 |
2 files changed, 62 insertions, 219 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtil.kt deleted file mode 100644 index 73f507964..000000000 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtil.kt +++ /dev/null @@ -1,215 +0,0 @@ -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) } -}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index 8a822d7b9..db1767214 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -7,9 +7,10 @@ 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.LinkedList +import java.util.* object ItemUtils { + private val gson = GsonBuilder().setPrettyPrinting().create() fun ItemStack.cleanName() = this.displayName.removeColorCodes() @@ -32,7 +33,18 @@ object ItemUtils { fun isSack(name: String): Boolean = name.endsWith(" Sack")//TODO change - fun ItemStack.getLore() = ItemUtil.getItemLore(this) + 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 { @@ -120,7 +132,53 @@ object ItemUtils { return false } - fun ItemStack.getSBItemID(): String { - return ItemUtil.getSkyBlockItemID(this) ?: "" + 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 } }
\ No newline at end of file |