1414import net .minecraft .util .datafix .DataFixers ;
1515import net .minecraft .util .datafix .fixes .References ;
1616import org .bukkit .Bukkit ;
17+ import org .bukkit .UnsafeValues ;
1718import org .bukkit .World ;
1819import org .bukkit .craftbukkit .CraftWorld ;
1920import org .bukkit .craftbukkit .inventory .CraftItemStack ;
@@ -71,18 +72,8 @@ private static CraftWorld getDefaultWorld() {
7172 * @param itemStack the item to be serialized
7273 * @return binary NBT representation of the item stack
7374 */
74- public static byte [] itemToBinary (ItemStack itemStack ) throws IOException {
75- net .minecraft .world .item .ItemStack nativeItemStack = CraftItemStack .unwrap (itemStack );
76- CompoundTag tagPrefix = new CompoundTag ();
77- tagPrefix .putInt (NYAACORE_ITEMSTACK_DATAVERSION_KEY , currentDataVersion );
78- Tag tag = nativeItemStack .save (getDefaultWorld ().getHandle ().registryAccess (), tagPrefix );
79- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
80- DataOutputStream dos = new DataOutputStream (baos );
81- tag .write (dos );
82- byte [] outputByteArray = baos .toByteArray ();
83- dos .close ();
84- baos .close ();
85- return outputByteArray ;
75+ public static byte [] itemToBinary (ItemStack itemStack ) {
76+ return itemStack .serializeAsBytes ();
8677 }
8778
8879 /**
@@ -93,41 +84,12 @@ public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
9384 * @return constructed item
9485 */
9586 public static ItemStack itemFromBinary (byte [] nbt ) throws IOException {
96- return itemFromBinary (nbt , 0 , nbt . length );
87+ return ItemStack . deserializeBytes (nbt );
9788 }
9889
99- public static ItemStack itemFromBinary (byte [] nbt , int offset , int len ) throws IOException {
100- if (unlimitedNbtAccounter == null ) {
101- unlimitedNbtAccounter = NbtAccounter .unlimitedHeap ();
102- }
103-
104- //Constructor<?> constructNativeItemStackFromCompoundTag = classNativeItemStack.getConstructor(classCompoundTag);
105- ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream (nbt , offset , len );
106- DataInputStream dataInputStream = new DataInputStream (byteArrayInputStream );
107- CompoundTag reconstructedCompoundTag = CompoundTag .TYPE .load (dataInputStream , unlimitedNbtAccounter );
108- dataInputStream .close ();
109- byteArrayInputStream .close ();
110- int dataVersion = reconstructedCompoundTag .getInt (NYAACORE_ITEMSTACK_DATAVERSION_KEY ).orElse (NYAACORE_ITEMSTACK_DEFAULT_DATAVERSION );
111- if (dataVersion > 0 ) {
112- reconstructedCompoundTag .remove (NYAACORE_ITEMSTACK_DATAVERSION_KEY );
113- }
114- if (dataVersion < currentDataVersion ) {
115- // 1.12 to 1.13
116- if (dataVersion <= 0 ) {
117- dataVersion = NYAACORE_ITEMSTACK_DEFAULT_DATAVERSION ;
118- }
119- DSL .TypeReference References_ITEM_STACK = References .ITEM_STACK ;
120- NbtOps NbtOps_instance = NbtOps .INSTANCE ;
121- DataFixer dataFixer_instance = DataFixers .getDataFixer ();
122- Dynamic <Tag > dynamicInstance = new Dynamic <>(NbtOps_instance , reconstructedCompoundTag );
123- Dynamic <Tag > out = dataFixer_instance .update (References_ITEM_STACK , dynamicInstance , dataVersion , currentDataVersion );
124- reconstructedCompoundTag = (CompoundTag ) out .getValue ();
125- }
126- Optional <net .minecraft .world .item .ItemStack > reconstructedNativeItemStack = net .minecraft .world .item .ItemStack .parse (getDefaultWorld ().getHandle ().registryAccess (), reconstructedCompoundTag );
127- if (reconstructedNativeItemStack .isEmpty ()) {
128- throw new IOException ("Failed to parse item from binary" );
129- }
130- return CraftItemStack .asCraftMirror (reconstructedNativeItemStack .get ());
90+ @ Deprecated
91+ public static ItemStack itemFromBinary (byte [] nbt , int offset , int len ) {
92+ return ItemStack .deserializeBytes (nbt );
13193 }
13294
13395 private static byte [] compress (byte [] data ) {
@@ -243,19 +205,7 @@ public static ItemStack itemFromBase64(String base64) {
243205 * NOTE: this method has no corresponding deserializer.
244206 */
245207 public static String itemToJson (ItemStack itemStack ) throws RuntimeException {
246- CompoundTag nmsCompoundTagObj ; // This will just be an empty CompoundTag instance to invoke the saveNms method
247- net .minecraft .world .item .ItemStack nmsItemStackObj ; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method
248- Tag itemAsJsonObject ; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
249-
250- try {
251- nmsItemStackObj = CraftItemStack .unwrap (itemStack );
252- itemAsJsonObject = nmsItemStackObj .save (getDefaultWorld ().getHandle ().registryAccess ());
253- } catch (Throwable t ) {
254- throw new RuntimeException ("failed to serialize itemstack to nms item" , t );
255- }
256-
257- // Return a string representation of the serialized object
258- return itemAsJsonObject .toString ();
208+ return Bukkit .getUnsafe ().serializeItemAsJson (itemStack ).getAsString ();
259209 }
260210
261211 /**
0 commit comments