tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockentity [2021/07/22 15:43] – Normalized indentation and added RenderType fireblast | tutorial:blockentity [2023/09/20 19:18] (current) – [Creating a BlockEntity] Change outdated interface notes to generic statement haykam | ||
---|---|---|---|
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 '' | + | |
+ | Block entities support a variety of methods to enable functionality such as serialization to and deserialization from NBT, ticking, providing inventories, | ||
===== 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, | |
- | @Override | + | new Identifier(" |
- | public void onInitialize() { | + | |
- | | + | |
- | } | + | |
</ | </ | ||
+ | |||
+ | The block entity type defines that only the '' | ||
==== Connecting a Block Entity to a Block ==== | ==== Connecting a Block Entity to a Block ==== | ||
Line 56: | Line 56: | ||
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 72: | ||
// Serialize the BlockEntity | // Serialize the BlockEntity | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
+ | nbt.putInt(" | ||
- | | + | |
- | tag.putInt(" | + | |
- | + | ||
- | return tag; | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | 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 113: | Line 132: | ||
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.1626968589.txt.gz · Last modified: 2021/07/22 15:43 by fireblast