The Block Class
To have a GeckoLib block, you first need to have a block class.
note
This tutorial is specifically for GeckoLib implementation. For information on general-modding block features, seek a tutorial or community support.
Creating the class
💻Additive Code Section
All code snippets in this section add onto the previous code snippets, using a step-by-step approach to build the complete code.
Green highlights show added or modified lines of code for each step.
Green highlights show added or modified lines of code for each step.
First, we create the base block class and implement the base constructor:
public class ExampleBlock extends Block {
public ExampleBlock(BlockBehaviour.Properties properties) {
super(properties);
}
}
Then we implement the EntityBlock interface:
public class ExampleBlock extends Block implements EntityBlock {
public ExampleBlock(BlockBehaviour.Properties properties) {
super(properties);
}
}
Lastly, we extend newBlockEntity to return a new instance of our custom block entity:
- Fabric
- Forge
- NeoForge
public class ExampleBlock extends Block implements EntityBlock {
public ExampleBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return BlockEntityRegistry.EXAMPLE_BLOCK_ENTITY.create(pos, state);
}
}
public class ExampleBlock extends Block implements EntityBlock {
public ExampleBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return BlockEntityRegistry.EXAMPLE_BLOCK_ENTITY.get().create(pos, state);
}
}
public class ExampleBlock extends Block implements EntityBlock {
public ExampleBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return BlockEntityRegistry.EXAMPLE_BLOCK_ENTITY.get().create(pos, state);
}
}
And that's it!
Now we move onto the BlockEntity class.