Class EntityAnimationWrapper<E extends net.minecraft.world.entity.LivingEntity,M extends IModelPartProvider<E>>
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionEntityAnimationWrapper(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.EntityAnimationWrapper(byte layerCount, M modelProvider) Creates an entity animation wrapper with the specified number of layers.EntityAnimationWrapper(HashMap<Short, IMathAnimation<E>> globalAnimations, M modelProvider) Creates an entity animation wrapper with the given global animations on layer 0.EntityAnimationWrapper(M modelProvider) Creates an entity animation wrapper with default settings (8 layers, no global animations). -
Method Summary
Modifier and TypeMethodDescriptiongetAnimator(E entity) Gets the animator for a specific entity, allowing direct access to animator methods.intGets the number of layers configured for this wrapper.voidinitOrUpdate(E entity, float partialTicks) Initializes or updates an entity with the given model provider.voidplayAnimation(E entity, byte layerIndex, short animationId) Plays an animation on a specific entity and layer.voidplayAnimation(E entity, int layerIndex, int animationId) voidplayAnimationIf(E entity, byte layerIndex, short animationId, @NotNull IAnimationCondition condition) Plays an animation on a specific entity and layer if the condition is met.voidplayAnimationIf(E entity, int layerIndex, int animationId, @NotNull IAnimationCondition condition) voidregisterAnimation(E entity, byte layerIndex, short animationId, IMathAnimation<E> animation) Registers an animation to a specific entity on a specific layer.voidregisterAnimation(E entity, int layerIndex, int animationId, IMathAnimation<E> animation) Wrapper method that converts parameters to required types.voidregisterGlobalAnimation(byte layerIndex, short animationId, IMathAnimation<E> animation) Registers a global animation to a specific layer.voidregisterGlobalAnimation(int layerIndex, int animationId, IMathAnimation<E> animation) voidRemoves the animator for the given entity from the internal map.voidresetEntity(E entity) Resets the animator for the given entity, callingEntityAnimator.reset()on it.voidstopAnimation(E entity, byte layerIndex, short animationId, float transitionTime) Stops a specific animation on a specific layer with a transition.voidstopAnimation(E entity, int layerIndex, int animationId, float transitionTime) voidStops all animations on a specific layer with a transition.void
-
Field Details
-
DEFAULT_LAYER_COUNT
public static final byte DEFAULT_LAYER_COUNT- See Also:
-
-
Constructor Details
-
EntityAnimationWrapper
Creates an entity animation wrapper with default settings (8 layers, no global animations).- Parameters:
modelProvider- The model provider for the entity
-
EntityAnimationWrapper
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
Creates an entity animation wrapper with the given global animations on layer 0.Usage (in your
LivingEntityRendererconstructor):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 0modelProvider- 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 toglobalAnimations- The global animations to apply to all entitiesmodelProvider- The model provider for the entity- Throws:
IllegalArgumentException- if layerCount is invalid or targetLayer is out of bounds
-
-
Method Details
-
initOrUpdate
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 updatepartialTicks- 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 toanimationId- The ID of the animationanimation- The animation to add- Throws:
IllegalArgumentException- if layerIndex is out of bounds
-
registerGlobalAnimation
- 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 tolayerIndex- The layer to register the animation toanimationId- The ID of the animationanimation- 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
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 onlayerIndex- The layer to play the animation onanimationId- The ID of the animation to play
-
playAnimation
- 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 onlayerIndex- The layer to play the animation onanimationId- The ID of the animation to playcondition- 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 stoppedlayerIndex- The layer containing the animationanimationId- The ID of the animation to stoptransitionTime- Time in seconds to fade out the animation (-1 to use animation's default transition time)
-
stopAnimation
- See Also:
-
stopLayer
Stops all animations on a specific layer with a transition.- Parameters:
entity- The entity whose layer should be stoppedlayerIndex- The layer to stoptransitionTime- Time in seconds to fade out all animations (-1 to use animation's default transition time)
-
stopLayer
- See Also:
-
getAnimator
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
Resets the animator for the given entity, callingEntityAnimator.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
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
-