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/10/27 14:21] – [Registering block and block entities] 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",
-      // For versions 1.21.2 and above, +      // For versions before 1.21.2, please use BlockEntityType.Builder. 
-      // replace `BlockEntityType.Builder` with `FabricBlockEntityTypeBuilder`+      FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, TutorialBlocks.DEMO_BLOCK).build()
-      BlockEntityType.Builder.create(DemoBlockEntity::new, TutorialBlocks.DEMO_BLOCK).build()+
   );   );
      
Line 70: 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.
  
 > **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. > **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.
Line 120: 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.1730038915.txt.gz · Last modified: 2024/10/27 14:21 by solidblock