package at.hannibal2.skyhanni.utils.const import net.minecraft.item.EnumDyeColor import net.minecraft.item.Item import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTBase import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagString /** * Access a shallow copy of the underlying item stack. Callers of this method promise not to modify the nbt data of * the returned instance, but can modify the stack size, metadata, item or outright replace the root tag compound. * Interior immutability of [ItemStack.stackTagCompound] needs to be guaranteed by the caller. The instance is owned * ergo not shared with anyone else, so that there are no mutability concerns about the ItemStack instance itself. */ fun Const.getOwnedShallowCopy(): ItemStack { return ItemStack(unsafeMutable.item, unsafeMutable.stackSize, unsafeMutable.metadata, unsafeMutable.tagCompound) } /** * Access a copy of the underlying item stack. The returned copy is fully and deeply owned, so changes can be made * to the item stack instance, including interior mutability concerning the [ItemStack.stackTagCompound]. */ fun Const.getOwnedDeepCopy(): ItemStack { return unsafeMutable.copy() } /** * Returns the [ItemStack.stackSize] */ inline val Const.stackSize: Int get() = unsafeMap(ItemStack::stackSize).unconst /** * Returns the [item type](ItemStack.item) */ inline val Const.itemType: Item get() = unsafeMap(ItemStack::getItem).unconst /** * Returns the [damage or metadata](ItemStack.metadata) */ inline val Const.damage: Int get() = unsafeMap(ItemStack::getItemDamage).unconst /** * Interprets the [damage] of this item as a [color](EnumDyeColor). This is only valid for some [item types](itemType), * so check that one first. */ inline val Const.color: EnumDyeColor get() = EnumDyeColor.byDyeDamage(damage) /** * Access the underlying NBT data of this item stack in a [Const]. */ inline val Const.nbt: Const? get() = unsafeMap(ItemStack::getTagCompound).liftNull() /** * Access the NBT data relating to display data. */ inline val Const.displayNbt: Const? get() = nbt?.getTagCompound("display") /** * Get the display name of this item stack according to its NBT. */ fun Const.getDisplayName(): String? = displayNbt?.getString("Name")?.getString() /** * Get the lore of this item stack according to its NBT. * @throws NullPointerException if `display.Lore[*]` is not a string in the NBT */ fun Const.getLore(): List? = displayNbt ?.getList("Lore") ?.intoList() ?.liftList() ?.map { it.tryCast()!!.getString() }