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 .craftbukkit .v1_20_R3 .inventory .CraftItemStack ;
17
+ import org .bukkit .World ;
18
+ import org .bukkit .craftbukkit .CraftWorld ;
19
+ import org .bukkit .craftbukkit .inventory .CraftItemStack ;
18
20
import org .bukkit .inventory .ItemStack ;
19
21
20
22
import java .io .*;
21
23
import java .util .ArrayList ;
22
24
import java .util .Collections ;
23
25
import java .util .List ;
26
+ import java .util .Optional ;
24
27
import java .util .stream .Collectors ;
25
28
import java .util .zip .Deflater ;
26
29
import java .util .zip .DeflaterInputStream ;
@@ -38,6 +41,24 @@ public final class ItemStackUtils {
38
41
.maximumWeight (256L * 1024 * 1024 ).build (); // Hard Coded 256M
39
42
private static NbtAccounter unlimitedNbtAccounter = null ;
40
43
44
+ private static CraftWorld defaultWorld ;
45
+
46
+ private static CraftWorld getDefaultWorld () {
47
+ if (defaultWorld == null ) {
48
+ var worlds = Bukkit .getWorlds ();
49
+ if (!worlds .isEmpty ()) {
50
+ var first = worlds .getFirst ();
51
+ if (first instanceof CraftWorld ) {
52
+ defaultWorld = (CraftWorld ) first ;
53
+ }
54
+ }
55
+ if (defaultWorld == null ) {
56
+ throw new IllegalStateException ("No world available" );
57
+ }
58
+ }
59
+ return defaultWorld ;
60
+ }
61
+
41
62
static {
42
63
//noinspection deprecation
43
64
currentDataVersion = Bukkit .getUnsafe ().getDataVersion ();
@@ -51,14 +72,13 @@ public final class ItemStackUtils {
51
72
* @return binary NBT representation of the item stack
52
73
*/
53
74
public static byte [] itemToBinary (ItemStack itemStack ) throws IOException {
54
- net .minecraft .world .item .ItemStack nativeItemStack = CraftItemStack .asNMSCopy (itemStack );
55
- CompoundTag CompoundTag = new CompoundTag ();
56
- nativeItemStack .save (CompoundTag );
57
- CompoundTag .putInt (NYAACORE_ITEMSTACK_DATAVERSION_KEY , currentDataVersion );
58
-
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 );
59
79
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
60
80
DataOutputStream dos = new DataOutputStream (baos );
61
- CompoundTag .write (dos );
81
+ tag .write (dos );
62
82
byte [] outputByteArray = baos .toByteArray ();
63
83
dos .close ();
64
84
baos .close ();
@@ -72,7 +92,7 @@ public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
72
92
* @param nbt binary item nbt data
73
93
* @return constructed item
74
94
*/
75
- public static ItemStack itemFromBinary (byte [] nbt ) throws ReflectiveOperationException , IOException {
95
+ public static ItemStack itemFromBinary (byte [] nbt ) throws IOException {
76
96
return itemFromBinary (nbt , 0 , nbt .length );
77
97
}
78
98
@@ -103,8 +123,11 @@ public static ItemStack itemFromBinary(byte[] nbt, int offset, int len) throws I
103
123
Dynamic <Tag > out = dataFixer_instance .update (References_ITEM_STACK , dynamicInstance , dataVersion , currentDataVersion );
104
124
reconstructedCompoundTag = (CompoundTag ) out .getValue ();
105
125
}
106
- net .minecraft .world .item .ItemStack reconstructedNativeItemStack = net .minecraft .world .item .ItemStack .of (reconstructedCompoundTag );
107
- return CraftItemStack .asBukkitCopy (reconstructedNativeItemStack );
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 ());
108
131
}
109
132
110
133
private static byte [] compress (byte [] data ) {
@@ -195,7 +218,7 @@ public static List<ItemStack> itemsFromBase64(String base64) {
195
218
dis .readFully (tmp );
196
219
ret .add (itemFromBinary (tmp ));
197
220
}
198
- } catch (IOException | ReflectiveOperationException ex ) {
221
+ } catch (IOException ex ) {
199
222
throw new RuntimeException (ex );
200
223
}
201
224
itemDeserializerCache .put (base64 , ret .stream ().map (ItemStack ::clone ).collect (Collectors .toList ()));
@@ -222,12 +245,11 @@ public static ItemStack itemFromBase64(String base64) {
222
245
public static String itemToJson (ItemStack itemStack ) throws RuntimeException {
223
246
CompoundTag nmsCompoundTagObj ; // This will just be an empty CompoundTag instance to invoke the saveNms method
224
247
net .minecraft .world .item .ItemStack nmsItemStackObj ; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method
225
- CompoundTag itemAsJsonObject ; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
248
+ Tag itemAsJsonObject ; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
226
249
227
250
try {
228
- nmsCompoundTagObj = new CompoundTag ();
229
- nmsItemStackObj = CraftItemStack .asNMSCopy (itemStack );
230
- itemAsJsonObject = nmsItemStackObj .save (nmsCompoundTagObj );
251
+ nmsItemStackObj = CraftItemStack .unwrap (itemStack );
252
+ itemAsJsonObject = nmsItemStackObj .save (getDefaultWorld ().getHandle ().registryAccess ());
231
253
} catch (Throwable t ) {
232
254
throw new RuntimeException ("failed to serialize itemstack to nms item" , t );
233
255
}
0 commit comments