From 03299012acc3d3a980a662ed914b4f69f102db6e Mon Sep 17 00:00:00 2001 From: tr7zw Date: Wed, 24 Apr 2024 01:40:33 +0200 Subject: [PATCH] Fix BlockEntities for 1.20.5 --- .../changeme/nbtapi/NBTReflectionUtil.java | 33 +++++++++++-------- .../utils/nmsmappings/MojangToMapping.java | 2 ++ .../utils/nmsmappings/ReflectionMethod.java | 8 +++-- 3 files changed, 28 insertions(+), 15 deletions(-) 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 14564f2ee..a56d96de1 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 @@ -98,8 +98,9 @@ public static Object getNMSEntity(Entity entity) { */ public static Object readNBT(InputStream stream) { try { - if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R3)) { - return ReflectionMethod.NBTFILE_READV2.run(null, stream, ReflectionMethod.NBTACCOUNTER_CREATE_UNLIMITED.run(null)); + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R3)) { + return ReflectionMethod.NBTFILE_READV2.run(null, stream, + ReflectionMethod.NBTACCOUNTER_CREATE_UNLIMITED.run(null)); } else { return ReflectionMethod.NBTFILE_READ.run(null, stream); } @@ -168,7 +169,7 @@ public static Object getItemRootNBTTagCompound(Object nmsitem) { try { if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { Object customData = ReflectionMethod.NMSDATACOMPONENTHOLDER_GET.run(nmsitem, type_custom_data); - if(customData == null) { + if (customData == null) { return null; } return ReflectionMethod.NMSCUSTOMDATA_GETCOPY.run(customData); @@ -180,7 +181,7 @@ public static Object getItemRootNBTTagCompound(Object nmsitem) { throw new NbtApiException("Exception while getting an Itemstack's NBTCompound!", e); } } - + /** * Set the Compound as the Items NBT or CustomData * @@ -188,11 +189,12 @@ public static Object getItemRootNBTTagCompound(Object nmsitem) { * @param compound */ public static void setItemStackCompound(Object nmsItem, Object compound) { - if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { - if(compound == null) { - ReflectionMethod.NMSITEM_SET.run(nmsItem, new Object[] { type_custom_data, null}); + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + if (compound == null) { + ReflectionMethod.NMSITEM_SET.run(nmsItem, new Object[] { type_custom_data, null }); } else { - ReflectionMethod.NMSITEM_SET.run(nmsItem, type_custom_data, ObjectCreator.NMS_CUSTOMDATA.getInstance(compound)); + ReflectionMethod.NMSITEM_SET.run(nmsItem, type_custom_data, + ObjectCreator.NMS_CUSTOMDATA.getInstance(compound)); } } else { ReflectionMethod.ITEMSTACK_SET_TAG.run(nmsItem, compound); @@ -208,7 +210,7 @@ public static void setItemStackCompound(Object nmsItem, Object compound) { public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { try { Object nmsComp = gettoCompount(nbtcompound.getCompound(), nbtcompound); - if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { return ReflectionMethod.NMSITEM_LOAD.run(null, registry_access, nmsComp); } else if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_11_R1.getVersionId()) { return ObjectCreator.NMS_COMPOUNDFROMITEM.getInstance(nmsComp); @@ -228,10 +230,11 @@ public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { */ public static NBTContainer convertNMSItemtoNBTCompound(Object nmsitem) { try { - if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { return new NBTContainer(ReflectionMethod.NMSITEM_SAVE_MODERN.run(nmsitem, registry_access)); } else { - Object answer = ReflectionMethod.NMSITEM_SAVE.run(nmsitem, ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance()); + Object answer = ReflectionMethod.NMSITEM_SAVE.run(nmsitem, + ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance()); return new NBTContainer(answer); } } catch (Exception e) { @@ -309,7 +312,9 @@ public static Object getTileEntityNBTTagCompound(BlockState tile) { } Object answer = null; - if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) { + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + answer = ReflectionMethod.TILEENTITY_GET_NBT_1205.run(o, registry_access); + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) { answer = ReflectionMethod.TILEENTITY_GET_NBT_1181.run(o); } else { answer = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); @@ -342,7 +347,9 @@ public static void setTileEntityNBTTagCompound(BlockState tile, Object comp) { Object pos = ObjectCreator.NMS_BLOCKPOSITION.getInstance(tile.getX(), tile.getY(), tile.getZ()); o = ReflectionMethod.NMS_WORLD_GET_TILEENTITY.run(nmsworld, pos); } - if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) { + if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + ReflectionMethod.TILEENTITY_SET_NBT_1205.run(o, comp, registry_access); + } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) { ReflectionMethod.TILEENTITY_SET_NBT.run(o, comp); } else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_16_R1)) { Object blockData = ReflectionMethod.TILEENTITY_GET_BLOCKDATA.run(o); diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/MojangToMapping.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/MojangToMapping.java index 71bc31cf0..5983aa54e 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/MojangToMapping.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/MojangToMapping.java @@ -163,6 +163,8 @@ public class MojangToMapping { put("net.minecraft.world.item.ItemStack#save(net.minecraft.core.HolderLookup$Provider)", "a"); put("net.minecraft.server.MinecraftServer#registryAccess()", "bc"); put("net.minecraft.world.item.ItemStack#parseOptional(net.minecraft.core.HolderLookup$Provider,net.minecraft.nbt.CompoundTag)", "a"); + put("net.minecraft.world.level.block.entity.BlockEntity#saveWithId(net.minecraft.core.HolderLookup$Provider)", "c"); + put("net.minecraft.world.level.block.entity.BlockEntity#loadWithComponents(net.minecraft.nbt.CompoundTag,net.minecraft.core.HolderLookup$Provider)", "c"); } }; diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ReflectionMethod.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ReflectionMethod.java index 19b85ccab..639c2ab68 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ReflectionMethod.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ReflectionMethod.java @@ -208,7 +208,7 @@ MinecraftVersion.MC1_16_R1, MinecraftVersion.MC1_16_R3, new Since(MinecraftVersi TILEENTITY_GET_NBT(ClassWrapper.NMS_TILEENTITY, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_17_R1, new Since(MinecraftVersion.MC1_7_R4, "b"), new Since(MinecraftVersion.MC1_9_R1, "save")), - TILEENTITY_GET_NBT_1181(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_18_R1, + TILEENTITY_GET_NBT_1181(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_18_R1, MinecraftVersion.MC1_20_R3, new Since(MinecraftVersion.MC1_18_R1, "saveWithId()")), TILEENTITY_SET_NBT_LEGACY1151(ClassWrapper.NMS_TILEENTITY, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, MinecraftVersion.MC1_7_R4, @@ -218,7 +218,7 @@ MinecraftVersion.MC1_15_R1, new Since(MinecraftVersion.MC1_7_R4, "a"), new Class[] { ClassWrapper.NMS_IBLOCKDATA.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, MinecraftVersion.MC1_16_R1, MinecraftVersion.MC1_16_R3, new Since(MinecraftVersion.MC1_16_R1, "load")), TILEENTITY_SET_NBT(ClassWrapper.NMS_TILEENTITY, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, - MinecraftVersion.MC1_17_R1, new Since(MinecraftVersion.MC1_16_R1, "load"), + MinecraftVersion.MC1_17_R1, MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_16_R1, "load"), new Since(MinecraftVersion.MC1_18_R1, "load(net.minecraft.nbt.CompoundTag)")), TILEENTITY_GET_BLOCKDATA(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_16_R1, new Since(MinecraftVersion.MC1_16_R1, "getBlock"), @@ -309,6 +309,10 @@ MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "parseOptional MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "registryAccess()")), NMSSERVER_GETSERVER(ClassWrapper.CRAFT_SERVER, new Class[] {}, MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "getServer()")), + TILEENTITY_GET_NBT_1205(ClassWrapper.NMS_TILEENTITY, new Class[] {ClassWrapper.NMS_PROVIDER.getClazz()}, MinecraftVersion.MC1_20_R4, + new Since(MinecraftVersion.MC1_20_R4, "saveWithId(net.minecraft.core.HolderLookup$Provider)")), + TILEENTITY_SET_NBT_1205(ClassWrapper.NMS_TILEENTITY, new Class[] {ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), ClassWrapper.NMS_PROVIDER.getClazz() }, + MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "loadWithComponents(net.minecraft.nbt.CompoundTag,net.minecraft.core.HolderLookup$Provider)")), ; private MinecraftVersion removedAfter;