tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockentity [2019/07/22 17:37] – override -> implement, remove superfluous information fudge | tutorial:blockentity [2021/06/11 02:18] – Fix ffrann | ||
---|---|---|---|
Line 11: | Line 11: | ||
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); |
} | } | ||
} | } | ||
</ | </ | ||
- | Bellow | + | Below will show you how to create the '' |
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 '' | ||
- | ===== Registring | + | ===== Registering |
- | Once you have created the '' | + | Once you have created the '' |
The '' | The '' | ||
Line 32: | Line 32: | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | | + | |
} | } | ||
</ | </ | ||
- | Once your '' | + | ==== Connecting a Block Entity to a Block ==== |
+ | |||
+ | Once your '' | ||
<code java> | <code java> | ||
- | @Override | + | public class DemoBlock extends Block implements BlockEntityProvider { |
- | public BlockEntity createBlockEntity(BlockView blockView) { | + | |
- | | + | |
+ | |||
+ | @Override | ||
+ | | ||
+ | return new DemoBlockEntity(pos, state); | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 47: | Line 54: | ||
===== Serializing Data ===== | ===== Serializing Data ===== | ||
- | If you want to store any data in your '' | + | If you want to store any data in your '' |
- | '' | + | '' |
<code java> | <code java> | ||
Line 57: | Line 64: | ||
| | ||
- | | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); |
} | } | ||
// Serialize the BlockEntity | // Serialize the BlockEntity | ||
- | | + | |
- | super.toTag(tag); | + | |
+ | super.writeNbt(tag); | ||
| | ||
// Save the current value of the number to the tag | // Save the current value of the number to the tag | ||
Line 73: | Line 81: | ||
</ | </ | ||
- | In order to retrieve the data later, you will also need to override '' | + | In order to retrieve the data later, you will also need to override '' |
<code java> | <code java> | ||
// Deserialize the BlockEntity | // Deserialize the BlockEntity | ||
- | public void fromTag(CompoundTag | + | @Override |
- | | + | public void readNbt(NbtCompound |
+ | | ||
| | ||
} | } | ||
</ | </ | ||
- | Once you have implemented the '' | + | Once you have implemented the '' |
- | If you need to sync some of your '' | + | If you need to sync some of your '' |
===== Overview ===== | ===== Overview ===== | ||
You should now have your very own '' | You should now have your very own '' |
tutorial/blockentity.txt · Last modified: 2023/09/20 19:18 by haykam