Skip to content

Commit 81650e2

Browse files
committed
Made the NBT part better
Also improve the javadocs
1 parent 5f42c64 commit 81650e2

File tree

2 files changed

+83
-41
lines changed

2 files changed

+83
-41
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/CreateItemStack.java

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.broken.arrow.library.itemcreator.utility.ConvertToItemStack;
1111
import org.broken.arrow.library.itemcreator.utility.Tuple;
1212
import org.broken.arrow.library.itemcreator.utility.builders.ItemBuilder;
13+
import org.broken.arrow.library.itemcreator.utility.nbt.NBTDataWriter;
1314
import org.broken.arrow.library.logging.Logging;
1415
import org.broken.arrow.library.logging.Validate;
1516
import 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
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/NBTDataWrapper.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.reflect.TypeToken;
44
import com.google.gson.Gson;
5+
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
56
import org.broken.arrow.library.itemcreator.utility.nbt.NBTDataWriter;
67
import org.broken.arrow.library.itemcreator.utility.nbt.NBTValue;
78
import org.broken.arrow.library.nbt.RegisterNbtAPI;
@@ -40,7 +41,6 @@ public final class NBTDataWrapper {
4041
private final float serverVersion;
4142
private final Plugin plugin;
4243
private final PersistentDataUtility persistentData;
43-
4444
private Consumer<NBTDataWriter> consumer;
4545

4646
/**
@@ -65,6 +65,18 @@ public static NBTDataWrapper of(@Nonnull final ItemCreator itemCreator) {
6565
return new NBTDataWrapper(itemCreator);
6666
}
6767

68+
/**
69+
* Sets a custom NBT application function to be used when applying NBT data.
70+
* Compared to {@link #add(String, Object, boolean)} and
71+
* {@link #add(String, Object)}, this gives you greater control over
72+
* which metadata is applied and also allows removing keys.
73+
*
74+
* @param function a consumer that modifies the provided {@link NBTDataWriter}
75+
*/
76+
public void applyNBT(Consumer<NBTDataWriter> function) {
77+
this.consumer = function;
78+
}
79+
6880
/**
6981
* Add metadata to your item. To get the metadata with method {@link #getMetaDataMap()}
7082
* This method will convert value to string always, use {@link #add(String, Object, boolean)}
@@ -100,15 +112,6 @@ public Map<String, Object> getMetaDataMap() {
100112
return itemMetaMap;
101113
}
102114

103-
/**
104-
* Sets a custom NBT application function to be used when applying NBT data.
105-
*
106-
* @param function a consumer that modifies the provided {@link NBTDataWriter}
107-
*/
108-
public void applyNBT(Consumer<NBTDataWriter> function) {
109-
this.consumer = function;
110-
}
111-
112115
/**
113116
* Returns the current NBT application consumer.
114117
*
@@ -130,11 +133,9 @@ public ItemStack applyNBT(ItemStack itemStack) {
130133
final NBTDataWriter nbtData = new NBTDataWriter();
131134
apply(nbtData);
132135
final RegisterNbtAPI nbtApi = this.itemCreator.getNbtApi();
133-
final Map<String, NBTValue> nbtCache = nbtData.getNbtCache();
134-
final Map<String, Object> metaDataMap = this.getMetaDataMap();
135136

136137
if (nbtApi != null) {
137-
return applyNbtToItem(nbtApi,itemStack, nbtData);
138+
return applyNbtToItem(nbtApi, itemStack, nbtData);
138139
} else {
139140
this.setPersistentData(itemStack, nbtData);
140141
}
@@ -256,13 +257,18 @@ private ItemStack applyNbtToItem(final RegisterNbtAPI nbtApi, final ItemStack it
256257
if (nbtData.isClearNBT())
257258
nbtDataWrite.clearNBT();
258259
else {
260+
final ReadWriteNBT compound = nbtDataWrite.getCompound();
259261
if (!metaDataMap.isEmpty())
260262
metaDataMap.forEach((s, nbtValue) ->
261-
ConvertObjectType.setNBTValue(nbtDataWrite.getCompound(), s, nbtValue)
263+
ConvertObjectType.setNBTValue(compound, s, nbtValue)
262264
);
263265
else
264266
nbtCache.forEach((key, nbtValue) -> {
265-
ConvertObjectType.setNBTValue(nbtDataWrite.getCompound(), key, nbtValue);
267+
if (nbtValue.isRemoveKey()) {
268+
compound.removeKey(key);
269+
} else {
270+
ConvertObjectType.setNBTValue(compound, key, nbtValue.getValue());
271+
}
266272
});
267273
}
268274
});

0 commit comments

Comments
 (0)