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/01/30 19:44] – Update Serializing NBT to 1.18 outercloudstudio | 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 '' |
- | If you need to sync some of your '' | + | ===== Sync data from server |
+ | The data is read in the server world usually. Most data are not needed by the client, for example, your client does not need to know what's in the chest or furnace, until you open the GUI. But for some block entities, | ||
+ | |||
+ | For version 1.17.1 and below, | ||
+ | |||
+ | For version 1.18 and above, override '' | ||
+ | <code java> | ||
+ | @Nullable | ||
+ | @Override | ||
+ | public Packet< | ||
+ | return BlockEntityUpdateS2CPacket.create(this); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public NbtCompound toInitialChunkDataNbt() { | ||
+ | return createNbt(); | ||
+ | } | ||
+ | </ | ||
+ | **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 112: | 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