aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt
blob: ac30771e09d461b601b056c0b10e5243a1e52a9c (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.util.item

import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtString
import net.minecraft.text.Text

val ItemStack.loreAccordingToNbt
    get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).getList(ItemStack.LORE_KEY, NbtElement.STRING_TYPE.toInt())
        .map { Text.Serializer.fromJson((it as NbtString).asString()) }

val ItemStack.displayNameAccordingToNbt
    get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).let {
        if (it.contains(ItemStack.NAME_KEY, NbtElement.STRING_TYPE.toInt()))
            Text.Serializer.fromJson(it.getString(ItemStack.NAME_KEY))
        else
            null
    }