From 63b4cbd4423b9e6b1d305d0a7ddf9c3aec5d1b1e Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:26:20 +0200 Subject: [PATCH] Fix nova items in item frames having empty hover name instead of none --- .../xenondevs/nova/item/logic/PacketItems.kt | 18 ++-- .../xyz/xenondevs/nova/util/data/NBTUtils.kt | 85 +++++++++++++++++++ 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/nova/src/main/kotlin/xyz/xenondevs/nova/item/logic/PacketItems.kt b/nova/src/main/kotlin/xyz/xenondevs/nova/item/logic/PacketItems.kt index 361606179a..24d5041674 100644 --- a/nova/src/main/kotlin/xyz/xenondevs/nova/item/logic/PacketItems.kt +++ b/nova/src/main/kotlin/xyz/xenondevs/nova/item/logic/PacketItems.kt @@ -63,6 +63,7 @@ import xyz.xenondevs.nova.util.component.adventure.withoutPreFormatting import xyz.xenondevs.nova.util.data.NBTUtils import xyz.xenondevs.nova.util.data.getOrNull import xyz.xenondevs.nova.util.data.getOrPut +import xyz.xenondevs.nova.util.data.getStringOrNull import xyz.xenondevs.nova.util.get import xyz.xenondevs.nova.util.item.ItemUtils import xyz.xenondevs.nova.util.item.novaCompoundOrNull @@ -235,7 +236,7 @@ internal object PacketItems : Listener, PacketListener { val novaTag = itemTag.getOrNull("nova") ?: throw IllegalArgumentException("The provided ItemStack is not a Nova item.") - val id = novaTag.getString("id") ?: return getUnknownItem(itemStack, null) + val id = novaTag.getStringOrNull("id") ?: return getUnknownItem(itemStack, null) val item = NovaRegistries.ITEM[id] ?: return getUnknownItem(itemStack, id) val subId = novaTag.getInt("subId") @@ -262,21 +263,24 @@ internal object PacketItems : Listener, PacketListener { val displayTag = newItemTag.getOrPut("display", ::CompoundTag) // name - var itemDisplayName: Component + var itemDisplayName: Component? if (!displayTag.contains("Name")) { if (useName) { itemDisplayName = packetItemData.name } else { - itemDisplayName = Component.empty() + itemDisplayName = null } } else { val customName = displayTag.getString("Name").toAdventureComponentOrEmpty() itemDisplayName = customName.style(customName.style().merge(item.style.decorate(TextDecoration.ITALIC), Style.Merge.Strategy.IF_ABSENT_ON_TARGET)) } - // enchanted items with no custom color or complex structure are colored aqua - if (Enchantable.isEnchanted(newItemStack) && itemDisplayName.color() == null && itemDisplayName.children().isEmpty()) - itemDisplayName = itemDisplayName.color(NamedTextColor.AQUA) - displayTag.putString("Name", itemDisplayName.withoutPreFormatting().toJson()) + if (itemDisplayName != null) { + // enchanted items with no custom color or complex structure are colored aqua + if (Enchantable.isEnchanted(newItemStack) && itemDisplayName.color() == null && itemDisplayName.children().isEmpty()) + itemDisplayName = itemDisplayName.color(NamedTextColor.AQUA) + + displayTag.putString("Name", itemDisplayName.withoutPreFormatting().toJson()) + } val loreTag = displayTag.getOrPut("Lore", ::ListTag) // enchantments diff --git a/nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt b/nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt index 991c87d5e5..2c9d165ed4 100644 --- a/nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt +++ b/nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt @@ -11,6 +11,7 @@ import net.minecraft.nbt.IntTag import net.minecraft.nbt.ListTag import net.minecraft.nbt.LongArrayTag import net.minecraft.nbt.LongTag +import net.minecraft.nbt.NumericTag import net.minecraft.nbt.ShortTag import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag @@ -90,4 +91,88 @@ fun CompoundTag.getOrNull(key: String): T? { return if (contains(key)) { get(key) as? T } else null +} + +fun CompoundTag.getByteOrNull(key: String): Byte? { + if (contains(key, NBTUtils.TAG_BYTE)) + return (get(key) as? NumericTag)?.asByte + + return null +} + +fun CompoundTag.getShortOrNull(key: String): Short? { + if (contains(key, NBTUtils.TAG_SHORT)) + return (get(key) as? NumericTag)?.asShort + + return null +} + +fun CompoundTag.getIntOrNull(key: String): Int? { + if (contains(key, NBTUtils.TAG_INT)) + return (get(key) as? NumericTag)?.asInt + + return null +} + +fun CompoundTag.getLongOrNull(key: String): Long? { + if (contains(key, NBTUtils.TAG_LONG)) + return (get(key) as? NumericTag)?.asLong + + return null +} + +fun CompoundTag.getFloatOrNull(key: String): Float? { + if (contains(key, NBTUtils.TAG_FLOAT)) + return (get(key) as? NumericTag)?.asFloat + + return null +} + +fun CompoundTag.getDoubleOrNull(key: String): Double? { + if (contains(key, NBTUtils.TAG_DOUBLE)) + return (get(key) as? NumericTag)?.asDouble + + return null +} + +fun CompoundTag.getStringOrNull(key: String): String? { + if (contains(key, NBTUtils.TAG_STRING)) + return (get(key) as? StringTag)?.asString + + return null +} + +fun CompoundTag.getByteArrayOrNull(key: String): ByteArray? { + if (contains(key, NBTUtils.TAG_BYTE_ARRAY)) + return (get(key) as? ByteArrayTag)?.asByteArray + + return null +} + +fun CompoundTag.getIntArrayOrNull(key: String): IntArray? { + if (contains(key, NBTUtils.TAG_INT_ARRAY)) + return (get(key) as? IntArrayTag)?.asIntArray + + return null +} + +fun CompoundTag.getLongArrayOrNull(key: String): LongArray? { + if (contains(key, NBTUtils.TAG_LONG_ARRAY)) + return (get(key) as? LongArrayTag)?.asLongArray + + return null +} + +fun CompoundTag.getCompoundOrNull(key: String): CompoundTag? { + if (contains(key, NBTUtils.TAG_COMPOUND)) + return get(key) as? CompoundTag + + return null +} + +fun CompoundTag.getListOrNull(key: String): ListTag? { + if (contains(key, NBTUtils.TAG_LIST)) + return get(key) as? ListTag + + return null } \ No newline at end of file