1010import org .broken .arrow .library .itemcreator .utility .ConvertToItemStack ;
1111import org .broken .arrow .library .itemcreator .utility .Tuple ;
1212import org .broken .arrow .library .itemcreator .utility .builders .ItemBuilder ;
13+ import org .broken .arrow .library .itemcreator .utility .nbt .NBTDataWriter ;
1314import org .broken .arrow .library .logging .Logging ;
1415import org .broken .arrow .library .logging .Validate ;
1516import org .broken .arrow .library .nbt .RegisterNbtAPI ;
@@ -461,59 +462,89 @@ else if (enchant instanceof Enchantment)
461462 }
462463
463464 /**
464- * Set custom nbt data on item.
465+ * Sets a custom NBT application function to be used when applying NBT data.
466+ * Compared to {@link #setItemMetaData(String, Object, boolean)} and
467+ * {@link #setItemMetaData(String, Object)}, this gives you greater control over
468+ * which metadata is applied and also allows removing keys.
465469 *
466- * @param itemMetaKey key for get value.
467- * @param itemMetaValue value you want to set.
468- * @return this class.
470+ * @param function a consumer that modifies the provided {@link NBTDataWriter}
471+ * @return this instance
469472 */
470- public CreateItemStack setItemMetaData (final String itemMetaKey , final Object itemMetaValue ) {
471- return setItemMetaData (itemMetaKey , itemMetaValue , false );
473+ public CreateItemStack setItemNBT (Consumer <NBTDataWriter > function ) {
474+ createNBTWrapperIfMissing ();
475+ nbtDataWrapper .applyNBT (function );
476+ return this ;
472477 }
473-
478+
474479 /**
475- * Set custom nbt data on item.
480+ * Sets custom NBT data on the item.
476481 *
477- * @param itemMetaKey key for get value.
478- * @param itemMetaValue value you want to set.
479- * @param keepClazz true if it shall keep all data on the item or false to convert value to string.
480- * @return this class.
482+ * <p><b>Note:</b> This method is less flexible and the naming is misleading, and it may be
483+ * removed in the future. Prefer {@link #setItemNBT(java.util.function.Consumer)} when
484+ * working with multiple values, custom logic, or when you need to remove keys.</p>
485+ *
486+ * @param itemMetaKey the key used to retrieve the value
487+ * @param itemMetaValue the value to set
488+ * @return this instance
481489 */
482- public CreateItemStack setItemMetaData (final String itemMetaKey , final Object itemMetaValue , final boolean keepClazz ) {
483- nbtDataWrapper = NBTDataWrapper .of (this .itemCreator ).add (itemMetaKey , itemMetaValue , keepClazz );
484- return this ;
490+ public CreateItemStack setItemMetaData (final String itemMetaKey , final Object itemMetaValue ) {
491+ return setItemMetaData (itemMetaKey , itemMetaValue , false );
485492 }
486493
487494 /**
488- * Set your metadata on the item. Use {@link NBTDataWrapper} class.
489- * To set key and value.
495+ * Sets custom NBT data on the item.
490496 *
491- * @param wrapper values from MetaDataWrapper.
492- * @return this class.
497+ * <p><b>Note:</b> This method is less flexible and the naming is misleading, and it may be
498+ * removed in the future. Prefer {@link #setItemNBT(java.util.function.Consumer)} when
499+ * working with multiple values, custom logic, or when you need to remove keys.</p>
500+ *
501+ * @param itemMetaKey the key used to retrieve the value
502+ * @param itemMetaValue the value to set
503+ * @param keepClazz {@code true} to keep the original value type, or {@code false} to convert
504+ * the value to a string
505+ * @return this instance
493506 */
494- public CreateItemStack setItemMetaDataList (final NBTDataWrapper wrapper ) {
495- nbtDataWrapper = wrapper ;
507+ public CreateItemStack setItemMetaData (final String itemMetaKey , final Object itemMetaValue , final boolean keepClazz ) {
508+ createNBTWrapperIfMissing ();
509+ nbtDataWrapper .add (itemMetaKey , itemMetaValue , keepClazz );
496510 return this ;
497511 }
498512
499513 /**
500514 * Map list of metadata you want to set on a item.
501- * It use map key and value form the map.
515+ * It uses map key and value form the map.
516+ *
517+ * <p><b>Note:</b> This method is less flexible and the naming is misleading, and it may be
518+ * removed in the future. Prefer {@link #setItemNBT(java.util.function.Consumer)} when
519+ * working with multiple values, custom logic, or when you need to remove keys.</p>
502520 *
503521 * @param itemMetaMap map of values.
504522 * @return this class.
505523 */
506524 public CreateItemStack setItemMetaDataList (final Map <String , Object > itemMetaMap ) {
507525 if (itemMetaMap != null && !itemMetaMap .isEmpty ()) {
508- final NBTDataWrapper wrapper = NBTDataWrapper .of (this .itemCreator );
526+ createNBTWrapperIfMissing ();
527+ final NBTDataWrapper wrapper = this .nbtDataWrapper ;
509528 for (final Map .Entry <String , Object > itemData : itemMetaMap .entrySet ()) {
510529 wrapper .add (itemData .getKey (), itemData .getValue ());
511530 }
512- nbtDataWrapper = wrapper ;
513531 }
514532 return this ;
515533 }
516534
535+ /**
536+ * Set your metadata on the item. Use {@link NBTDataWrapper} class.
537+ * To set key and value.
538+ *
539+ * @param wrapper values from MetaDataWrapper.
540+ * @return this class.
541+ */
542+ public CreateItemStack setItemMetaDataList (final NBTDataWrapper wrapper ) {
543+ nbtDataWrapper = wrapper ;
544+ return this ;
545+ }
546+
547+
517548 /**
518549 * if this item is unbreakable or not
519550 *
@@ -939,7 +970,7 @@ private ItemStack checkTypeOfItem(final Object object) {
939970 return getConvertItems ().checkItem (object );
940971 }
941972
942- private void addItemMeta (@ Nonnull final ItemStack itemStack ,@ Nonnull final ItemMeta itemMeta ) {
973+ private void addItemMeta (@ Nonnull final ItemStack itemStack , @ Nonnull final ItemMeta itemMeta ) {
943974 this .setDamageMeta (itemStack , itemMeta );
944975 if (this .serverVersion > 10.0F )
945976 setUnbreakableMeta (itemMeta );
@@ -949,7 +980,7 @@ private void addItemMeta(@Nonnull final ItemStack itemStack,@Nonnull final ItemM
949980 hideEnchantments (itemMeta );
950981 }
951982
952- private void setDamageMeta (final ItemStack itemStack ,final ItemMeta itemMeta ) {
983+ private void setDamageMeta (final ItemStack itemStack , final ItemMeta itemMeta ) {
953984 short dmg = getDmg (itemMeta );
954985 if (dmg > 0 ) {
955986 if (serverVersion > 12.2F ) {
@@ -1036,5 +1067,10 @@ private Map<String, Object> getNBTdataMap() {
10361067 return new HashMap <>();
10371068 }
10381069
1070+ private void createNBTWrapperIfMissing () {
1071+ if (nbtDataWrapper == null )
1072+ nbtDataWrapper = NBTDataWrapper .of (this .itemCreator );
1073+ }
1074+
10391075
10401076}
0 commit comments