Class ItemCapability<T,C>

java.lang.Object
net.neoforged.neoforge.capabilities.BaseCapability<T,C>
net.neoforged.neoforge.capabilities.ItemCapability<T,C>
Type Parameters:
T - type of queried objects
C - type of the additional context

public final class ItemCapability<T,C> extends BaseCapability<T,C>
An ItemCapability gives flexible access to objects of type T from item stacks.

Querying an item capability

To get an object of type T, use IItemStackExtension.getCapability(ItemCapability). For example, to query an item handler from an item stack:


 ItemStack stack = ...;

 IItemHandler maybeHandler = stack.getCapability(Capabilities.ItemHandler.ITEM);
 if (maybeHandler != null) {
     // Use maybeHandler
 }
 

Providing an item capability

To provide objects of type T, register providers to RegisterCapabilitiesEvent. For example:


 modBus.addListener((RegisterCapabilitiesEvent event) -> {
     event.registerItem(
         Capabilities.ItemHandler.ITEM, // capability to register for
         (itemStack, context) -> <return the IItemHandler for the itemStack>,
         MY_ITEM);
 });
 
  • Field Details

  • Constructor Details

  • Method Details

    • create

      public static <T, C> ItemCapability<T,C> create(ResourceLocation name, Class<T> typeClass, Class<C> contextClass)
      Creates a new item capability, or gets it if it already exists.
      Parameters:
      name - name of the capability
      typeClass - type of the queried API
      contextClass - type of the additional context
    • createVoid

      public static <T> ItemCapability<T,Void> createVoid(ResourceLocation name, Class<T> typeClass)
      Creates a new item capability with Void context, or gets it if it already exists. This should be used for capabilities that do not require any additional context.
      See Also:
    • getAll

      public static List<ItemCapability<?,?>> getAll()
      Returns a new immutable copy of all the currently known item capabilities.
      Returns:
      a new immutable copy of all the currently known item capabilities
    • getCapability

      @Internal @Nullable public T getCapability(ItemStack stack, C context)