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 objectsC
- type of the additional context
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 Summary
-
Constructor Summary
ModifierConstructorDescriptionprivate
ItemCapability
(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.static <T> ItemCapability
<T, Void> createVoid
(ResourceLocation name, Class<T> typeClass) Creates a new item capability withVoid
context, or gets it if it already exists.static List
<ItemCapability<?, ?>> getAll()
Returns a new immutable copy of all the currently known item capabilities.getCapability
(ItemStack stack, C context) Methods inherited from class net.neoforged.neoforge.capabilities.BaseCapability
contextClass, name, typeClass
-
Field Details
-
registry
-
providers
-
-
Constructor Details
-
ItemCapability
-
-
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 capabilitytypeClass
- type of the queried APIcontextClass
- type of the additional context
-
createVoid
Creates a new item capability withVoid
context, or gets it if it already exists. This should be used for capabilities that do not require any additional context.- See Also:
-
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
-