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 [2021/06/11 02:18] – Fix ffranntutorial:blockentity [2023/09/20 19:18] (current) – [Creating a BlockEntity] Change outdated interface notes to generic statement haykam
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 11: Line 11:
 <code java> <code java>
 public class DemoBlockEntity extends BlockEntity { public class DemoBlockEntity extends BlockEntity {
-   public DemoBlockEntity(BlockPos pos, BlockState state) { +    public DemoBlockEntity(BlockPos pos, BlockState state) { 
-      super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); +        super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); 
-   }+    }
 } }
 </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 invalidThe ''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.+
  
 +Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, ticking, providing inventories, and more. This tutorial covers the most common implementations of block entity functionality.
 ===== 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, 
-@Override +        new Identifier("tutorial", "demo_block_entity"), 
-public void onInitialize() { +        FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build() 
-   DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, "tutorial:demo_block_entity", FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null)); +    );
-}+
 </code> </code>
 +
 +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 43: Line 43:
 public class DemoBlock extends Block implements BlockEntityProvider { public class DemoBlock extends Block implements BlockEntityProvider {
  
-   [...]+    [...]
  
-   @Override +    @Override 
-   public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { +    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { 
-      return new DemoBlockEntity(pos, state); +        return new DemoBlockEntity(pos, state); 
-   }+    }
 } }
 </code> </code>
Line 56: Line 56:
 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()'' returns a ''NBTCompound'', which should contain all of the data in your ''BlockEntity''This data is saved to the disk and also send through packets if you need to sync your ''BlockEntity'' data with clients. It is very important to call the default implementation of ''writeNbt'', as it saves "Identifying Data" (position and ID) to the tag. 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 tag. 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 tag, and you will need to remember the key in order to retrieve the data later.+''writeNbt()'' modifies the parameter ''nbt'', which should contain all of the data in your block entityIt usually does not modify the block entity object itself. The NBT is saved to the diskand 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.
  
 <code java> <code java>
 public class DemoBlockEntity extends BlockEntity { public class DemoBlockEntity extends BlockEntity {
  
-   // Store the current value of the number +    // Store the current value of the number 
-   private int number = 7;+    private int number = 7;
        
-   public DemoBlockEntity(BlockPos pos, BlockState state) { +    public DemoBlockEntity(BlockPos pos, BlockState state) { 
-      super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); +        super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); 
-   }+    }
        
-   // Serialize the BlockEntity +    // Serialize the BlockEntity 
-   @Override +    @Override 
-    public NbtCompound writeNbt(NbtCompound tag) { +    public void writeNbt(NbtCompound nbt) { 
-      super.writeNbt(tag); +        // Save the current value of the number to the nbt 
-       +        nbt.putInt("number", number); 
-      // Save the current value of the number to the tag + 
-      tag.putInt("number", number); +        super.writeNbt(nbt)
-       +    }
-      return tag+
-   }+
 } }
 </code> </code>
  
-In order to retrieve the data later, you will also need to override ''readNbt''. This method is the opposite of ''writeNbt'' - instead of saving your data to a ''NBTCompound'', you are given the tag which you saved earlier, enabling you to retrieve any data that you need. As with ''writeNbt'', it is essential that you call ''super.readNbt'', and you will need to use the same keys to retrieve data that you saved. To retrieve, the number we saved earlier, see the example below.+In order to read the data, you will also need to override ''readNbt''. This method is the opposite of ''writeNbt'' - instead of saving your data to a ''NBTCompound'', you are given the nbt data which you saved earlier, enabling you to retrieve any data that you need. It modifies the block entity object itself, instead of the ''nbt''. As with ''writeNbt'', it is essential that you call ''super.readNbt'', and you will need to use the same keys to retrieve data that you saved. To read, the number we saved earlier in the nbt, see the example below.
  
 <code java> <code java>
 // Deserialize the BlockEntity // Deserialize the BlockEntity
 @Override @Override
-public void readNbt(NbtCompound tag) { +public void readNbt(NbtCompound nbt) { 
-   super.readNbt(tag); +    super.readNbt(nbt); 
-   number = tag.getInt("number");+     
 +    number = nbt.getInt("number");
 } }
 </code> </code>
  
-Once you have implemented the ''writeNbt'' and ''readNbt'' methods, you simply need to ensure that they are called at the right time. Whenever your ''BlockEntity'' data changes 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 change any custom variable in your ''BlockEntity'' class.+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.
  
-If you need to sync some of your ''BlockEntity'' data to the client, for purposes such as rendering, you should 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. +===== 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 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.18 and above, override ''toUpdatePacket'' and ''toInitialChunkDataNbt'': 
 +<code java> 
 +  @Nullable 
 +  @Override 
 +  public Packet<ClientPlayPacketListener> toUpdatePacket() { 
 +    return BlockEntityUpdateS2CPacket.create(this); 
 +  } 
 + 
 +  @Override 
 +  public NbtCompound toInitialChunkDataNbt() { 
 +    return createNbt(); 
 +  } 
 +</code> 
 +**Warning**: Need to call ''world.updateListeners(pos, state, state, Block.NOTIFY_LISTENERS);'' to trigger the update, otherwise the client does not know that the block entity has been changed. 
 + 
 +===== 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 ''BlockEntity''. See below for the common implementation of ticking.  
 + 
 + 
 +In your ''Block'' class: 
 +<code java> 
 +public class DemoBlock extends BlockWithEntity { 
 +    [...] 
 +    @Override 
 +    public BlockRenderType getRenderType(BlockState state) { 
 +        // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! 
 +        return BlockRenderType.MODEL; 
 +    } 
 +    @Override 
 +    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) { 
 +        return checkType(type, ExampleMod.DEMO_BLOCK_ENTITY, (world1, pos, state1, be) -> DemoBlockEntity.tick(world1, pos, state1, be)); 
 +    } 
 +
 +</code> 
 +And in your ''BlockEntity'' class: 
 +<code java> 
 +public class DemoBlockEntity extends BlockEntity { 
 +    public DemoBlockEntity(BlockPos pos, BlockState state) { 
 +        super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); 
 +    } 
 +    public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity be) { 
 +        [...] 
 +    } 
 +
 +</code>
  
 ===== Overview ===== ===== Overview =====
  
-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 implemented ''BlockEntityProvider'' in your ''Block'' class, and used the interface to provide an instance of your new ''BlockEntity''Finally, you learned how to save data to your ''BlockEntity'', and how to retrieve for use later.+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 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.
tutorial/blockentity.1623377897.txt.gz · Last modified: 2021/06/11 02:18 by ffrann