From 8570c6f77e29793a34d2e2cad0ddb780bce9e3dc Mon Sep 17 00:00:00 2001 From: tr7zw Date: Sun, 19 May 2024 01:07:24 +0200 Subject: [PATCH] Improve error when trying to get NBT from non BlockEntity BlockStates. Fix #276 --- .../de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 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 5c50ac154..733f2d303 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 @@ -213,8 +213,9 @@ public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { try { 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.getCurrentVersion()); + if (nbtcompound.hasTag("tag")) { + 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()) { @@ -316,6 +317,11 @@ public static Object getTileEntityNBTTagCompound(BlockState tile) { o = ReflectionMethod.NMS_WORLD_GET_TILEENTITY.run(nmsworld, pos); } + if (o == null) { + throw new NbtApiException("The passed BlockState(" + tile.getType() + + ") doesn't point to a BlockEntity. Only BlockEntities like Chest/Signs/Furnance/etc have NBT."); + } + Object answer = null; if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { answer = ReflectionMethod.TILEENTITY_GET_NBT_1205.run(o, registry_access);