From 243a5c2bcdf8d9358aa7b74fb73afe928d400116 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Tue, 22 Oct 2024 22:08:04 +0200 Subject: [PATCH] Fix really stupid edge case of writeCompound --- .../java/de/tr7zw/changeme/nbtapi/NBTReflectionUtil.java | 3 +++ .../tr7zw/nbtapi/plugin/tests/compounds/StreamTest.java | 9 +++++++++ 2 files changed, 12 insertions(+) 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 45707fe70..fdfcf460e 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 @@ -154,6 +154,9 @@ public static Object getCraftItemHandle(ItemStack item) { public static void writeApiNBT(NBTCompound comp, OutputStream stream) { try { Object workingtag = comp.getResolvedObject(); + if(workingtag == null) { + workingtag = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance(); + } ReflectionMethod.NBTFILE_WRITE.run(null, workingtag, stream); } catch (Exception e) { throw new NbtApiException("Exception while writing NBT!", e); diff --git a/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/compounds/StreamTest.java b/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/compounds/StreamTest.java index 4211ad235..625682691 100644 --- a/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/compounds/StreamTest.java +++ b/item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/compounds/StreamTest.java @@ -3,6 +3,10 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +import de.tr7zw.changeme.nbtapi.NBT; import de.tr7zw.changeme.nbtapi.NBTContainer; import de.tr7zw.changeme.nbtapi.NbtApiException; import de.tr7zw.nbtapi.plugin.tests.Test; @@ -21,6 +25,11 @@ public void test() throws Exception { if (!container.toString().equals(base.getOrCreateCompound("sub").toString())) { throw new NbtApiException("Component content did not match! " + base.getCompound("sub") + " " + container); } + ItemStack item = new ItemStack(Material.STICK); + + NBT.modify(item, nbt -> { + nbt.writeCompound(outStream); + }); } }