Heads up!
This page assumes you have exported the assets properly as per How to Export Your Project
How to animate Block Entities
Creating your custom AzBlockAnimator
For this, you will create a class which extends AzBlockAnimator
. This class will handle the registration of animation controllers and providing the animation file's location for the animations.
Example:
public class ExampleBEAnimator extends AzBlockAnimator<ExampleBE> {private static final ResourceLocation ANIMATIONS = ResourceLocation.fromNamespaceAndPath(YOUR_MOD_ID,"animations/item/examplebe.animation.json");@Overridepublic void registerControllers(AzAnimationControllerContainer<ExampleBE> animationControllerContainer) {animationControllerContainer.add(AzAnimationController.builder(this, "base_controller").build());}@Overridepublic @NotNull ResourceLocation getAnimationLocation(ExampleBE animatable) {return ANIMATIONS;}}
Some highlights of the above example.
-
ANIMATIONS
: This stores theResourceLocation
reference to the animation JSON file. You need to replaceYOUR_MOD_ID
with your mod's ID and ensure the file path matches your JSON animation file. -
registerControllers()
Registers the animation controllers that will define the animation behavior and states for the item. You can multiple if needed.
Creating an Animation Dispatcher
This class isn't required but is highly suggested to help store your animation commands to call in other classes.
Example:
public class ExampleBEDispatcher {private static final AzCommand SPINNING_COMMAND = AzCommand.create("base_controller","spinning",AzPlayBehaviors.LOOP);public void spin(BlockEntity entity) {SPINNING_COMMAND.sendForItem(entity, itemStack);}}
Accessing your Dispatcher to trigger animations
You will now define your ExampleBEDispatcher from above in your BlockEntity class.
public class ExampleBE extends BlockEntity {// This is your class where you will setup the AzCommands/Animations you wish to playpublic final ExampleBEDispatcher dispatcher;public ExampleBE(BlockPos pos, BlockState blockState) {super(YourEntityTypes.EXAMPLE_BE, pos, blockState);// Create the instance of the class here to use later.this.dispatcher = new ExampleBEDispatcher();}}
Now you simply call the dispatcher wherever you want to play the animations.
Example:
Heads up!
This methed is set in your Block Entity class and registered to your Block classes getTicker method.
public static void tick(Level level,BlockPos pos,BlockState state,ExampleBE blockEntity) {if (blockEntity.level != null && level.isClientSide()) {// This is where you now trigger an animation to playblockEntity.animationDispatcher.spin(blockEntity);}}
Creating your Renderer
This renderer is responsible for how your custom block entity is displayed in the game.
It connects the block entity with the following:
- Geometry file (
geo.json
): Defines the 3D model of the block entity. - Texture file (
.png
): The visual appearance of the entity (applies over the geometry). - Animator: Animations for the entity, provided by
ExampleBEAnimator
.
The renderer class must extend AzBlockEntityRenderer<ExampleBE>
and provide the required configuration.
Heads up!
See AzRendererConfigs 101 for all AzBlockEntityRendererConfig#builder options.
Example Renderer
public class ExampleBERenderer extends AzBlockEntityRenderer<ExampleBE> {private static final ResourceLocation GEO = ResourceLocation.fromNamespaceAndPath(YOUR_MOD_ID,"geo/block/examplebe.geo.json");private static final ResourceLocation TEX = ResourceLocation.fromNamespaceAndPath(YOUR_MOD_ID,"textures/block/examplebe.png");public ExampleBERenderer() {super(AzBlockEntityRendererConfig.<ExampleBE>builder(GEO, TEX).setAnimatorProvider(ExampleBEAnimator::new).build());}}
Registering your Renderer
Nothing special is required here! Simply call render your block entity render as per the standard use case of your modloader.