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 revisionBoth sides next revision
tutorial:blockentity [2019/05/27 19:09] modmuss50tutorial:blockentity [2019/06/29 23:49] – spelling fixes draylar
Line 19: Line 19:
 Bellow will show you how to create the ''ExampleMod.DEMO_BLOCK_ENTITY'' field. Bellow 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 funcionality. ''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 seperate tutorial dedicated entierly 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 ===== ===== Registring your BlockEntity =====
Line 49: Line 49:
 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 ''toTag()'' and ''fromTag()'' 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 ''toTag()'' and ''fromTag()''
  
-''toTag()'' returns a ''CompoundTag'', which should contain all of the data in your ''BlockEntity''. This data is saved to the diskand also send through packets if you need to sync your ''BlockEntity'' data with clients. It is very important to call the default implementation of ''toTag'', 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 retrive the data later.+''toTag()'' returns a ''CompoundTag'', 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 ''toTag'', 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.
  
 <code java> <code java>
Line 73: Line 73:
 </code> </code>
  
-In order to retrieve the data later, you will also need to override ''fromTag''. This method is the opposite of ''toTag'' - instead of saving your data to a ''CompoundTag'', you are given the tag which you saved earlier, enabling you to retrieve any data that you need. As with ''toTag'', it is essential that you call ''super.fromTag'', and you will need to use the same keys to retrieve data that you saved. To retrive, the number we saved earlier, see the example below.+In order to retrieve the data later, you will also need to override ''fromTag''. This method is the opposite of ''toTag'' - instead of saving your data to a ''CompoundTag'', you are given the tag which you saved earlier, enabling you to retrieve any data that you need. As with ''toTag'', it is essential that you call ''super.fromTag'', 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.
  
 <code java> <code java>
Line 89: Line 89:
 ===== 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 retrive 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