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 [2019/07/22 17:37] – override -> implement, remove superfluous information fudgetutorial:blockentity [2020/11/29 12:11] – [Serializing Data] Add BlockState arg boogiemonster1o1
Line 17: Line 17:
 </code> </code>
  
-Bellow will show you how to create the ''ExampleMod.DEMO_BLOCK_ENTITY'' field.+Below will show you how to create the ''ExampleMod.DEMO_BLOCK_ENTITY'' field.
  
 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.
  
-===== Registring 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. ''modid:demo'' should be replaced by your Mod ID and the name you want your ''BlockEntity'' to be registered under. 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. ''modid:demo'' should be replaced by your Mod ID and the name you want your ''BlockEntity'' to be registered under.
Line 32: Line 32:
 @Override @Override
 public void onInitialize() { public void onInitialize() {
-   DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY, "modid:demo", BlockEntityType.Builder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null));+   DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, "modid:demo", BlockEntityType.Builder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null));
 } }
 </code> </code>
  
-Once your ''BlockEntityType'' has been created and registered as seen above, you can simply implement ''BlockEntityProvider'' in your ''Block'' class:+==== Connecting a Block Entity to a Block ==== 
 + 
 +Once your ''BlockEntityType'' has been created and registered, you'll need a block that is associated with it. You can do this by implementing ''BlockEntityProvider'' and overriding ''createBlockEntity''. Each time your block is placed, your Block Entity will spawn alongside it.
  
 <code java> <code java>
-@Override +public class MyBlock extends Block implements BlockEntityProvider { 
-public BlockEntity createBlockEntity(BlockView blockView) { + 
-   return new DemoBlockEntity();+   [...] 
 + 
 +   @Override 
 +   public BlockEntity createBlockEntity(BlockView blockView) { 
 +      return new DemoBlockEntity(); 
 +   }
 } }
 </code> </code>
Line 62: Line 69:
        
    // Serialize the BlockEntity    // Serialize the BlockEntity
 +   @Override
    public CompoundTag toTag(CompoundTag tag) {    public CompoundTag toTag(CompoundTag tag) {
       super.toTag(tag);       super.toTag(tag);
Line 77: Line 85:
 <code java> <code java>
 // Deserialize the BlockEntity // Deserialize the BlockEntity
-public void fromTag(CompoundTag tag) {+@Override 
 +public void fromTag(BlockState state, CompoundTag tag) {
    super.fromTag(tag);    super.fromTag(tag);
    number = tag.getInt("number");    number = tag.getInt("number");
Line 85: Line 94:
 Once you have implemented the ''toTag'' and ''fromTag'' 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 ''toTag'' 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 ''toTag'' and ''fromTag'' 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 ''toTag'' 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.
  
-If you need to sync some of your ''BlockEntity'' data to the client, for purposes such as rendering, you should implement ''BlockEntityClientSerializablle'' from the Fabric API. This class provides the ''fromClientTag'' and ''toClientTag'' methods, which work much the same as the previously discussed ''fromTag'' and ''toTag'' methods, except that they are used specifically for sending to and receiving data on the client. +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 ''fromTag'' and ''toTag'' methods, except that they are used specifically for sending to and receiving data on the client. 
  
 ===== 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''. Finally, you learned how to save data to your ''BlockEntity'', and how to retrieve for use later.
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam