Class EntityAnimationWrapper<E extends net.minecraft.world.entity.LivingEntity,M extends IModelPartProvider<E>>

java.lang.Object
dev.oxydien.mathimations.animations.EntityAnimationWrapper<E,M>

public class EntityAnimationWrapper<E extends net.minecraft.world.entity.LivingEntity,M extends IModelPartProvider<E>> extends Object
Wrapper for the EntityAnimator. This is a helper class to make it easier to manage animations for entities separately.

Each entity gets its own animator instance with configurable layer support. Global animations can be registered to specific layers and are automatically applied to all entities when they are initialized.

  • Field Details

  • Constructor Details

    • EntityAnimationWrapper

      public EntityAnimationWrapper(M modelProvider)
      Creates an entity animation wrapper with default settings (8 layers, no global animations).
      Parameters:
      modelProvider - The model provider for the entity
    • EntityAnimationWrapper

      public EntityAnimationWrapper(byte layerCount, M modelProvider)
      Creates an entity animation wrapper with the specified number of layers.
      Parameters:
      layerCount - The number of layers for each entity's animator (should be power of 2: 1, 2, 4, 8, 16, 32 for easier synchronization)
      modelProvider - The model provider for the entity
    • EntityAnimationWrapper

      public EntityAnimationWrapper(HashMap<Short,IMathAnimation<E>> globalAnimations, M modelProvider)
      Creates an entity animation wrapper with the given global animations on layer 0.

      Usage (in your LivingEntityRenderer constructor):

       this.entityAnimationWrapper = new EntityAnimationWrapper<>(new HashMap<>() {{
           put((short) 0, new YourAnimation1());
           put((short) 1, new YourAnimation2());
           // Or make constants
           put((short) RUN_ANIMATION_ID, new YourAnimation3());
           // etc...
       }});
       
      Parameters:
      globalAnimations - The global animations to apply to all entities on layer 0
      modelProvider - The model provider for the entity
    • EntityAnimationWrapper

      public EntityAnimationWrapper(byte layerCount, byte targetLayer, HashMap<Short,IMathAnimation<E>> globalAnimations, M modelProvider)
      Creates an entity animation wrapper with the given global animations on a specific layer.
      Parameters:
      layerCount - The number of layers for each entity's animator (must be power of 2: 1, 2, 4, 8, 16, 32)
      targetLayer - The layer index to register global animations to
      globalAnimations - The global animations to apply to all entities
      modelProvider - The model provider for the entity
      Throws:
      IllegalArgumentException - if layerCount is invalid or targetLayer is out of bounds
  • Method Details

    • initOrUpdate

      public void initOrUpdate(@NotNull E entity, float partialTicks)
      Initializes or updates an entity with the given model provider.

      Call this in the LivingEntityRenderer.render(LivingEntity, float, float, PoseStack, MultiBufferSource, int) method BEFORE THE super::render CALL, to initialize or update the animations for the given entity.

      If the entity is not already registered, a new animator is created with the configured number of layers and all global animations are registered to their respective layers.

      If the entity implements IAutoAnimation, the animation is automatically updated.

      Finally, calls EntityAnimator.updatePose(float) to update all active animations for this entity.

      Parameters:
      entity - The entity to initialize or update
      partialTicks - The partial tick time
    • registerGlobalAnimation

      public void registerGlobalAnimation(byte layerIndex, short animationId, IMathAnimation<E> animation)
      Registers a global animation to a specific layer. The animation is automatically added to all current and future animators on the specified layer.
      Parameters:
      layerIndex - The layer to register the animation to
      animationId - The ID of the animation
      animation - The animation to add
      Throws:
      IllegalArgumentException - if layerIndex is out of bounds
    • registerGlobalAnimation

      public void registerGlobalAnimation(int layerIndex, int animationId, IMathAnimation<E> animation)
      See Also:
    • registerAnimation

      public void registerAnimation(@NotNull E entity, byte layerIndex, short animationId, IMathAnimation<E> animation)
      Registers an animation to a specific entity on a specific layer. The animation is only added if the entity's animator exists.
      Parameters:
      entity - The entity to add the animation to
      layerIndex - The layer to register the animation to
      animationId - The ID of the animation
      animation - The animation to add
    • registerAnimation

      public void registerAnimation(@NotNull E entity, int layerIndex, int animationId, IMathAnimation<E> animation)
      Wrapper method that converts parameters to required types.
    • playAnimation

      public void playAnimation(@NotNull E entity, byte layerIndex, short animationId)
      Plays an animation on a specific entity and layer. If other animations are active on the same layer, they will transition out while this animation transitions in.
      Parameters:
      entity - The entity to play the animation on
      layerIndex - The layer to play the animation on
      animationId - The ID of the animation to play
    • playAnimation

      public void playAnimation(@NotNull E entity, int layerIndex, int animationId)
      See Also:
    • playAnimationIf

      public void playAnimationIf(@NotNull E entity, byte layerIndex, short animationId, @NotNull @NotNull IAnimationCondition condition)
      Plays an animation on a specific entity and layer if the condition is met.
      Parameters:
      entity - The entity to play the animation on
      layerIndex - The layer to play the animation on
      animationId - The ID of the animation to play
      condition - The condition to check
    • playAnimationIf

      public void playAnimationIf(@NotNull E entity, int layerIndex, int animationId, @NotNull @NotNull IAnimationCondition condition)
      See Also:
    • stopAnimation

      public void stopAnimation(@NotNull E entity, byte layerIndex, short animationId, float transitionTime)
      Stops a specific animation on a specific layer with a transition.
      Parameters:
      entity - The entity whose animation should be stopped
      layerIndex - The layer containing the animation
      animationId - The ID of the animation to stop
      transitionTime - Time in seconds to fade out the animation (-1 to use animation's default transition time)
    • stopAnimation

      public void stopAnimation(@NotNull E entity, int layerIndex, int animationId, float transitionTime)
      See Also:
    • stopLayer

      public void stopLayer(@NotNull E entity, byte layerIndex, float transitionTime)
      Stops all animations on a specific layer with a transition.
      Parameters:
      entity - The entity whose layer should be stopped
      layerIndex - The layer to stop
      transitionTime - Time in seconds to fade out all animations (-1 to use animation's default transition time)
    • stopLayer

      public void stopLayer(@NotNull E entity, int layerIndex, float transitionTime)
      See Also:
    • getAnimator

      public EntityAnimator<E,M> getAnimator(@NotNull E entity)
      Gets the animator for a specific entity, allowing direct access to animator methods.
      Parameters:
      entity - The entity to get the animator for
      Returns:
      The animator for the entity, or null if not found
    • resetEntity

      public void resetEntity(@NotNull E entity)
      Resets the animator for the given entity, calling EntityAnimator.reset() on it.

      Call this after the entity has been rendered to reset the model for the next render (if needed).

      This call is not needed if you use initOrUpdate(LivingEntity, float). The animations will be calculated based on the initial poses of the entity's model.

      Parameters:
      entity - The entity to reset the animator for
    • remove

      public void remove(@NotNull E entity)
      Removes the animator for the given entity from the internal map.

      Call this whenever the entity dies, is no longer rendered, or no longer needs to be animated to free up memory.

      Parameters:
      entity - The entity to remove the animator for
    • getLayerCount

      public int getLayerCount()
      Gets the number of layers configured for this wrapper.
      Returns:
      The number of layers