diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java index a7300bd2d..5c50ac154 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java @@ -9,7 +9,6 @@ import java.util.Collections; import java.util.Deque; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -19,8 +18,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import com.mojang.datafixers.DataFixerUpper; - import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil; import de.tr7zw.changeme.nbtapi.utils.GsonWrapper; import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; @@ -217,7 +214,7 @@ public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { Object nmsComp = getToCompount(nbtcompound.getCompound(), nbtcompound); if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { if(nbtcompound.hasTag("tag")) { - nmsComp = DataFixerUtil.fixUpRawItemData(nmsComp, DataFixerUtil.VERSION1_20_4, DataFixerUtil.VERSION1_20_5); + nmsComp = DataFixerUtil.fixUpRawItemData(nmsComp, DataFixerUtil.VERSION1_20_4, DataFixerUtil.getCurrentVersion()); } return ReflectionMethod.NMSITEM_LOAD.run(null, registry_access, nmsComp); } else if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_11_R1.getVersionId()) { 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 d2b0f69b5..7c94e1395 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,6 +8,7 @@ import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTContainer; import de.tr7zw.changeme.nbtapi.NBTReflectionUtil; +import de.tr7zw.changeme.nbtapi.NbtApiException; import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.MojangToMapping; @@ -35,13 +36,49 @@ public static Object fixUpRawItemData(Object nbt, int fromVersion, int toVersion .get(null); DynamicOps nbtOps = (DynamicOps) ClassWrapper.NMS_NBTOPS.getClazz() .getField(MojangToMapping.getMapping().get("net.minecraft.nbt.NbtOps#INSTANCE")).get(null); - Dynamic fixed = dataFixer.update(itemStackReference, new Dynamic(nbtOps, nbt), fromVersion, toVersion); + Dynamic fixed = dataFixer.update(itemStackReference, new Dynamic(nbtOps, nbt), fromVersion, + toVersion); return fixed.getValue(); } - + public static ReadWriteNBT fixUpItemData(ReadWriteNBT nbt, int fromVersion, int toVersion) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - return new NBTContainer(fixUpRawItemData(NBTReflectionUtil.getToCompount(((NBTCompound) nbt).getCompound(), ((NBTCompound) nbt)), fromVersion, toVersion)); + return new NBTContainer(fixUpRawItemData( + NBTReflectionUtil.getToCompount(((NBTCompound) nbt).getCompound(), ((NBTCompound) nbt)), fromVersion, + toVersion)); + } + + /** + * This method will return the closest or exact version number of the current + * server. Only contains versions people will reasonably use and will fail with + * an exception, when the target version is before 1.12.2. (Assuming no one will + * update 1.8 items to 1.11, if so, provide the version numbers to the converter + * method directly) + * + * @return + */ + public static int getCurrentVersion() { + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + return VERSION1_20_5; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R3)) { + return VERSION1_20_4; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R1)) { + return VERSION1_20_1; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_19_R3)) { + return VERSION1_19_4; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_19_R1)) { + return VERSION1_19_2; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) { + return VERSION1_18_2; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) { + return VERSION1_17_1; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_16_R1)) { + return VERSION1_16_5; + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_12_R1)) { + return VERSION1_12_2; + } + throw new NbtApiException( + "Trying to update data *to* a version before 1.12.2? Something is probably going wrong, contact the plugin author."); } } 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 81a275909..d7227747c 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 @@ -22,7 +22,7 @@ public void test() throws Exception { } 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); + nbt = DataFixerUtil.fixUpItemData(nbt, DataFixerUtil.VERSION1_12_2, DataFixerUtil.getCurrentVersion()); item = NBT.itemStackFromNBT(nbt); if (item.getType() != Material.COBBLESTONE || item.getAmount() != 42 || item.getEnchantmentLevel(Enchantment.DURABILITY) != 3