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:09] solidblocktutorial: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);
     }     }
 } }
 </code> </code>
  
-Please ensure that the constructor only takes the two parameters, otherwise the method reference ''DemoBlockEntity::new'' that we write later will be invalid. The ''TutorialBlockEntities.DEMO_BLOCK_ENTITY'' field will be created later.+Please ensure that the constructor only takes the two parameters, otherwise the method reference ''DemoBlockEntity::new'' that we write later will be invalid. The ''TutorialBlockEntityTypes.DEMO_BLOCK'' field will be created later.
  
 Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, providing inventories, and more. This tutorial covers the most common implementations of block entity functionality. Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, providing inventories, and more. This tutorial covers the most common implementations of block entity functionality.
Line 21: Line 21:
 ===== Registering block and block entities ===== ===== Registering block and block entities =====
  
-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'', which links your ''Block'' and ''BlockEntity'' together. Create a ''Block'' as the static final field ''DEMO_BLOCK'', and create the matching ''BlockEntityType'' with the line below. 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>
 public final class TutorialBlocks { public final class TutorialBlocks {
-    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 36: Line 39:
 </code> </code>
  
-<code java TutorialBlockEntities.java> +<code java TutorialBlockEntityTypes.java> 
-public class TutorialBlockEntities {+public class TutorialBlockEntityTypes {
   public static <T extends BlockEntityType<?>> T register(String path, T blockEntityType) {   public static <T extends BlockEntityType<?>> T register(String path, T blockEntityType) {
     return Registry.register(Registries.BLOCK_ENTITY_TYPE, Identifier.of("tutorial", path), blockEntityType);     return Registry.register(Registries.BLOCK_ENTITY_TYPE, Identifier.of("tutorial", path), blockEntityType);
   }   }
  
-  public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY = 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 62: Line 66:
         [...]         [...]
                  
-        TutorialBlockEntities.initialize();+        TutorialBlockEntityTypes.initialize();
     }     }
 } }
Line 69: 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 ''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 89: 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 112: 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 122: 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.1724634570.txt.gz · Last modified: 2024/08/26 01:09 by solidblock