tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockentity [2024/08/26 01:09] – solidblock | tutorial:blockentity [2025/04/01 12:20] (current) – [Block entity ticking] solidblock | ||
---|---|---|---|
Line 10: | Line 10: | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
public DemoBlockEntity(BlockPos pos, BlockState state) { | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
- | super(TutorialBlockEntities.DEMO_BLOCK_ENTITY, pos, state); | + | super(TutorialBlockEntityTypes.DEMO_BLOCK, pos, state); |
} | } | ||
} | } | ||
</ | </ | ||
- | Please ensure that the constructor only takes the two parameters, otherwise the method reference '' | + | Please ensure that the constructor only takes the two parameters, otherwise the method reference '' |
Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, providing inventories, | Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, providing inventories, | ||
Line 21: | Line 21: | ||
===== Registering block and block entities ===== | ===== Registering block and block entities ===== | ||
- | Once you have created the '' | + | Once you have created the '' |
- | The '' | + | The '' |
<code java TutorialBlocks.java> | <code java TutorialBlocks.java> | ||
public final class TutorialBlocks { | public final class TutorialBlocks { | ||
- | public static final DemoBlock DEMO_BLOCK = register( | + | |
- | | + | |
- | | + | // For versions before 1.21.2 |
- | ); | + | // public static final DemoBlock DEMO_BLOCK = register(" |
+ | | ||
+ | // For version 1.21.2 and later | ||
+ | public static final DemoBlock DEMO_BLOCK = register(" | ||
| | ||
[...] | [...] | ||
Line 36: | Line 39: | ||
</ | </ | ||
- | <code java TutorialBlockEntities.java> | + | <code java TutorialBlockEntityTypes.java> |
- | public class TutorialBlockEntities | + | public class TutorialBlockEntityTypes |
public static <T extends BlockEntityType<?>> | public static <T extends BlockEntityType<?>> | ||
return Registry.register(Registries.BLOCK_ENTITY_TYPE, | return Registry.register(Registries.BLOCK_ENTITY_TYPE, | ||
} | } | ||
- | public static final BlockEntityType< | + | public static final BlockEntityType< |
" | " | ||
- | BlockEntityType.Builder.create(DemoBlockEntity:: | + | |
+ | FabricBlockEntityTypeBuilder.create(DemoBlockEntity:: | ||
); | ); | ||
| | ||
Line 62: | Line 66: | ||
[...] | [...] | ||
| | ||
- | | + | |
} | } | ||
} | } | ||
Line 69: | Line 73: | ||
For old versions, if you cannot access '' | For old versions, if you cannot access '' | ||
- | The block entity type defines that only the '' | + | The block entity type defines that only the '' |
- | ==== Connecting | + | > **Note:** Like other blocks, |
- | Once your '' | + | ===== Connecting the block entity and the block ===== |
+ | |||
+ | Once your '' | ||
<code java DemoBlock.java> | <code java DemoBlock.java> | ||
Line 89: | Line 95: | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new DemoBlockEntity(pos, | return new DemoBlockEntity(pos, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected BlockRenderType getRenderType(BlockState state) { | ||
+ | return BlockRenderType.MODEL; | ||
} | } | ||
} | } | ||
</ | </ | ||
- | To serialize data for bloc kentities, see [[block_entity_modifying_data]]. | + | Overriding '' |
- | ===== Block Entity Ticking | + | ===== Block entity ticking |
Ticking means the block should run something on every tick (which is 1/20 second). For your block to tick, you would normally use '' | Ticking means the block should run something on every tick (which is 1/20 second). For your block to tick, you would normally use '' | ||
Line 112: | Line 123: | ||
And in your '' | And in your '' | ||
<code java DemoBlockEntity.java> | <code java DemoBlockEntity.java> | ||
- | public class DemoBlockEntity extends BlockEntity { | + | public class DemoBlockEntity extends BlockEntity |
[...] | [...] | ||
| | ||
Line 122: | Line 133: | ||
</ | </ | ||
- | ===== Overview | + | ===== Next steps ===== |
+ | |||
+ | You should now have your very own '' | ||
- | You should now have your very own '' | + | You also learned how to add ticking for it. Next step, you can try some other complex operations for the block entities, such as: |
+ | * [[blockentity_modify_data|Modifying block entity data]] | ||
+ | * [[inventory|Storing items in the block entity as an inventory]] | ||
+ | * [[blockentityrenderers|Using block entity renderers | ||
+ | * [[screenhandler|Creating a container block]] |
tutorial/blockentity.1724634570.txt.gz · Last modified: 2024/08/26 01:09 by solidblock