类 EntityCapability<T,C extends @Nullable Object>

java.lang.Object
net.neoforged.neoforge.capabilities.BaseCapability<T,C>
net.neoforged.neoforge.capabilities.EntityCapability<T,C>
类型参数:
T - type of queried objects
C - type of the additional context

public final class EntityCapability<T,C extends @Nullable Object> 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>);
 });
 
  • 字段详细资料

  • 构造器详细资料

  • 方法详细资料

    • create

      public static <T, C extends @Nullable Object> EntityCapability<T,C> create(ResourceLocation name, Class<T> typeClass, Class<C> contextClass)
      Creates a new entity capability, or gets it if it already exists.
      参数:
      name - name of the capability
      typeClass - type of the queried API
      contextClass - type of the additional context
    • createVoid

      public static <T> EntityCapability<T,@Nullable 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.
      另请参阅:
    • 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()
      返回 a new immutable copy of all the currently known entity capabilities。
      返回:
      a new immutable copy of all the currently known entity capabilities
    • getCapability

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