Skip to content

Commit

Permalink
Improve data fixing of itemstacks
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed Jun 16, 2024
1 parent d2f021d commit 3d5eccc
Showing 1 changed file with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import net.minecraft.util.Identifier
const val ITEM_NBT_DATA_VERSION = 3817
const val ITEM_COMPONENTS_DATA_VERSION = 3825

const val COMPONENTS = "components" // ItemStack
const val PROPERTIES = "Properties" // BlockState
const val COUNT_PRE_1_20_5 = "Count" // ItemStack
const val COUNT = "count" // ItemStack
const val TAG = "tag" // ItemStack
const val UUID = "UUID" // Entity

object NbtUtils {
Expand All @@ -47,26 +46,18 @@ object NbtUtils {

fun itemFromProperties(tag: String?, name: Identifier, registries: RegistryWrapper.WrapperLookup): ItemStack {
val extraDataTag = StringNbtReader.parse(tag ?: "{}")
var itemTag: NbtElement
if (extraDataTag.contains(COMPONENTS)) {
itemTag = extraDataTag
} else {
itemTag = NbtCompound()
var itemTag = extraDataTag
if (!extraDataTag.contains(COUNT)) {
// 1.20.4 and lower (need data fixing)
itemTag.putString("id", name.toString())
if (extraDataTag.contains(COUNT)) {
itemTag.putByte(COUNT, extraDataTag.getByte(COUNT))
} else {
itemTag.putByte(COUNT, 1)
}

if (extraDataTag.contains(TAG)) {
itemTag.put(TAG, extraDataTag.getCompound(TAG))

itemTag = Schemas.getFixer().update(
TypeReferences.ITEM_STACK,
Dynamic(NbtOps.INSTANCE, itemTag), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION
).cast(NbtOps.INSTANCE)
if (!itemTag.contains(COUNT_PRE_1_20_5)) {
// Ledger ItemStack in 1.20.4 and earlier had "Count" omitted if it was 1
itemTag.putByte(COUNT_PRE_1_20_5, 1)
}
itemTag = Schemas.getFixer().update(
TypeReferences.ITEM_STACK,
Dynamic(NbtOps.INSTANCE, itemTag), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION
).cast(NbtOps.INSTANCE) as NbtCompound?
}

return ItemStack.fromNbt(registries, itemTag).get()
Expand Down

0 comments on commit 3d5eccc

Please sign in to comment.