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
Last revisionBoth sides next revision
tutorial:blockentity [2022/12/16 02:14] – [Registering your BlockEntity] solidblocktutorial:blockentity [2023/06/03 17:57] – fixed typos terra
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-A BlockEntity is primarily used to store data within blocks. Before creating one, you will need a [[tutorial:blocks|Block]]. This tutorial will cover the creation of your BlockEntity class, and it'registration.+A BlockEntity is primarily used to store data within blocks. Before creating one, you will need a [[tutorial:blocks|Block]]. This tutorial will cover the creation of your BlockEntity class, and its registration.
  
 ===== Creating a BlockEntity ===== ===== Creating a BlockEntity =====
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 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''.+''writeNbt()'' modifies the parameter ''nbt'', which should contain all of the data in your block entity. It usually does not modify the block entity 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 94: Line 94:
 </code> </code>
  
-Once you have implemented the ''writeNbt'' and ''readNbt'' methods, you simply need to ensure that they are called when needed. Whenever your block entity is modified and needs to be saved, call ''markDirty()''. This will force the ''writeNbt'' method to be called when the world is next saved by marking the chunk which your block is in as dirty. As a general rule of thumb, simply call ''markDirty()'' whenever you modify any custom variable in your ''BlockEntity'' class, otherwise after you exit and re-enter the world, the block entity appears as if the modification had not been done.+Once you have implemented the ''writeNbt'' and ''readNbt'' methods, you simply need to ensure that they are called when needed. Whenever your block entity is modified and needs to be saved, call ''markDirty()''. This will force the ''writeNbt'' method to be called when the world is next saved by marking the chunk in which your block is as dirty. As a general rule of thumb, simply call ''markDirty()'' whenever you modify any custom variable in your ''BlockEntity'' class, otherwise after you exit and re-enter the world, the block entity appears as if the modification had not been done.
  
 ===== Sync data from server to client ===== ===== Sync data from server to client =====
-The data is read in the server world usually. Most data are not needed by the client, for example, your client does not need to know what's in the chest or furnace, until you open the GUI. But for some block entities, such as signs and banners, you have to inform the client of the data of the block entity, for example, for renderering.+The data is read in the server world usually. Most data are not needed by the client, for example, your client does not need to know what's in the chest or furnace, until you open the GUI. But for some block entities, such as signs and banners, you have to inform the client of the data of the block entity, for example, for rendering.
  
 For version 1.17.1 and below, implement ''BlockEntityClientSerializable'' from the Fabric API. This class provides the ''fromClientTag'' and ''toClientTag'' methods, which work much the same as the previously discussed ''readNbt'' and ''writeNbt'' methods, except that they are used specifically for sending to and receiving data on the client. You may simply call ''readNbt'' and ''writeNbt'' in the ''fromClientTag'' and ''toClientTag'' methods. For version 1.17.1 and below, implement ''BlockEntityClientSerializable'' from the Fabric API. This class provides the ''fromClientTag'' and ''toClientTag'' methods, which work much the same as the previously discussed ''readNbt'' and ''writeNbt'' methods, except that they are used specifically for sending to and receiving data on the client. You may simply call ''readNbt'' and ''writeNbt'' in the ''fromClientTag'' and ''toClientTag'' methods.
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam