tutorial:blockentity_modify_data
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Last revisionBoth sides next revision | ||
block_modifying_blockentity [2023/06/18 09:43] – changed title terra | tutorial:blockentity_modify_data [2023/06/18 13:24] – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Modify BlockEntity data ====== | ||
- | ===== Introduction ===== | ||
- | |||
- | Having a '' | ||
- | |||
- | Before proceeding, you will need a [[tutorial: | ||
- | |||
- | <code java> | ||
- | public class DemoBlockEntity extends BlockEntity { | ||
- | |||
- | public int number = 0; | ||
- | |||
- | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
- | super(ExampleMod.DEMO_BLOCK_ENTITY, | ||
- | } | ||
- | |||
- | @Override | ||
- | public void writeNbt(NbtCompound nbt) { | ||
- | nbt.putInt(" | ||
- | |||
- | super.writeNbt(nbt); | ||
- | } | ||
- | | ||
- | @Override | ||
- | public void readNbt(NbtCompound nbt) { | ||
- | super.readNbt(nbt); | ||
- | |||
- | number = nbt.getInt(" | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Make sure the '' | ||
- | |||
- | ===== From Block' | ||
- | |||
- | This gets the '' | ||
- | |||
- | <code java> | ||
- | public class DemoBlock extends Block implements BlockEntityProvider { | ||
- | |||
- | [...] | ||
- | |||
- | @Override | ||
- | public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
- | if (!world.isClient){ | ||
- | BlockEntity blockEntity = world.getBlockEntity(pos); | ||
- | if (blockEntity instanceof DemoBlockEntity){ | ||
- | DemoBlockEntity demoBlockEntity = (DemoBlockEntity) blockEntity; | ||
- | demoBlockEntity.number++; | ||
- | player.sendMessage(Text.literal(" | ||
- | | ||
- | return ActionResult.SUCCESS; | ||
- | } | ||
- | } | ||
- | | ||
- | return ActionResult.PASS; | ||
- | } | ||
- | } | ||
- | </ |
tutorial/blockentity_modify_data.txt · Last modified: 2023/06/18 13:24 by terra