tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockentity [2022/05/26 09:21] – Add a prompt to actively trigger BlockEntity synchronization moheng | tutorial:blockentity [2024/10/27 14:21] (current) – [Registering block and block entities] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Adding a BlockEntity ====== | ====== Adding a BlockEntity ====== | ||
- | ===== Introduction ===== | + | A **block entity** is primarily used to store data within blocks. Before creating one, you will need a [[blocks|Block]]. This tutorial will cover the creation of your BlockEntity class, and its registration. |
- | A BlockEntity is primarily used to store data within blocks. Before creating one, you will need a [[tutorial: | + | ===== Creating |
- | ===== Creating a BlockEntity | + | The simplest block entity simply extends '' |
- | The simplest Block Entity simply extends '' | + | < |
- | + | ||
- | <code java> | + | |
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
public DemoBlockEntity(BlockPos pos, BlockState state) { | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
- | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); | + | super(TutorialBlockEntityTypes.DEMO_BLOCK, pos, state); |
} | } | ||
} | } | ||
</ | </ | ||
- | 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 | + | Block entities support a variety of methods |
- | ===== Registering | + | ===== Registering |
- | Once you have created the '' | + | Once you have created the '' |
- | The '' | + | The '' |
- | <code java> | + | < |
- | public | + | public |
- | + | [...] | |
- | @Override | + | |
- | public void onInitialize() { | + | |
- | | + | |
+ | [...] | ||
} | } | ||
</ | </ | ||
- | ==== Connecting a Block Entity to a Block ==== | + | <code java TutorialBlockEntityTypes.java> |
+ | public class TutorialBlockEntityTypes { | ||
+ | public static <T extends BlockEntityType<?>> | ||
+ | return Registry.register(Registries.BLOCK_ENTITY_TYPE, | ||
+ | } | ||
- | Once your '' | + | public static final BlockEntityType< |
+ | " | ||
+ | // For versions 1.21.2 | ||
+ | // replace `BlockEntityType.Builder` | ||
+ | BlockEntityType.Builder.create(DemoBlockEntity:: | ||
+ | ); | ||
+ | |||
+ | public static void initialize() { | ||
+ | } | ||
+ | } | ||
- | < | + | </code> |
- | public class DemoBlock extends Block implements BlockEntityProvider { | + | |
+ | Remember to refer to the '' | ||
+ | <code java ExampleMod.java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
[...] | [...] | ||
+ | | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
+ | |||
+ | TutorialBlockEntityTypes.initialize(); | ||
} | } | ||
} | } | ||
</ | </ | ||
- | ===== Serializing Data ===== | + | For old versions, if you cannot access '' |
- | If you want to store any data in your '' | + | The block entity type defines that only the '' |
- | '' | + | > **Note:** Like other blocks, the block also needs a block model and an item model, and may also need a loot table, see [[blocks]] for how to create them. As for loot tables, [[blockentity_sync_itemstack|subsequent tutorials]] |
- | <code java> | + | ===== Connecting the block entity and the block ===== |
- | public class DemoBlockEntity extends BlockEntity { | + | |
- | // Store the current value of the number | + | Once your '' |
- | | + | |
- | | + | <code java DemoBlock.java> |
- | public | + | public class DemoBlock extends BlockWithEntity { |
- | super(ExampleMod.DEMO_BLOCK_ENTITY, | + | public |
+ | super(settings); | ||
} | } | ||
- | + | ||
- | // Serialize the BlockEntity | + | |
@Override | @Override | ||
- | | + | |
- | | + | |
- | tag.putInt(" | + | } |
- | super.writeNbt(tag); | + | @Override |
+ | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
+ | return new DemoBlockEntity(pos, | ||
} | } | ||
- | } | ||
- | </ | ||
- | In order to retrieve the data later, you will also need to override '' | + | |
- | + | | |
- | <code java> | + | |
- | // Deserialize the BlockEntity | + | |
- | @Override | + | |
- | public void readNbt(NbtCompound tag) { | + | |
- | super.readNbt(tag); | + | |
- | | + | |
- | number = tag.getInt(" | + | |
} | } | ||
</ | </ | ||
- | Once you have implemented the '' | + | Overriding |
- | + | ||
- | ===== Sync data from server to client ===== | + | |
- | The data is read in the server world usually. Sometimes you may have to sync all or some of the data to the client, for example, for renderering. | + | |
- | + | ||
- | 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 ===== | + | |
- | 1.17 has added static ticking, where before you'd implement the '' | + | |
+ | ===== Block entity ticking ===== | ||
+ | Ticking means the block should run something on every tick (which is 1/20 second). For your block to tick, you would normally use '' | ||
In your '' | In your '' | ||
- | <code java> | + | < |
public class DemoBlock extends BlockWithEntity { | public class DemoBlock extends BlockWithEntity { | ||
[...] | [...] | ||
- | | + | |
- | public BlockRenderType getRenderType(BlockState state) { | + | |
- | // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! | + | |
- | return BlockRenderType.MODEL; | + | |
- | } | + | |
@Override | @Override | ||
public <T extends BlockEntity> | public <T extends BlockEntity> | ||
- | return | + | |
+ | | ||
} | } | ||
+ | } | ||
</ | </ | ||
- | And in your '' | + | And in your '' |
- | <code java> | + | < |
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | | + | |
- | | + | |
- | | + | |
- | public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity | + | public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity |
[...] | [...] | ||
} | } | ||
Line 143: | Line 130: | ||
</ | </ | ||
- | ===== Overview | + | ===== Next steps ===== |
+ | |||
+ | You should now have your very own '' | ||
- | You should now have your very own '' | + | You also learned how to add ticking for it. Next step, you can try some other complex operations for the block entities, such as: |
+ | * [[blockentity_modify_data|Modifying block entity data]] | ||
+ | * [[inventory|Storing items in the block entity as an inventory]] | ||
+ | * [[blockentityrenderers|Using block entity renderers | ||
+ | * [[screenhandler|Creating a container block]] |
tutorial/blockentity.1653556910.txt.gz · Last modified: 2022/05/26 09:21 by moheng