From 027f2a2edd31eff40be0267b4286975702eaf55d Mon Sep 17 00:00:00 2001 From: tr7zw Date: Thu, 25 Apr 2024 18:39:40 +0200 Subject: [PATCH] Add more version ids, test loading 1.12.2 items in 1.20.5 --- .../changeme/nbtapi/utils/DataFixerUtil.java | 17 +++++++++++++---- .../plugin/tests/items/LegacyItemTest.java | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/DataFixerUtil.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/DataFixerUtil.java index 2c62892a0..d2b0f69b5 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/DataFixerUtil.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/DataFixerUtil.java @@ -8,14 +8,23 @@ import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTContainer; import de.tr7zw.changeme.nbtapi.NBTReflectionUtil; +import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.MojangToMapping; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; public class DataFixerUtil { - public static final int VERSION1_20_4 = 3700; // 1.20.4 - public static final int VERSION1_20_5 = 3837; // 1.20.5 + public static final int VERSION1_12_2 = 1343; + public static final int VERSION1_16_5 = 2586; + public static final int VERSION1_17_1 = 2730; + public static final int VERSION1_18_2 = 2975; + public static final int VERSION1_19_2 = 3120; + public static final int VERSION1_19_4 = 3337; + public static final int VERSION1_20_1 = 3465; + public static final int VERSION1_20_2 = 3578; + public static final int VERSION1_20_4 = 3700; + public static final int VERSION1_20_5 = 3837; @SuppressWarnings("unchecked") public static Object fixUpRawItemData(Object nbt, int fromVersion, int toVersion) @@ -30,9 +39,9 @@ public static Object fixUpRawItemData(Object nbt, int fromVersion, int toVersion return fixed.getValue(); } - public static NBTCompound fixUpItemData(NBTCompound nbt, int fromVersion, int toVersion) + public static ReadWriteNBT fixUpItemData(ReadWriteNBT nbt, int fromVersion, int toVersion) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - return new NBTContainer(fixUpRawItemData(NBTReflectionUtil.getToCompount(nbt.getCompound(), nbt), fromVersion, toVersion)); + return new NBTContainer(fixUpRawItemData(NBTReflectionUtil.getToCompount(((NBTCompound) nbt).getCompound(), ((NBTCompound) nbt)), fromVersion, toVersion)); } } diff --git a/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/items/LegacyItemTest.java b/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/items/LegacyItemTest.java index 8d226a7e7..81a275909 100644 --- a/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/items/LegacyItemTest.java +++ b/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/items/LegacyItemTest.java @@ -6,16 +6,29 @@ import de.tr7zw.changeme.nbtapi.NBT; import de.tr7zw.changeme.nbtapi.NbtApiException; +import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; +import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil; import de.tr7zw.nbtapi.plugin.tests.Test; public class LegacyItemTest implements Test { @Override public void test() throws Exception { - ItemStack item = NBT.itemStackFromNBT(NBT.parseNBT("{id:cobblestone,Count:42,tag:{Enchantments:[{lvl:3,id:unbreaking}]}}")); - if(item.getType() != Material.COBBLESTONE || item.getAmount() != 42 || item.getEnchantmentLevel(Enchantment.DURABILITY) != 3) { + ItemStack item = NBT + .itemStackFromNBT(NBT.parseNBT("{id:cobblestone,Count:42,tag:{Enchantments:[{lvl:3,id:unbreaking}]}}")); + if (item.getType() != Material.COBBLESTONE || item.getAmount() != 42 + || item.getEnchantmentLevel(Enchantment.DURABILITY) != 3) { throw new NbtApiException("1.20 item didn't load correctly! " + item); } + ReadWriteNBT nbt = NBT + .parseNBT("{id:cobblestone,Count:42,tag:{display:{Name:\"test\"},ench:[{lvl:3,id:34}]}}"); + nbt = DataFixerUtil.fixUpItemData(nbt, DataFixerUtil.VERSION1_12_2, DataFixerUtil.VERSION1_20_5); + item = NBT.itemStackFromNBT(nbt); + if (item.getType() != Material.COBBLESTONE || item.getAmount() != 42 + || item.getEnchantmentLevel(Enchantment.DURABILITY) != 3 + || !"test".equals(item.getItemMeta().getDisplayName())) { + throw new NbtApiException("1.12.2 item didn't load correctly! " + item); + } } }