User Tools

Site Tools


tutorial:blockentity

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:blockentity [2024/08/26 01:35] solidblocktutorial:blockentity [2025/04/01 12:20] (current) – [Block entity ticking] solidblock
Line 29: Line 29:
     [...]     [...]
          
-    public static final DemoBlock DEMO_BLOCK = register("demo_block", new DemoBlock(AbstractBlock.Settings.create()));+    // For versions before 1.21.2 
 +    // public static final DemoBlock DEMO_BLOCK = register("demo_block", new DemoBlock(AbstractBlock.Settings.create())); 
 +     
 +    // For version 1.21.2 and later 
 +    public static final DemoBlock DEMO_BLOCK = register("demo_block", DemoBlock::new, AbstractBlock.Settings.create());
          
     [...]     [...]
Line 43: Line 47:
   public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK = register(   public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK = register(
       "demo_block",       "demo_block",
-      BlockEntityType.Builder.create(DemoBlockEntity::new, TutorialBlocks.DEMO_BLOCK).build()+      // For versions before 1.21.2, please use BlockEntityType.Builder
 +      FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, TutorialBlocks.DEMO_BLOCK).build()
   );   );
      
Line 68: Line 73:
 For old versions, if you cannot access ''BlockEntityType.Builder.create'', try ''FabricBlockEntityTypeBuilder.create''. For old versions, if you cannot access ''BlockEntityType.Builder.create'', try ''FabricBlockEntityTypeBuilder.create''.
  
-The block entity type defines that only the ''TutorialBlocks.DEMO_BLOCK'' can have this block entity type. If you want the block entity type to support more blocks, just add them in the parameters of ''BlockEntityType.Builder.create''. If the method reference ''DemoBlockEntity::new'' does not parse, check if the constructor of ''DemoBlockEntity'' has the correct parameters.+The block entity type defines that only the ''TutorialBlocks.DEMO_BLOCK'' can have this block entity type. If you want the block entity type to support more blocks, just add them in the parameters of ''FabricBlockEntityTypeBuilder.create''. If the method reference ''DemoBlockEntity::new'' does not parse, check if the constructor of ''DemoBlockEntity'' has the correct parameters.
  
-==== Connecting the block entity and the block ====+> **Note:** Like other blocks, the block also needs a block model and an item model, and may also need a loot table, see [[blocks]] for how to create them. As for loot tables, [[blockentity_sync_itemstack|subsequent tutorials]] will cover how to improve the loot tables to include block entity data.
  
-Once your ''BlockEntityType'' has been created and registered, you'll need a block that is associated with it. You can do this by extending ''BlockWithEntity'' (or implementing ''BlockEntityProvider'') and overriding ''createBlockEntity''. Each time your block is placed, your Block Entity will spawn alongside it.+===== Connecting the block entity and the block ===== 
 + 
 +Once your ''BlockEntityType'' has been created and registered, you'll need a block that is associated with it. You can do this by extending ''BlockWithEntity'' (or implementing ''BlockEntityProvider'') and overriding ''createBlockEntity''. Each time your block is placed, your block entity will be created.
  
 <code java DemoBlock.java> <code java DemoBlock.java>
Line 88: Line 95:
     public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {     public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
         return new DemoBlockEntity(pos, state);         return new DemoBlockEntity(pos, state);
 +    }
 +
 +    @Override
 +    protected BlockRenderType getRenderType(BlockState state) {
 +        return BlockRenderType.MODEL;
     }     }
 } }
 </code> </code>
 +
 +Overriding ''getRenderType'' is because ''BlockWithEntity'' makes it invisible by default.
  
 ===== Block entity ticking ===== ===== Block entity ticking =====
Line 109: Line 123:
 And in your ''BlockEntity'' class: And in your ''BlockEntity'' class:
 <code java DemoBlockEntity.java> <code java DemoBlockEntity.java>
-public class DemoBlockEntity extends BlockEntity {+public class DemoBlockEntity extends BlockEntity implements BlockEntityTicker<DemoBlockEntity> {
     [...]     [...]
          
tutorial/blockentity.1724636119.txt.gz · Last modified: 2024/08/26 01:35 by solidblock