Skip to content

Commit

Permalink
Remove CBFCompoundTag reserialization
Browse files Browse the repository at this point in the history
This is no longer required because Nova cannot be reloaded anymore
  • Loading branch information
NichtStudioCode committed Jul 17, 2023
1 parent 82aa7f8 commit 926572b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 60 deletions.
53 changes: 0 additions & 53 deletions nova/src/main/kotlin/xyz/xenondevs/nova/util/data/NBTUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import net.minecraft.nbt.StringTag
import net.minecraft.nbt.Tag
import net.minecraft.nbt.TagType
import net.minecraft.world.item.ItemStack
import xyz.xenondevs.cbf.CBF
import xyz.xenondevs.nova.data.serialization.cbf.CBFCompoundTag
import xyz.xenondevs.nova.data.serialization.cbf.NamespacedCompound
import java.util.stream.Stream

object NBTUtils {
Expand Down Expand Up @@ -75,12 +72,6 @@ object NBTUtils {
return tag.stream().map { ItemStack.of(it as CompoundTag) }
}

internal fun reserializeCBFCompoundTag(cbfTag: Any): CBFCompoundTag {
val toByteArrayMethod = cbfTag::class.java.getMethod("getAsByteArray")
val serializedCBFCompound = toByteArrayMethod.invoke(cbfTag) as ByteArray
return CBFCompoundTag(CBF.read<NamespacedCompound>(serializedCBFCompound)!!)
}

}

@Suppress("UNCHECKED_CAST")
Expand All @@ -99,48 +90,4 @@ fun <T : Tag> CompoundTag.getOrNull(key: String): T? {
return if (contains(key)) {
get(key) as? T
} else null
}

internal fun CompoundTag.getOrPutCBFCompoundTag(key: String, defaultValue: () -> CBFCompoundTag): CBFCompoundTag {
var value = getCBFCompoundTag(key)
if (value != null)
return value

value = defaultValue()
put(key, value)

return value
}

internal fun CompoundTag.getCBFCompoundTag(key: String): CBFCompoundTag? {
val tag = get(key) ?: return null
if (tag !is CBFCompoundTag) {
val newTag = NBTUtils.reserializeCBFCompoundTag(tag)
put(key, newTag)
return newTag
}

return tag
}

internal fun MutableMap<String, Tag>.getCBFCompoundTag(key: String): CBFCompoundTag? {
val tag = get(key) ?: return null
if (tag !is CBFCompoundTag) {
val newTag = NBTUtils.reserializeCBFCompoundTag(tag)
put(key, newTag)
return newTag
}

return tag
}

internal fun MutableMap<String, Tag>.getOrPutCBFCompoundTag(key: String, defaultValue: () -> CBFCompoundTag): CBFCompoundTag {
var value = getCBFCompoundTag(key)
if (value != null)
return value

value = defaultValue()
put(key, value)

return value
}
12 changes: 5 additions & 7 deletions nova/src/main/kotlin/xyz/xenondevs/nova/util/item/ItemUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ import xyz.xenondevs.nova.util.bukkitMirror
import xyz.xenondevs.nova.util.component.adventure.toAdventureComponent
import xyz.xenondevs.nova.util.component.adventure.toJson
import xyz.xenondevs.nova.util.data.NBTUtils
import xyz.xenondevs.nova.util.data.getCBFCompoundTag
import xyz.xenondevs.nova.util.data.getOrNull
import xyz.xenondevs.nova.util.data.getOrPut
import xyz.xenondevs.nova.util.data.getOrPutCBFCompoundTag
import xyz.xenondevs.nova.util.get
import xyz.xenondevs.nova.util.name
import xyz.xenondevs.nova.util.nmsCopy
Expand Down Expand Up @@ -208,8 +206,8 @@ var ItemStack.novaCompound: NamespacedCompound
return handle.novaCompound

val unhandledTags = backingItemMeta!!.unhandledTags
val tag = unhandledTags.getOrPutCBFCompoundTag("nova_cbf", ::CBFCompoundTag) as? CBFCompoundTag
return tag!!.compound
val tag = unhandledTags.getOrPut("nova_cbf", ::CBFCompoundTag) as CBFCompoundTag
return tag.compound
}
set(value) {
val handle = handle
Expand All @@ -226,19 +224,19 @@ val ItemStack.novaCompoundOrNull: NamespacedCompound?
if (handle != null)
return handle.novaCompoundOrNull

return backingItemMeta?.unhandledTags?.getCBFCompoundTag("nova_cbf")?.compound
return (backingItemMeta?.unhandledTags?.get("nova_cbf") as? CBFCompoundTag)?.compound
}
//</editor-fold>

//<editor-fold desc="MojangStack - Nova Compound", defaultstate="collapsed">
var MojangStack.novaCompound: NamespacedCompound
get() = orCreateTag.getOrPutCBFCompoundTag("nova_cbf", ::CBFCompoundTag).compound
get() = orCreateTag.getOrPut("nova_cbf", ::CBFCompoundTag).compound
set(value) {
orCreateTag.put("nova_cbf", CBFCompoundTag(value))
}

val MojangStack.novaCompoundOrNull: NamespacedCompound?
get() = tag?.getCBFCompoundTag("nova_cbf")?.compound
get() = (tag?.get("nova_cbf") as? CBFCompoundTag)?.compound
//</editor-fold>

//<editor-fold desc="BukkitStack - retrieve", defaultstate="collapsed">
Expand Down

0 comments on commit 926572b

Please sign in to comment.