Skip to content

Geckolib Entities (Geckolib3)

Tslat edited this page Nov 5, 2023 · 8 revisions

Steps

Creating a GeckoLib entity requires the following steps:

  1. Creating your Blockbench Model
  2. Creating your Geo Model
  3. Creating your entity class
  4. Creating and registering your renderer

Steps #1 and #2 will not be covered on this page, instead visit their respective links for info. This page will focus on steps #3 and #4

Full Video Guides

Forge

Fabric

The Entity Class

Quick Summary
  1. implement IAnimatable
  2. Override getFactory and registerControllers
  3. Instantiate a new AnimationFactory via GeckoLibUtil.createFactory(this) at the top of your entity class and return it in getFactory
  4. Add any controllers you want for animations in registerControllers

There are only a few things needed to set up a GeckoLib entity's class.

The first is to implement IAnimatable on your entity class, and override the two methods your IDE will tell you to override. This interface is the base of all animatable objects in GeckoLib, and lets the various other features of the mod pick up your entity as an animatable entity.

Next, we'll create an instance of an AnimationFactory for our entity. This stores our animatable instance so that it can be retrieved by the renderer and other outside areas.

To do this, we'll instantiate a new factory via GeckoLibUtil.createFactory(this) at the top of your entity class, caching it in a final variable.

Next, we'll override getFactory in our entity class if it hasn't been done already, and return the factory instance we just created.

And finally, override registerControllers in your entity class. This method is called when your entity is first being used for animations, and is where you define your actual animation handling.

Your class is all set up! The only thing left to do is define your animations in registerControllers

Example Entity Class

public class ExampleEntity extends PathfinderMob implements IAnimatable {
        protected static final AnimationBuilder FLY_ANIM = new AnimationBuilder().addAnimation("move.fly", true);

        private final AnimationFactory factory = GeckoLibUtil.createFactory(this);

        public ExampleEntity(EntityType<? extends ExampleEntity> type, Level level) {
            super(type, level);
        }
    
        @Override
        public void registerControllers(final AnimationData data) {
            data.addAnimationController(new AnimationController<>(this, "Flying", 5, this::flyAnimController));
        }
        
        protected <E extends ExampleEntity> PlayState flyAnimController(final AnimationEvent<E> event) {
            if (event.isMoving()) {
                event.getController().setAnimation(FLY_ANIM);

                return PlayState.CONTINUE;
            }

            return PlayState.STOP;
        }
    
        @Override
        public AnimationFactory getFactory() {
            return this.factory;
        }
}

The Renderer

To have your entity show up in the world, you'll need to register your entity's renderer - like you would for any other entity.

The difference with GeckoLib is that you must register an instance of GeoEntityRenderer, instead of any vanilla renderers.

One bonus to this is that you don't need to register any model layers or mesh definitions like you do with non-GeckoLib models, and instead can skip straight to registering the renderer itself. The renderer will take the render context and your Geo Model instance.

Example Entity Renderer

    public class ExampleEntityRenderer extends GeoEntityRenderer<ExampleEntity> {
        public ExampleEntityRenderer(EntityRendererProvider.Context context) {
            super(context, new ExampleEntityModel());
        }
    }

Common Issues

I get a crash when spawning my entity

If you're getting a crash that says something like this:

java.lang.NullPointerException: Cannot invoke "net.minecraft.client.renderer.entity.EntityRenderer.shouldRender(net.minecraft.world.entity.Entity, net.minecraft.client.renderer.culling.Frustum, double, double, double)" because "entityrenderer" is null

You forgot to register your renderer

My 'hold_on_last_frame' animation isn't working!

This loop type is not supported in GeckoLib3, and was only added in GeckoLib4.

To make your functionality work, you should make a second animation that is just a single keyframe of the position you want your animation to stay in, then set it to loop. Then, in your AnimationBuilder, set that looped animation to follow the first one.

This will cause the animation to play the first animation, then hold onto it until you tell it to stop

Table of Contents

Geckolib 3
Geckolib 4

Hosted By: Cloudsmith

Package repository hosting is graciously provided by Cloudsmith.

Cloudsmith is the only fully hosted, cloud-native, universal package management solution that enables your organization to create, store and share packages in any format, to any place, with total confidence.

Clone this wiki locally