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:16] solidblocktutorial:blockentity [2025/04/01 12:20] (current) – [Block entity ticking] solidblock
Line 23: Line 23:
 Once you have created the ''BlockEntity'' class, you will need to register it for it to function. The first step of this process is to create a ''BlockEntityType'' object in our ''TutorialBlockEntityTypes'' class, which links your ''Block'' and ''BlockEntity'' together. Create a ''Block'' object as the static final field ''DEMO_BLOCK'' in the ''TutorialBlocks'' class we created earlier. In this tutorial, the ID of the block entity is ''tutorial:demo_block''. Once you have created the ''BlockEntity'' class, you will need to register it for it to function. The first step of this process is to create a ''BlockEntityType'' object in our ''TutorialBlockEntityTypes'' class, which links your ''Block'' and ''BlockEntity'' together. Create a ''Block'' object as the static final field ''DEMO_BLOCK'' in the ''TutorialBlocks'' class we created earlier. In this tutorial, the ID of the block entity is ''tutorial:demo_block''.
  
-The ''BlockEntityType'' can be registered in the initialization of class or in your ''onInitialize'' method. This is to ensure it gets registered at the correct time. In this example, we register them in separate classes (see [[bocks]]).+The ''BlockEntityType'' can be registered in the initialization of class or in your ''onInitialize'' method. This is to ensure it gets registered at the correct time. In this example, we register them in separate classes (see [[blocks]]).
  
 <code java TutorialBlocks.java> <code java TutorialBlocks.java>
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>
  
-To serialize data for bloc kentities, see [[block_entity_modifying_data]].+Overriding ''getRenderType'' is because ''BlockWithEntity'' makes it invisible by default.
  
-===== 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 ''getTicker'' in ''Block'', linking back to a static ''tick'' method in the ''BlockEntity''. See below for the common implementation of 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 ''getTicker'' in ''Block'', linking back to a static ''tick'' method in the ''BlockEntity''. See below for the common implementation of ticking. 
  
Line 111: 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> {
     [...]     [...]
          
Line 121: Line 133:
 </code> </code>
  
-===== Overview =====+===== Next steps ===== 
 + 
 +You should now have your very own ''BlockEntity'', which you can expand in various ways to suit your needs. You registered a ''BlockEntityType'', and used it to connect your ''Block'' and ''BlockEntity'' classes together. Then, you extended ''BlockWithEntity'', and used its interface ''BlockEntityProvider'' to provide an instance of your new ''BlockEntity''.
  
-You should now have your very own ''BlockEntity'', which you can expand in various ways to suit your needs. You registered a ''BlockEntityType'', and used it to connect your ''Block'' and ''BlockEntity'' classes togetherThen, you implemented ''BlockEntityProvider'' in your ''Block'' class, and used the interface to provide an instance of your new ''BlockEntity''. You also learned how to save data to your ''BlockEntity'', how to retrieve for use later, and finally, you learned how to add ticking to it.+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 to dynamically render]] 
 +  * [[screenhandler|Creating a container block]]
tutorial/blockentity.1724634969.txt.gz · Last modified: 2024/08/26 01:16 by solidblock