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:11] – [Creating a BlockEntity] solidblocktutorial:blockentity [2022/12/16 02:14] – [Registering your BlockEntity] solidblock
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 final BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY = Registry.register(     public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY = Registry.register(
-        Registry.BLOCK_ENTITY_TYPE,+        Registries.BLOCK_ENTITY_TYPE,
         new Identifier("tutorial", "demo_block_entity"),         new Identifier("tutorial", "demo_block_entity"),
         FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build()         FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build()
Line 35: Line 35:
 </code> </code>
  
-The block entity types defines that only the ''DEMO_BLOCK'' can have this block entity type. If you want it to support more blocks, just add them in the parameters of ''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 ''FabricBlockEntityTypeBuilder.create''. If the method reference ''DemoBlockEntity::new'' does not parse, check if the constructor of ''DemoBlockEntity'' has the correct parameters.
  
 ==== Connecting a Block Entity to a Block ==== ==== Connecting a Block Entity to a Block ====
Line 57: 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.
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam