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:24] – Fixed Ticker and added the section to Overview 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 11: | Line 11: | ||
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | public DemoBlockEntity(BlockPos pos, BlockState state) { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY, | + | super(ExampleMod.DEMO_BLOCK_ENTITY, |
- | | + | } |
} | } | ||
</ | </ | ||
- | 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() { | + | |
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | + | |
- | } | + | |
</ | </ | ||
+ | |||
+ | The block entity type defines that only the '' | ||
==== Connecting a Block Entity to a Block ==== | ==== Connecting a Block Entity to a Block ==== | ||
Line 43: | Line 43: | ||
public class DemoBlock extends Block implements BlockEntityProvider { | public class DemoBlock extends Block implements BlockEntityProvider { | ||
- | [...] | + | |
- | @Override | + | |
- | | + | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { |
- | return new DemoBlockEntity(pos, | + | return new DemoBlockEntity(pos, |
- | | + | } |
} | } | ||
</ | </ | ||
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> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | // Store the current value of the number | + | |
- | | + | private int number = 7; |
- | public DemoBlockEntity(BlockPos pos, BlockState state) { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY, | + | super(ExampleMod.DEMO_BLOCK_ENTITY, |
- | | + | } |
- | // Serialize the BlockEntity | + | |
- | | + | @Override |
- | public | + | public |
- | | + | // Save the current value of the number to the nbt |
- | + | nbt.putInt(" | |
- | | + | |
- | tag.putInt(" | + | |
- | + | } | |
- | | + | |
- | | + | |
} | } | ||
</ | </ | ||
- | 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(nbt); |
- | | + | |
+ | | ||
} | } | ||
</ | </ | ||
- | 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 ``Tickable`` interface. For your block to tick, you would normally use '' | + | 1.17 has added static ticking, where before you'd implement the '' |
Line 103: | Line 122: | ||
<code java> | <code java> | ||
public class DemoBlock extends BlockWithEntity { | public class DemoBlock extends BlockWithEntity { | ||
- | [...] | + | |
- | | + | @Override |
- | | + | |
- | | + | // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! |
- | | + | return BlockRenderType.MODEL; |
+ | } | ||
+ | @Override | ||
+ | | ||
+ | return checkType(type, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | And in your '' | + | And in your '' |
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | public DemoBlockEntity(BlockPos pos, BlockState state) { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY, | + | super(ExampleMod.DEMO_BLOCK_ENTITY, |
- | | + | } |
- | | + | public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity be) { |
- | [...] | + | [...] |
- | | + | } |
} | } | ||
</ | </ |
tutorial/blockentity.1626967449.txt.gz · Last modified: 2021/07/22 15:24 by fireblast