Skip to content

Commit

Permalink
Fix BlockEntities for 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Apr 23, 2024
1 parent 9367942 commit 0329901
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -180,19 +181,20 @@ 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
*
* @param 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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0329901

Please sign in to comment.