Class EntityCapability<T,C>
java.lang.Object
net.neoforged.neoforge.capabilities.BaseCapability<T,C>
net.neoforged.neoforge.capabilities.EntityCapability<T,C>
- Type Parameters:
T
- type of queried objectsC
- type of the additional context
An
EntityCapability
gives flexible access to objects of type T
from entities.
Querying an entity capability
To get an object of type T
, use Entity.getCapability(EntityCapability)
.
For example, to query an item handler from an entity:
Entity entity = ...;
IItemHandler maybeHandler = entity.getCapability(Capabilities.ItemHandler.ENTITY);
if (maybeHandler != null) {
// Use maybeHandler
}
Providing an entity capability
To provide objects of type T
, register providers to RegisterCapabilitiesEvent
. For example:
modBus.addListener((RegisterCapabilitiesEvent event) -> {
event.registerEntity(
Capabilities.ItemHandler.ENTITY, // capability to register for
MY_ENTITY_TYPE,
(myEntity, context) -> <return the IItemHandler for myEntity>);
});
-
Field Summary
Modifier and TypeFieldDescription(package private) final Map
<EntityType<?>, List<ICapabilityProvider<Entity, C, T>>> private static final CapabilityRegistry
<EntityCapability<?, ?>> -
Constructor Summary
ModifierConstructorDescriptionprivate
EntityCapability
(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
C> EntityCapability <T, C> create
(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) Creates a new entity capability, or gets it if it already exists.static <T> EntityCapability
<T, @Nullable Direction> createSided
(ResourceLocation name, Class<T> typeClass) Creates a new entity capability with nullableDirection
context, or gets it if it already exists.static <T> EntityCapability
<T, Void> createVoid
(ResourceLocation name, Class<T> typeClass) Creates a new entity capability withVoid
context, or gets it if it already exists.static List
<EntityCapability<?, ?>> getAll()
Returns a new immutable copy of all the currently known entity capabilities.getCapability
(Entity entity, C context) Methods inherited from class net.neoforged.neoforge.capabilities.BaseCapability
contextClass, name, typeClass
-
Field Details
-
registry
-
providers
-
-
Constructor Details
-
EntityCapability
-
-
Method Details
-
create
public static <T,C> EntityCapability<T,C> create(ResourceLocation name, Class<T> typeClass, Class<C> contextClass) Creates a new entity 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 entity 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:
-
createSided
public static <T> EntityCapability<T,@Nullable Direction> createSided(ResourceLocation name, Class<T> typeClass) Creates a new entity capability with nullableDirection
context, or gets it if it already exists. The side is generally the side from which the entity is being accessed, ornull
if it is not known or not a specific side. -
getAll
Returns a new immutable copy of all the currently known entity capabilities.- Returns:
- a new immutable copy of all the currently known entity capabilities
-
getCapability
-