Class ItemResource

java.lang.Object
net.neoforged.neoforge.transfer.item.ItemResource
All Implemented Interfaces:
DataComponentGetter, DataComponentHolder, TypedInstance<Item>, IDataComponentHolderExtension, DataComponentHolderResource<Item>, RegisteredResource<Item>, Resource

public final class ItemResource extends Object implements DataComponentHolderResource<Item>
Immutable combination of an Item and data components. Similar to an ItemStack, but immutable and without a count.
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • EMPTY

      public static final ItemResource EMPTY
      The empty resource instance of a ItemResource
    • CODEC

      public static final com.mojang.serialization.Codec<ItemResource> CODEC
      Codec for an item resource. Does not accept empty resources.
    • OPTIONAL_CODEC

      public static final com.mojang.serialization.Codec<ItemResource> OPTIONAL_CODEC
      Codec for an item resource. Same format as CODEC, and also accepts empty resources.
    • STREAM_CODEC

      public static final StreamCodec<RegistryFriendlyByteBuf, ItemResource> STREAM_CODEC
      Stream codec for an item resource. Accepts empty resources.
    • innerStack

      private final ItemStack innerStack
      A wrapped ItemStack which must never be modified or exposed. This will be a size of 1 so that we can make use of the fact it is already an instance with a data component map.
  • Constructor Details

    • ItemResource

      private ItemResource(ItemStack stack)
  • Method Details

    • of

      public static ItemResource of(ItemStack stack)
      Creates an ItemResource using the default or copy of the passed in item stack. Note the count is lost.
      Parameters:
      stack - stack to copy with a size of 1
      Returns:
      If there were no patches on the stack's data components, the item's default resource will be returned, otherwise a new instance with the copied stack.
    • of

      public static ItemResource of(@Nullable ItemStackTemplate template)
      Creates an ItemResource using the default or copy of the passed in item stack. Note the count is lost.
      Parameters:
      template - stack to copy with a size of 1
      Returns:
      If null was given, an empty resource is returned. If there were no patches on the stack's data components, the item's default resource will be returned, otherwise a new instance with the copied stack.
    • of

      public static ItemResource of(ItemLike item)
      Note: This cannot be called before your item is registered
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static ItemResource of(ItemLike item, DataComponentPatch patch)
      Note: This cannot be called before your item is registered
      Parameters:
      item - Item to create the resource with.
      patch - Data components that should be on the resource instance.
      Returns:
      a new ItemResource. If the item is empty, then EMPTY will be returned; If the patch matches the default values the default instance of that item will be provided.
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static ItemResource of(Holder<Item> holder)
      Note: This cannot be called before your item is registered
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static ItemResource of(Holder<Item> holder, DataComponentPatch patch)
      Note: This cannot be called before your item is registered
      Parameters:
      holder - Item holder to create the resource with.
      patch - Data components that should be on the resource instance.
      Returns:
      a new ItemResource. If the item is empty, then EMPTY will be returned; If the patch matches the default values the default instance of that item will be provided.
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • value

      public Item value()
      Description copied from interface: RegisteredResource
      Returns the backing instance of the resource.
      Specified by:
      value in interface RegisteredResource<Item>
      Returns:
      the backing instance of the resource
    • getItem

      public Item getItem()
      Returns:
      The Item of the resource from the inner ItemStack
    • typeHolder

      public Holder<Item> typeHolder()
      Specified by:
      typeHolder in interface TypedInstance<Item>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Resource
      Returns true if this represents an empty resource.

      Examples include item resource with air as an item, or fluid resource with empty fluid.

      Specified by:
      isEmpty in interface Resource
    • matches

      public boolean matches(ItemStack stack)
      Returns true if this resource matches the item and components of the passed stack.
      Parameters:
      stack - the item stack to check
      Returns:
      true if this resource matches the item and components of the passed stack
    • matches

      public boolean matches(@Nullable ItemStackTemplate template)
      Returns true if this resource matches the item and components of the passed template.
      Parameters:
      template - the item stack template to check
      Returns:
      true if this resource matches the item and components of the passed template
    • is

      public boolean is(ItemLike item)
      Returns true if the item instance matches the backing instance value.
      Parameters:
      item - the item to check
      Returns:
      true if the item instance matches the backing instance value
    • test

      public boolean test(Predicate<ItemStack> predicate)
      Tests an ItemStack predicate with the inner stack.
      Parameters:
      predicate - Predicate to perform the test with
      Returns:
      true if the test passed
    • isComponentsPatchEmpty

      public boolean isComponentsPatchEmpty()
      Description copied from interface: DataComponentHolderResource
      Checks if the resource's data component holder has no patches applied to it. Equivalent to checking if the patch is empty, but more efficient.
      Specified by:
      isComponentsPatchEmpty in interface DataComponentHolderResource<Item>
    • withMergedPatch

      public ItemResource withMergedPatch(DataComponentPatch patch)
      Description copied from interface: DataComponentHolderResource
      Returns an instance of the resource with the desired patch applied on top of the existing one.
      Specified by:
      withMergedPatch in interface DataComponentHolderResource<Item>
      Parameters:
      patch - The patch added to the new resource instance.
      Returns:
      an instance of the resource with the desired patch applied on top of the existing one
    • with

      public <D> ItemResource with(DataComponentType<D> type, D data)
      Description copied from interface: DataComponentHolderResource
      Returns a resource with the data component set to the given value.
      Specified by:
      with in interface DataComponentHolderResource<Item>
      Type Parameters:
      D - the type of data component
      Parameters:
      type - the type of data component
      data - the data to set
      Returns:
      a resource with the data component set to the given value
    • with

      public <D> ItemResource with(Supplier<? extends DataComponentType<D>> type, D data)
      Description copied from interface: DataComponentHolderResource
      Returns a resource with the data component set to the given value.
      Specified by:
      with in interface DataComponentHolderResource<Item>
      Type Parameters:
      D - the type of data component
      Parameters:
      type - the supplier for the type of data component
      data - the data to set
      Returns:
      a resource with the data component set to the given value
    • without

      public ItemResource without(DataComponentType<?> type)
      Description copied from interface: DataComponentHolderResource
      Returns a resource without the data component, i.e. with the data component explicitly removed.
      Specified by:
      without in interface DataComponentHolderResource<Item>
      Parameters:
      type - the type of data component
      Returns:
      a resource without the data component, i.e. with the data component explicitly removed
    • without

      public ItemResource without(Supplier<? extends DataComponentType<?>> type)
      Description copied from interface: DataComponentHolderResource
      Returns a resource without the data component, i.e. with the data component explicitly removed.
      Specified by:
      without in interface DataComponentHolderResource<Item>
      Parameters:
      type - the supplier for the type of data component
      Returns:
      a resource without the data component, i.e. with the data component explicitly removed
    • getComponents

      public DataComponentMap getComponents()
      Specified by:
      getComponents in interface DataComponentHolder
    • getComponentsPatch

      public DataComponentPatch getComponentsPatch()
      Description copied from interface: DataComponentHolderResource
      Patches currently applied to the resource's data component holder.
      Specified by:
      getComponentsPatch in interface DataComponentHolderResource<Item>
    • toStack

      public ItemStack toStack(int count)
      Creates an ItemStack of the specified count.
      Parameters:
      count - The amount of the item the stack should have. Must be non-negative.
      Throws:
      IllegalArgumentException - when count is negative.
    • toStack

      public ItemStack toStack()
      Creates an ItemStack with a count of 1.
    • getMaxStackSize

      public int getMaxStackSize()
      See Also:
    • getHoverName

      public Component getHoverName()
      Returns the hover name of the ItemStack.
      Returns:
      The hover name of the ItemStack
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object