14
14
import net .minecraft .util .datafix .DataFixers ;
15
15
import net .minecraft .util .datafix .fixes .References ;
16
16
import org .bukkit .Bukkit ;
17
+ import org .bukkit .UnsafeValues ;
17
18
import org .bukkit .World ;
18
19
import org .bukkit .craftbukkit .CraftWorld ;
19
20
import org .bukkit .craftbukkit .inventory .CraftItemStack ;
@@ -71,18 +72,8 @@ private static CraftWorld getDefaultWorld() {
71
72
* @param itemStack the item to be serialized
72
73
* @return binary NBT representation of the item stack
73
74
*/
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 ();
86
77
}
87
78
88
79
/**
@@ -93,41 +84,12 @@ public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
93
84
* @return constructed item
94
85
*/
95
86
public static ItemStack itemFromBinary (byte [] nbt ) throws IOException {
96
- return itemFromBinary (nbt , 0 , nbt . length );
87
+ return ItemStack . deserializeBytes (nbt );
97
88
}
98
89
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 );
131
93
}
132
94
133
95
private static byte [] compress (byte [] data ) {
@@ -243,19 +205,7 @@ public static ItemStack itemFromBase64(String base64) {
243
205
* NOTE: this method has no corresponding deserializer.
244
206
*/
245
207
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 ();
259
209
}
260
210
261
211
/**
0 commit comments