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 [2023/06/03 17:57] – fixed typos terratutorial:blockentity [2024/06/13 08:56] (current) – update outdated read nbt method s1lverposting
Line 19: Line 19:
 Please ensure that the constructor only takes the two parameters, otherwise the method reference ''DemoBlockEntity::new'' that we write later will be invalid. The ''ExampleMod.DEMO_BLOCK_ENTITY'' field will be created later. Please ensure that the constructor only takes the two parameters, otherwise the method reference ''DemoBlockEntity::new'' that we write later will be invalid. The ''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 =====
  
Line 31: Line 30:
         Registries.BLOCK_ENTITY_TYPE,         Registries.BLOCK_ENTITY_TYPE,
         new Identifier("tutorial", "demo_block_entity"),         new Identifier("tutorial", "demo_block_entity"),
-        FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build()+        BlockEntityType.Builder.create(DemoBlockEntity::new, DEMO_BLOCK).build()
     );     );
 </code> </code>
Line 73: Line 72:
     // Serialize the BlockEntity     // Serialize the BlockEntity
     @Override     @Override
-    public void writeNbt(NbtCompound nbt) {+    public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapper) {
         // Save the current value of the number to the nbt         // Save the current value of the number to the nbt
         nbt.putInt("number", number);         nbt.putInt("number", number);
  
-        super.writeNbt(nbt);+        super.writeNbt(nbt, wrapper);
     }     }
 } }
Line 87: Line 86:
 // Deserialize the BlockEntity // Deserialize the BlockEntity
 @Override @Override
-public void readNbt(NbtCompound nbt) { +public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapper) { 
-    super.readNbt(nbt);+    super.readNbt(nbt, wrapper);
          
     number = nbt.getInt("number");     number = nbt.getInt("number");
tutorial/blockentity.txt · Last modified: 2024/06/13 08:56 by s1lverposting