3131import java .lang .reflect .InvocationTargetException ;
3232import java .lang .reflect .Method ;
3333import java .util .Arrays ;
34+ import java .util .List ;
3435import java .util .Locale ;
3536import java .util .concurrent .CompletableFuture ;
3637import java .util .function .Supplier ;
@@ -166,10 +167,11 @@ private static final class ModernParser<C> implements ArgumentParser.FutureArgum
166167 CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "b" ),
167168 CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "item" )
168169 );
169- private static final Field COMPOUND_TAG_FIELD = CraftBukkitReflection .firstNonNullOrThrow (
170+ private static final Field EXTRA_DATA_FIELD = CraftBukkitReflection .firstNonNullOrThrow (
170171 () -> "Couldn't find tag field on ItemInput" ,
171172 CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "c" ),
172- CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "tag" )
173+ CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "tag" ),
174+ CraftBukkitReflection .findField (ITEM_INPUT_CLASS , "components" )
173175 );
174176 private static final Class <?> HOLDER_CLASS = CraftBukkitReflection .findMCClass ("core.Holder" );
175177 private static final @ Nullable Method VALUE_METHOD = HOLDER_CLASS == null
@@ -179,6 +181,12 @@ private static final class ModernParser<C> implements ArgumentParser.FutureArgum
179181 CraftBukkitReflection .findMethod (HOLDER_CLASS , "value" ),
180182 CraftBukkitReflection .findMethod (HOLDER_CLASS , "a" )
181183 );
184+ private static final Class <?> NBT_TAG_CLASS = CraftBukkitReflection .firstNonNullOrThrow (
185+ () -> "Cloud not find net.minecraft.nbt.Tag" ,
186+ CraftBukkitReflection .findClass ("net.minecraft.nbt.Tag" ),
187+ CraftBukkitReflection .findClass ("net.minecraft.nbt.NBTBase" ),
188+ CraftBukkitReflection .findNMSClass ("NBTBase" )
189+ );
182190
183191 private final ArgumentParser <C , ProtoItemStack > parser ;
184192
@@ -225,7 +233,7 @@ private static final class ModernProtoItemStack implements ProtoItemStack {
225233
226234 private final Object itemInput ;
227235 private final Material material ;
228- private final @ Nullable String snbt ;
236+ private final boolean hasExtraData ;
229237
230238 ModernProtoItemStack (final @ NonNull Object itemInput ) {
231239 this .itemInput = itemInput ;
@@ -235,11 +243,17 @@ private static final class ModernProtoItemStack implements ProtoItemStack {
235243 item = VALUE_METHOD .invoke (item );
236244 }
237245 this .material = (Material ) GET_MATERIAL_METHOD .get ().invoke (null , item );
238- final Object compoundTag = COMPOUND_TAG_FIELD .get (itemInput );
239- if (compoundTag ! = null ) {
240- this .snbt = compoundTag . toString () ;
246+ final Object extraData = EXTRA_DATA_FIELD .get (itemInput );
247+ if (NBT_TAG_CLASS . isInstance ( extraData ) || extraData = = null ) {
248+ this .hasExtraData = extraData != null ;
241249 } else {
242- this .snbt = null ;
250+ final List <Method > isEmptyMethod = Arrays .stream (extraData .getClass ().getMethods ())
251+ .filter (it -> it .getParameterCount () == 0 && it .getReturnType ().equals (boolean .class ))
252+ .collect (Collectors .toList ());
253+ if (isEmptyMethod .size () != 1 ) {
254+ throw new IllegalStateException ("Failed to locate DataComponentMap/Patch#isEmpty; size=" + isEmptyMethod .size ());
255+ }
256+ this .hasExtraData = !(boolean ) isEmptyMethod .get (0 ).invoke (extraData );
243257 }
244258 } catch (final ReflectiveOperationException ex ) {
245259 throw new RuntimeException (ex );
@@ -253,7 +267,7 @@ private static final class ModernProtoItemStack implements ProtoItemStack {
253267
254268 @ Override
255269 public boolean hasExtraData () {
256- return this .snbt != null ;
270+ return this .hasExtraData ;
257271 }
258272
259273 @ Override
0 commit comments