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 [2020/05/07 19:15] – [Registring your BlockEntity] Fix typo in section header earthcomputertutorial:blockentity [2021/05/20 11:17] – [[tutorial:items]] mentioned that In this tutorial and all future ones, the “tutorial” namespace is used as a placeholder. 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. ''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. 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'' should be registered in your ''onInitialize'' method, this is to ensure it gets registered at the correct time.
Line 32: Line 32:
 @Override @Override
 public void onInitialize() { public void onInitialize() {
-   DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, "modid:demo", BlockEntityType.Builder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null));+   DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, "tutorial:demo_block_entity", BlockEntityType.Builder.create(DemoBlockEntity::new, DEMO_BLOCK).build(null));
 } }
 </code> </code>
Line 41: Line 41:
  
 <code java> <code java>
-public class MyBlock extends Block implements BlockEntityProvider {+public class DemoBlock extends Block implements BlockEntityProvider {
  
    [...]    [...]
Line 69: 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 84: Line 85:
 <code java> <code java>
 // Deserialize the BlockEntity // Deserialize the BlockEntity
-public void fromTag(CompoundTag tag) { +@Override 
-   super.fromTag(tag);+public void fromTag(BlockState state, CompoundTag tag) { 
 +   super.fromTag(state, tag);
    number = tag.getInt("number");    number = tag.getInt("number");
 } }
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam