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 objects
C - type of the additional context

public final class EntityCapability<T,C> extends BaseCapability<T,C>
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 Details

  • Constructor Details

  • 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 capability
      typeClass - type of the queried API
      contextClass - type of the additional context
    • createVoid

      public static <T> EntityCapability<T,Void> createVoid(ResourceLocation name, Class<T> typeClass)
      Creates a new entity 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:
    • createSided

      public static <T> EntityCapability<T,@Nullable Direction> createSided(ResourceLocation name, Class<T> typeClass)
      Creates a new entity capability with nullable Direction context, or gets it if it already exists. The side is generally the side from which the entity is being accessed, or null if it is not known or not a specific side.
    • getAll

      public static List<EntityCapability<?,?>> 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

      @Internal @Nullable public T getCapability(Entity entity, C context)