Skip to content

Commit 0132eea

Browse files
authored
Merge pull request #48 from ETWXR9/1.12.8-update
Enhance itemFromBinary method to handle legacy NBT data and GZIP compression
2 parents 3d5793f + 0de5e89 commit 0132eea

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/main/java/cat/nyaa/nyaacore/utils/ItemStackUtils.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,38 @@ public static byte[] itemToBinary(ItemStack itemStack) {
8484
* @return constructed item
8585
*/
8686
public static ItemStack itemFromBinary(byte[] nbt) throws IOException {
87-
return ItemStack.deserializeBytes(nbt);
87+
if (nbt == null || nbt.length == 0) {
88+
return null;
89+
}
90+
// GZIP magic number check
91+
if (nbt.length < 2 || !(nbt[0] == (byte) 0x1f && nbt[1] == (byte) 0x8b)) {
92+
// Data is not in GZIP format, assume it's legacy data.
93+
// Legacy data was a "headless" NBT payload. We need to wrap it in a full
94+
// NBT structure, then GZIP compress it before deserialization.
95+
try (ByteArrayOutputStream fullNbtStream = new ByteArrayOutputStream();
96+
DataOutputStream nbtDos = new DataOutputStream(fullNbtStream)) {
97+
98+
// 1. Create a full NBT structure by adding a header
99+
nbtDos.writeByte(10); // TAG_Compound ID
100+
nbtDos.writeUTF(""); // Root tag name (empty)
101+
nbtDos.write(nbt); // The original "headless" NBT payload
102+
nbtDos.flush();
103+
104+
// 2. GZIP compress the full NBT structure
105+
try (ByteArrayOutputStream gzipStream = new ByteArrayOutputStream();
106+
java.util.zip.GZIPOutputStream gzipOut = new java.util.zip.GZIPOutputStream(gzipStream)) {
107+
gzipOut.write(fullNbtStream.toByteArray());
108+
gzipOut.finish();
109+
byte[] compressedNbt = gzipStream.toByteArray();
110+
111+
// 3. Deserialize the GZIP-compressed data
112+
return ItemStack.deserializeBytes(compressedNbt);
113+
}
114+
}
115+
} else {
116+
// Data is already in GZIP format (new format).
117+
return ItemStack.deserializeBytes(nbt);
118+
}
88119
}
89120

90121
@Deprecated

0 commit comments

Comments
 (0)