-
-
Notifications
You must be signed in to change notification settings - Fork 131
Geckolib Entities (Geckolib4)
Creating a GeckoLib entity requires the following steps:
- Creating your Blockbench Model
- Creating your Geo Model
- Creating your entity class
- 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
Quick Summary
- implement
GeoEntity
- Override
getAnimatableInstanceCache
andregisterControllers
- Instantiate a new
AnimatableInstanceCache
viaGeckoLibUtil.createInstanceCache(this)
at the top of your entity class and return it ingetAnimatableInstanceCache
- 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 GeoEntity
on your entity class, and override the two methods your IDE will tell you to override.
This interface is the base of all animatable entities 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 AnimatableInstanceCache
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.createInstanceCache(this)
at the top of your entity class, caching it in a final variable.
Next, we'll override getAnimatableInstanceCache
in our entity class if it hasn't been done already, and return the cache 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
public class ExampleEntity extends PathfinderMob implements GeoEntity {
protected static final RawAnimation FLY_ANIM = RawAnimation.begin().thenLoop("move.fly");
private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);
public ExampleEntity(EntityType<? extends ExampleEntity> type, Level level) {
super(type, level);
}
@Override
public void registerControllers(final AnimatableManager.ControllerRegistrar controllers) {
controllers.add(new AnimationController<>(this, "Flying", 5, this::flyAnimController));
}
protected <E extends ExampleEntity> PlayState flyAnimController(final AnimationState<E> event) {
if (event.isMoving()
return event.setAndContinue(FLY_ANIM);
return PlayState.STOP;
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.geoCache;
}
}
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.
public class ExampleEntityRenderer extends GeoEntityRenderer<ExampleEntity> {
public ExampleEntityRenderer(EntityRendererProvider.Context context) {
super(context, new ExampleEntityModel());
}
}
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
You need to ensure you're either calling swing
, or using a goal/behaviour that calls swing
(such as MeleeAttackGoal) to tell your entity to swing its arm
Additionally, if you're not extending Monster
, you will need to override aiStep
in your entity and call updateSwingTime()
to ensure it's ticking its swing time
I'm using the DefaultAnimations#genericAttackAnimation controller, but it's only doing part of the animation before stopping!
If your animation is longer than the default swing time (6 ticks, including transition time), then your attack animation won't play fully before the entity stops swinging.
To fix this, you need to override getCurrentSwingDuration()
in your entity and return a more appropriate swing duration in ticks
Geckolib 3
Geckolib 4
- Installation
- Getting Started
- Upgrading from GeckoLib 3.1.x to 4.0
- Updating to GeckoLib 4.5
- Basic
- Advanced
- Miscellaneous
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.