Skip to content

Commit

Permalink
Fix nova items in item frames having empty hover name instead of none
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Oct 23, 2023
1 parent cbdf4bf commit 63b4cbd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
18 changes: 11 additions & 7 deletions nova/src/main/kotlin/xyz/xenondevs/nova/item/logic/PacketItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -235,7 +236,7 @@ internal object PacketItems : Listener, PacketListener {
val novaTag = itemTag.getOrNull<CompoundTag>("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")

Expand All @@ -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
Expand Down
85 changes: 85 additions & 0 deletions nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,4 +91,88 @@ fun <T : Tag> 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
}

0 comments on commit 63b4cbd

Please sign in to comment.