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
Next revisionBoth sides next revision
tutorial:blockentity [2022/08/08 04:02] – [Sync data from server to client] solidblocktutorial:blockentity [2022/12/16 02:14] – [Registering your BlockEntity] solidblock
Line 17: Line 17:
 </code> </code>
  
-Below will show you how to create the ''ExampleMod.DEMO_BLOCK_ENTITY'' field.+Please ensure that the constructor only takes the two parameters, otherwise the method reference ''DemoBlockEntity::new'' that we write later will be invalid. The ''ExampleMod.DEMO_BLOCK_ENTITY'' field will be created later.
  
 You can simply add variables to this barebone class or implement interfaces such as ''Tickable'' and ''Inventory'' to add more functionality. ''Tickable'' provides a single ''tick()'' method, which is called once per tick for every loaded instance of your Block in the world., while ''Inventory'' allows your BlockEntity to interact with automation such as hoppers - there will likely be a separate tutorial dedicated entirely to this interface later. You can simply add variables to this barebone class or implement interfaces such as ''Tickable'' and ''Inventory'' to add more functionality. ''Tickable'' provides a single ''tick()'' method, which is called once per tick for every loaded instance of your Block in the world., while ''Inventory'' allows your BlockEntity to interact with automation such as hoppers - there will likely be a separate tutorial dedicated entirely to this interface later.
Line 23: Line 23:
 ===== Registering your BlockEntity ===== ===== Registering your BlockEntity =====
  
-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. Assuming your ''Block'' has been created and saved to a local variable ''DEMO_BLOCK'', you would create the matching ''BlockEntityType'' with the line below. In this tutorial, the ID of the block entity is ''tutorial:demo_block_entity''.+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. Assuming your ''Block'' has been created and saved to the static final field ''DEMO_BLOCK'', you would create the matching ''BlockEntityType'' with the line below. In this tutorial, the ID of the block entity is ''tutorial:demo_block_entity''.
  
-The ''BlockEntityType'' should be registered in your ''onInitialize'' method, this is to ensure it gets registered at the correct time.+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.
  
 <code java> <code java>
-public static BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY;+    public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY = Registry.register( 
 +        Registries.BLOCK_ENTITY_TYPE, 
 +        new Identifier("tutorial", "demo_block_entity"), 
 +        FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build() 
 +    ); 
 +</code>
  
-@Override +The block entity type defines that only the ''DEMO_BLOCK'' can have this block entity typeIf you want the block entity type to support more blocksjust add them in the parameters of ''FabricBlockEntityTypeBuilder.create''. If the method reference ''DemoBlockEntity::new'' does not parsecheck if the constructor of ''DemoBlockEntity'' has the correct parameters.
-public void onInitialize() { +
-    DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, "tutorial:demo_block_entity", FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null)); +
-+
-</code>+
  
 ==== Connecting a Block Entity to a Block ==== ==== Connecting a Block Entity to a Block ====
Line 56: Line 57:
 If you want to store any data in your ''BlockEntity'', you will need to save and load it, or it will only be held while the ''BlockEntity'' is loaded, and the data will reset whenever you come back to it. Luckily, saving and loading is quite simple - you only need to override ''writeNbt()'' and ''readNbt()'' If you want to store any data in your ''BlockEntity'', you will need to save and load it, or it will only be held while the ''BlockEntity'' is loaded, and the data will reset whenever you come back to it. Luckily, saving and loading is quite simple - you only need to override ''writeNbt()'' and ''readNbt()''
  
-''writeNbt()'' modifies the parameter ''nbt'', which should contain all of the data in your block entity. It usually does not modify the block entiti object itself. The NBT is saved to the disk, and also send through packets if you need to sync your block entity data with clients. It is very important to call ''super.writeNbt'', which saves the position and id of the block entity to the nbt. Without this, any further data you try and save will be lost as it is not associated with a position and ''BlockEntityType''.+''writeNbt()'' modifies the parameter ''nbt'', which should contain all of the data in your block entity. It usually does not modify the block entiti object itself. The NBT is saved to the disk, and if you need to sync your block entity data with clients, also sent through packets. It is very important to call ''super.writeNbt'', which saves the position and id of the block entity to the nbt. Without this, any further data you try and save will be lost as it is not associated with a position and ''BlockEntityType''.
  
 Knowing this, the example below demonstrates saving an integer from your ''BlockEntity'' to the nbt. In the example, the integer is saved under the key ''"number"'' - you can replace this with any string, but you can only have one entry for each key in your nbt, and you will need to remember the key in order to read the data later. Knowing this, the example below demonstrates saving an integer from your ''BlockEntity'' to the nbt. In the example, the integer is saved under the key ''"number"'' - you can replace this with any string, but you can only have one entry for each key in your nbt, and you will need to remember the key in order to read the data later.
Line 116: Line 117:
  
 ===== Block Entity Ticking ===== ===== Block Entity Ticking =====
-1.17 has added static ticking, where before you'd implement the ''Tickable'' interface. For your block to tick, you would normally use ''getTicker'' in ''Block'', linking back to a ''Block Entity''. See below for the common implementation of ticking. +1.17 has added static ticking, where before you'd implement the ''Tickable'' interface. For your block to tick, you would normally use ''getTicker'' in ''Block'', linking back to a ''BlockEntity''. See below for the common implementation of ticking. 
  
  
Line 132: Line 133:
         return checkType(type, ExampleMod.DEMO_BLOCK_ENTITY, (world1, pos, state1, be) -> DemoBlockEntity.tick(world1, pos, state1, be));         return checkType(type, ExampleMod.DEMO_BLOCK_ENTITY, (world1, pos, state1, be) -> DemoBlockEntity.tick(world1, pos, state1, be));
     }     }
 +}
 </code> </code>
-And in your ''Block Entity'':+And in your ''BlockEntity'' class:
 <code java> <code java>
 public class DemoBlockEntity extends BlockEntity { public class DemoBlockEntity extends BlockEntity {
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam