diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java index 5384f74b8..b7ba0fe43 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java @@ -273,7 +273,7 @@ public static T modify(Entity entity, Function function) { /** * It takes an ItemStack and a Consumer<ReadWriteNBT>, 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. @@ -291,7 +291,7 @@ public static void modifyComponents(ItemStack item, Consumer consu /** * It takes an ItemStack and a Consumer<ReadWriteNBT>, 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. @@ -307,6 +307,39 @@ public static T modifyComponents(ItemStack item, Function f item.setItemMeta(tmp.getItemMeta()); return ret; } + + /** + * It takes an ItemStack and a Consumer<ReadWriteNBT>, 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 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<ReadWriteNBT>, 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 getComponents(ItemStack item, Function 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