Skip to content

Geckolib Blocks (Geckolib3)

Tslat edited this page Jun 19, 2023 · 13 revisions

Pre-word

Because blocks in Minecraft are static objects, it is not possible to outright animate a block without it also having a BlockEntity/TileEntity. This is because GeckoLib leverages the BlockEntityRenderer system to render animated blocks.

To make a GeckoLib-animated block, you will need to ensure your block has an associated BlockEntity/TileEntity. You can find find information on doing so here for Forge and Fabric

Your BlockEntity should then implement IAnimatable, not the block itself.

Steps

Creating a GeckoLib block requires the following steps:

  1. Creating your Blockbench Model
  2. Creating your Geo Model
  3. Creating your block and associated blockentity classes
  4. Disabling block rendering for your block
  5. 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, #4, and #5

Full Video Guides

The Block Class

This guide won't guide you through creating and registering a block specifically. If you need instruction on creating a block, seek a tutorial that does so.

Once you have your block class, ensure you have it marked as a blockentity by implementing EntityBlock, or any of its subclasses. Then, in the newBlockEntity method that you'll be told to override, return a new instance of your BlockEntity

Disabling the Vanilla Renderer

By default, Minecraft will search for a blockstate file in your assets folder to figure out how to render your block. Since the GeckoLib renderer doesn't render based on a blockstate, we need to tell the game not to bother looking.

To do this, we need to override the getRenderShape method in our block class, and return the ENTITYBLOCK_ANIMATED type E.G.

    @Override
    public RenderShape getRenderShape(BlockState state) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }

The BlockEntity 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 blockentity 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 blockentity's class.

The first is to implement IAnimatable on your blockentity 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 blockentity as an animatable blockentity.

Next, we'll create an instance of an AnimationFactory for our blockentity. 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 blockentity class, caching it in a final variable.

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

And finally, override registerControllers in your blockentity class. This method is called when your blockentity 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 BlockEntity Class

public class ExampleBlockEntity extends BlockEntity implements IAnimatable {
        protected static final AnimationBuilder DEPLOY = new AnimationBuilder().addAnimation("misc.deploy", true).addAnimation("misc.idle", true);

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

        public ExampleEntity(BlockPos pos, BlockState state) {
            super(MyBlockEntities.EXAMPLE_BLOCK_ENTITY.get(), pos, state);
        }
    
        @Override
        public void registerControllers(final AnimationData data) {
            data.addAnimationController(new AnimationController<>(this, "Deployment", 0, this::deployAnimController));
        }
        
        protected <E extends ExampleEntity> PlayState deployAnimController(final AnimationEvent<E> event) {
            event.getController().setAnimation(DEPLOY_ANIM);

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

The Renderer

To have your blockentity show up in the world, you'll need to register your its renderer, similarly to how you might register one for an entity.

To do this, we first create an instance of a class that extends GeoBlockRenderer. The renderer will take the render context and your Geo Model instance.

Example BlockEntity Renderer

    public class ExampleBlockEntityRenderer extends GeoBlockRenderer<ExampleBlockEntity> {
        public ExampleBlockEntityRenderer(EntityRendererProvider.Context context) {
            super(new ExampleBlockEntityModel());
        }
    }

Then we register the renderer with Minecraft so it knows how to render it.

Forge Example
    @SubscribeEvent
    public static void registerRenderers(final EntityRenderersEvent.RegisterRenderers event) {
        event.registerBlockEntityRenderer(MyBlockEntities.EXAMPLE_BLOCK_ENTITY.get(), ExampleBlockEntityRenderer::new);
    }
Fabric/Quilt Example
    @Override
    public void onInitializeClient() {
        BlockEntityRendererRegistry.register(MyBlockEntities.EXAMPLE_BLOCK_ENTITY, ExampleBlockEntityRenderer::new);
    }

Block Directionality

GeckoLib handles block rotations automatically, so if your block is directional the GeckoLib renderer will turn it automatically. However, if you need to do something special with rotations or disable them completely, override rotateBlock in GeoBlockRenderer

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