package at.hannibal2.skyhanni.utils.const import net.minecraft.nbt.NBTBase import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagInt import net.minecraft.nbt.NBTTagList import net.minecraft.nbt.NBTTagLong import net.minecraft.nbt.NBTTagString /** * This method indexes into an NBTTagCompound searching for a tag. * The caller must guarantee that the [T]s type identifier is [tag]. * This means for any `instance` of [NBTBase], `instance.getId() == tag` iff `instance is T`. */ @PublishedApi internal fun Const.getAndCast(name: String, tag: Int): Const? = unsafeMap { if (it.hasKey(name, tag)) @Suppress("UNCHECKED_CAST") it.getTag(name) as T else null }.liftNull() private val TAG_COMPOUND_KEY = NBTTagCompound().id.toInt() private val TAG_COMPOUND_STRING = NBTTagString().id.toInt() private val TAG_COMPOUND_INT = NBTTagInt(0).id.toInt() private val TAG_COMPOUND_LONG = NBTTagLong(0L).id.toInt() private val TAG_COMPOUND_LIST = NBTTagList().id.toInt() fun Const.getTagCompound(name: String): Const? = getAndCast(name, TAG_COMPOUND_KEY) fun Const.getString(name: String): Const? = getAndCast(name, TAG_COMPOUND_STRING) fun Const.getInt(name: String): Const? = getAndCast(name, TAG_COMPOUND_INT) fun Const.getLong(name: String): Const? = getAndCast(name, TAG_COMPOUND_LONG) fun Const.getList(name: String): Const? = getAndCast(name, TAG_COMPOUND_LIST) val Const.size: Int get() = unsafeMap(NBTTagList::tagCount).unconst fun Const.getTag(i: Int): Const { return unsafeMap { it.get(i) } } inline fun Const.intoList(): Const> { return unsafeMap { val build = mutableListOf() for (i in 0...getString(): String = unsafeMap(NBTTagString::getString).unconst fun Const.getInt(): Int = unsafeMap(NBTTagInt::getInt).unconst fun Const.getLong(): Long = unsafeMap(NBTTagLong::getLong).unconst