Skip to content

Geckolib Blocks (Geckolib4)

Tslat edited this page Aug 16, 2024 · 4 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. 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. You can find find information on doing so here for Forge and Fabric

Your BlockEntity should then implement GeoBlockEntity, 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 GeoBlockEntity
  2. Override getAnimatableInstanceCache and registerControllers
  3. Instantiate a new AnimatableInstanceCache via GeckoLibUtil.createInstanceCache(this) at the top of your blockentity class and return it in getAnimatableInstanceCache
  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 GeoBlockEntity on your blockentity class, and override the two methods your IDE will tell you to override. This interface is the base for all animatable blockentities in GeckoLib, and lets the various other features of the mod pick up your blockentity as an animatable one.

Next, we'll create an instance of an AnimatableInstanceCache 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 cache via GeckoLibUtil.createInstanceCache(this) at the top of your blockentity class, caching it in a final variable.

Next, we'll override getAnimatableInstanceCache in our blockentity class if it hasn't been done already, and return the cache 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 GeoBlockEntity {
        protected static final RawAnimation DEPLOY_ANIM = RawAnimation.begin().thenPlayOnce("misc.deploy").thenLoop("misc.idle");

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

        public ExampleEntity(BlockPos pos, BlockState state) {
            super(MyBlockEntities.EXAMPLE_BLOCK_ENTITY.get(), pos, state);
        }
    
        @Override
        public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
            controllers.add(new AnimationController<>(this, this::deployAnimController));
        }
        
        protected <E extends ExampleEntity> PlayState deployAnimController(final AnimationState<E> state) {
            return state.setAndContinue(DEPLOY_ANIM);
        }
    
        @Override
        public AnimatableInstanceCache getAnimatableInstanceCache() {
            return this.cache;
        }
}

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