tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:blockentity [2022/05/26 09:25] – correct a small mistake moheng | tutorial:blockentity [2023/06/03 17:57] – fixed typos terra | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | A BlockEntity is primarily used to store data within blocks. Before creating one, you will need a [[tutorial: | + | A BlockEntity is primarily used to store data within blocks. Before creating one, you will need a [[tutorial: |
===== Creating a BlockEntity ===== | ===== Creating a BlockEntity ===== | ||
Line 17: | Line 17: | ||
</ | </ | ||
- | Below will show you how to create | + | Please ensure that the constructor only takes the two parameters, otherwise the method reference '' |
You can simply add variables to this barebone class or implement interfaces such as '' | You can simply add variables to this barebone class or implement interfaces such as '' | ||
Line 23: | Line 23: | ||
===== Registering your BlockEntity ===== | ===== Registering your BlockEntity ===== | ||
- | Once you have created the '' | + | Once you have created the '' |
- | The '' | + | The '' |
<code java> | <code java> | ||
- | public static BlockEntityType< | + | |
+ | Registries.BLOCK_ENTITY_TYPE, | ||
+ | new Identifier(" | ||
+ | FabricBlockEntityTypeBuilder.create(DemoBlockEntity:: | ||
+ | ); | ||
+ | </ | ||
- | @Override | + | The block entity type defines that only the '' |
- | public void onInitialize() { | + | |
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | + | |
- | } | + | |
- | </ | + | |
==== Connecting a Block Entity to a Block ==== | ==== Connecting a Block Entity to a Block ==== | ||
Line 56: | Line 57: | ||
If you want to store any data in your '' | If you want to store any data in your '' | ||
- | '' | + | '' |
+ | |||
+ | Knowing this, the example below demonstrates saving an integer from your '' | ||
<code java> | <code java> | ||
Line 70: | Line 73: | ||
// Serialize the BlockEntity | // Serialize the BlockEntity | ||
@Override | @Override | ||
- | public void writeNbt(NbtCompound | + | public void writeNbt(NbtCompound |
- | // Save the current value of the number to the tag | + | // Save the current value of the number to the nbt |
- | | + | |
- | super.writeNbt(tag); | + | super.writeNbt(nbt); |
} | } | ||
} | } | ||
</ | </ | ||
- | In order to retrieve | + | In order to read the data, you will also need to override '' |
<code java> | <code java> | ||
// Deserialize the BlockEntity | // Deserialize the BlockEntity | ||
@Override | @Override | ||
- | public void readNbt(NbtCompound | + | public void readNbt(NbtCompound |
- | super.readNbt(tag); | + | super.readNbt(nbt); |
| | ||
- | number = tag.getInt(" | + | number = nbt.getInt(" |
} | } | ||
</ | </ | ||
- | Once you have implemented the '' | + | Once you have implemented the '' |
===== Sync data from server to client ===== | ===== Sync data from server to client ===== | ||
- | The data is read in the server world usually. | + | The data is read in the server world usually. |
- | For version 1.17.1 and below, | + | For version 1.17.1 and below, implement '' |
For version 1.18 and above, override '' | For version 1.18 and above, override '' | ||
Line 111: | Line 114: | ||
} | } | ||
</ | </ | ||
- | **Warning**: | + | **Warning**: |
===== Block Entity Ticking ===== | ===== Block Entity Ticking ===== | ||
- | 1.17 has added static ticking, where before you'd implement the '' | + | 1.17 has added static ticking, where before you'd implement the '' |
Line 130: | Line 133: | ||
return checkType(type, | return checkType(type, | ||
} | } | ||
+ | } | ||
</ | </ | ||
- | And in your '' | + | And in your '' |
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { |
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam