How to create an Animated Item

This page will not cover how to code an Item from the ground up, only how to add the required Azurelib code for the animations and model to work.

Needed Steps in Item class

To start, you must implement GeoItem

Override getAnimatableInstanceCache and registerControllers

Instantiate a new AnimatableInstanceCache via AzureLibUtil.createInstanceCache(this) at the top of your entity class and return it in getAnimatableInstanceCache

private final AnimatableInstanceCache cache = AzureLibUtil.createInstanceCache(this);

@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
    return cache;
}

Add any controllers you want for animations in registerControllers

@Override
public void registerControllers(ControllerRegistrar controllers) {
controllers.add(new AnimationController<>(this, "controllerName", 
    event -> PlayState.CONTINUE));
}

Creating the model class

Create a new class that extends GeoModel, then implement all the required methods:

public class ExampleItemModel extends GeoModel<ExampleItem> {
// Models must be stored in assets/<modid>/geo with subfolders supported inside the geo folder
	private static final ResourceLocation model = new ResourceLocation("yournamespace", "geo/yourmodel.geo.json");
// Textures must be stored in assets/<modid>/textures with subfolders supported inside the textures folder
	private static final ResourceLocation texture = new ResourceLocation("yournamespace", "textures/<modeltype>/yourtexture.png");
// Animations must be stored in assets/<modid>/animations with subfolders supported inside the animations folder
	private static final ResourceLocation animation = new ResourceLocation("yournamespace", "animations/youranimation.animation.json");

	@Override
	public ResourceLocation getModelResource(ExampleItem object) {
		return this.model;
	}

	@Override
	public ResourceLocation getTextureResource(ExampleItem object) {
		return this.texture;
	}

	@Override
	public ResourceLocation getAnimationResource(ExampleItem object) {
		return this.animation;
	}
}

Creating the renderer class

Create a class that extends GeoItemRenderer, then just set it like so:

public class ExampleItemRenderer extends GeoItemRenderer<ExampleItem > {
    public ExampleItemRenderer() {
	// Register the Model class to this render
        super(new ExampleItemModel());
    }
}

Registering the renderer

Fabric/NeoForge/Forge 1.20.1+

In your Item class, add:

private final Supplier<Object> renderProvider = GeoItem.makeRenderer(this);

// Creates the render
@Override
public void createRenderer(Consumer<Object> consumer) {
	// Accepts a consumer to create a new renderer
	consumer.accept(new RenderProvider() {
		// Your render class made above
		private ExampleItemRenderer renderer = null;

		@Override
		public BlockEntityWithoutLevelRenderer getCustomRenderer() {
			// Check if renderer is null, create a new instance if so
			if (renderer == null)
				return new ExampleItemRenderer();
			// Return the existing renderer if it's not null
			return this.renderer;
		}
	});
}

@Override
public Supplier<Object> getRenderProvider() {
	// Returns the above renderProvider created above
	return renderProvider;
}

Forge 1.19.4 and older

In your Item class, add:

@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
	// Accepts a consumer to create a new renderer
	consumer.accept(new IClientItemExtensions() {
		// Your render class made above
		private ExampleItemRenderer renderer = null;

		@Override
		public BlockEntityWithoutLevelRenderer getCustomRenderer() {
			// Check if renderer is null, create a new instance if so
			if (renderer == null)
				return new ExampleItemRenderer();
			// Return the existing renderer if it's not null
			return this.renderer;
		}
	});
}

Last updated