@@ -84,7 +84,38 @@ public static byte[] itemToBinary(ItemStack itemStack) {
84
84
* @return constructed item
85
85
*/
86
86
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
+ }
88
119
}
89
120
90
121
@ Deprecated
0 commit comments