30
30
31
31
package fr .zcraft .quartzlib .components .nbt ;
32
32
33
+ import fr .zcraft .quartzlib .tools .PluginLogger ;
33
34
import fr .zcraft .quartzlib .tools .items .ItemUtils ;
34
35
import fr .zcraft .quartzlib .tools .reflection .NMSException ;
35
36
import fr .zcraft .quartzlib .tools .reflection .Reflection ;
@@ -176,10 +177,10 @@ public static byte fromItemFlags(Set<ItemFlag> itemFlags) {
176
177
* @param item The ItemStack to change.
177
178
* @param tags The tags to place inside the stack.
178
179
* @return An item stack with the modification applied. It may (if you given
179
- * a CraftItemStack) or may not (else) be the same instance as the given one.
180
+ * a CraftItemStack) or may not (else) be the same instance as the given one.
180
181
* @throws NMSException if the operation cannot be executed.
181
182
* @see #addToItemStack(ItemStack, Map, boolean) This method is equivalent
182
- * to this one with replace = true.
183
+ * to this one with replace = true.
183
184
*/
184
185
public static ItemStack addToItemStack (ItemStack item , Map <String , Object > tags ) throws NMSException {
185
186
return addToItemStack (item , tags , true );
@@ -197,7 +198,7 @@ public static ItemStack addToItemStack(ItemStack item, Map<String, Object> tags)
197
198
* @param replace {@code true} to replace the whole set of tags. If {@code
198
199
* false}, tags will be added.
199
200
* @return An item stack with the modification applied. It may (if you given
200
- * a CraftItemStack) or may not (else) be the same instance as the given one.
201
+ * a CraftItemStack) or may not (else) be the same instance as the given one.
201
202
* @throws NMSException if the operation cannot be executed.
202
203
*/
203
204
public static ItemStack addToItemStack (final ItemStack item , final Map <String , Object > tags , final boolean replace )
@@ -241,11 +242,23 @@ public static ItemStack addToItemStack(final ItemStack item, final Map<String, O
241
242
}
242
243
}
243
244
245
+ /**
246
+ * Older version, the new addition of prefixes has been made mandatory by 1.17
247
+ */
244
248
static Class <?> getMinecraftClass (String className ) throws NMSException {
249
+ return getMinecraftClass ("" , className );
250
+ }
251
+
252
+ static Class <?> getMinecraftClass (String prefix , String className ) throws NMSException {
245
253
try {
246
- return Reflection .getMinecraftClassByName (className );
254
+ return Reflection
255
+ .getMinecraft1_17ClassByName (prefix .equals ("" ) ? className : prefix + "." + className ); //1.17+
247
256
} catch (ClassNotFoundException ex ) {
248
- throw new NMSException ("Unable to find class: " + className , ex );
257
+ try {
258
+ return Reflection .getMinecraftClassByName (className );//Legacy for older version than 1.17
259
+ } catch (ClassNotFoundException e ) {
260
+ throw new NMSException ("Unable to find class: " + prefix + className , e );
261
+ }
249
262
}
250
263
}
251
264
@@ -262,8 +275,8 @@ private static void init() throws NMSException {
262
275
return ; // Already initialized
263
276
}
264
277
265
- MC_ITEM_STACK = getMinecraftClass ("ItemStack" );
266
- MC_NBT_TAG_COMPOUND = getMinecraftClass ("NBTTagCompound" );
278
+ MC_ITEM_STACK = getMinecraftClass ("world.item" , " ItemStack" );
279
+ MC_NBT_TAG_COMPOUND = getMinecraftClass ("nbt" , " NBTTagCompound" );
267
280
CB_CRAFT_ITEM_META = getCraftBukkitClass ("inventory.CraftMetaItem" );
268
281
}
269
282
@@ -279,29 +292,49 @@ private static void init() throws NMSException {
279
292
* @throws NMSException If something goes wrong while extracting the tag.
280
293
*/
281
294
private static Object getMcNBTCompound (ItemStack item ) throws NMSException {
295
+
282
296
Object mcItemStack = ItemUtils .getNMSItemStack (item );
283
297
if (mcItemStack == null ) {
284
298
return null ;
285
299
}
286
300
287
301
try {
288
- Object tag = Reflection .getFieldValue (MC_ITEM_STACK , mcItemStack , "tag" );
289
-
290
- if (tag == null ) {
291
- tag = Reflection .instantiate (MC_NBT_TAG_COMPOUND );
302
+ Object tagCompound ;
303
+ try {
304
+ //1.18
305
+ tagCompound = Reflection .call (mcItemStack .getClass (), mcItemStack , "t" );
306
+ } catch (Exception e ) {
307
+ //1.17
308
+ tagCompound = Reflection .call (mcItemStack .getClass (), mcItemStack , "a" );
309
+ }
292
310
293
- try {
294
- Reflection .call (MC_ITEM_STACK , mcItemStack , "setTag" , tag );
295
- } catch (NoSuchMethodException e ) {
296
- // If the set method change—more resilient,
297
- // as the setTag will only update the field without any kind of callback.
298
- Reflection .setFieldValue (MC_ITEM_STACK , mcItemStack , "tag" , tag );
299
- }
311
+ if (tagCompound == null ) {
312
+ tagCompound = Reflection .instantiate (MC_NBT_TAG_COMPOUND );
313
+ Reflection .call (MC_ITEM_STACK , mcItemStack , "setTag" , tagCompound );
300
314
}
315
+ return tagCompound ;
316
+
317
+ } catch (Exception exc ) {
318
+ //Older method
319
+ try {
320
+ Object tag = Reflection .getFieldValue (MC_ITEM_STACK , mcItemStack , "tag" );
321
+
322
+ if (tag == null ) {
323
+ tag = Reflection .instantiate (MC_NBT_TAG_COMPOUND );
324
+
325
+ try {
326
+ Reflection .call (MC_ITEM_STACK , mcItemStack , "setTag" , tag );
327
+ } catch (NoSuchMethodException e ) {
328
+ // If the set method change—more resilient,
329
+ // as the setTag will only update the field without any kind of callback.
330
+ Reflection .setFieldValue (MC_ITEM_STACK , mcItemStack , "tag" , tag );
331
+ }
332
+ }
301
333
302
- return tag ;
303
- } catch (Exception ex ) {
304
- throw new NMSException ("Unable to retrieve NBT tag from item" , ex );
334
+ return tag ;
335
+ } catch (Exception ex ) {
336
+ throw new NMSException ("Unable to retrieve NBT tag from item" , ex );
337
+ }
305
338
}
306
339
}
307
340
@@ -316,7 +349,6 @@ static Object fromNativeValue(Object value) {
316
349
if (value == null ) {
317
350
return null ;
318
351
}
319
-
320
352
NBTType type = NBTType .fromClass (value .getClass ());
321
353
return type .newTag (value );
322
354
}
0 commit comments