Class ItemCapability<T, C extends @Nullable Object>

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 @Nullable Object> 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);
});