Skip to content

Commit

Permalink
Add getComponents methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Oct 22, 2024
1 parent e3142b4 commit 410e88a
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static <T> T modify(Entity entity, Function<ReadWriteNBT, T> function) {
/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quiet expensive, so don't overuse it.
* This method is quite expensive, so don't overuse it.
*
* @param item The item you want to modify the components of
* @param consumer The consumer that will be used to modify the components.
Expand All @@ -291,7 +291,7 @@ public static void modifyComponents(ItemStack item, Consumer<ReadWriteNBT> consu
/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quiet expensive, so don't overuse it.
* This method is quite expensive, so don't overuse it.
*
* @param item The item you want to modify the components of
* @param function The consumer that will be used to modify the components.
Expand All @@ -307,6 +307,39 @@ public static <T> T modifyComponents(ItemStack item, Function<ReadWriteNBT, T> f
item.setItemMeta(tmp.getItemMeta());
return ret;
}

/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quite expensive, try to cache the results/use it smartly.
*
* @param item The item you want to read the components of
* @param consumer The consumer that will be used to read the components.
*/
public static void getComponents(ItemStack item, Consumer<ReadableNBT> consumer) {
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
throw new NbtApiException("This method only works for 1.20.5+!");
}
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
consumer.accept(nbti.getOrCreateCompound("components"));
}

/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quite expensive, try to cache the results/use it smartly.
*
* @param item The item you want to read the components of
* @param function The consumer that will be used to read the components.
* @return The return type is the same as the return type of the function.
*/
public static <T> T getComponents(ItemStack item, Function<ReadableNBT, T> function) {
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
throw new NbtApiException("This method only works for 1.20.5+!");
}
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
return function.apply(nbti.getOrCreateCompound("components"));
}

/**
* It takes an entity and a function that takes a ReadWriteNBT and applies the
Expand Down

0 comments on commit 410e88a

Please sign in to comment.