tutorial:containers
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:containers [2020/02/27 13:31] – new tutorial mkpoli | tutorial:containers [2020/02/27 13:46] – typo mkpoli | ||
---|---|---|---|
Line 3: | Line 3: | ||
==== Block and BlockItem ==== | ==== Block and BlockItem ==== | ||
- | First create and register | + | First we need to create |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class BiggerChestBlock extends BlockWithEntity { | public class BiggerChestBlock extends BlockWithEntity { | ||
public BiggerChestBlock(Settings settings) { | public BiggerChestBlock(Settings settings) { | ||
Line 65: | Line 65: | ||
Then we need to register our Block and BlockItem. | Then we need to register our Block and BlockItem. | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer | public class ExampleMod implements ModInitializer | ||
{ | { | ||
Line 86: | Line 86: | ||
==== BlockEntity ==== | ==== BlockEntity ==== | ||
- | We then need to add an '' | + | BlockEntities are used for managing container inventories. Actually, it implements Inventory interface. |
- | <code java> | + | < |
- | | + | public class BiggerChestBlockEntity extends LootableContainerBlockEntity { |
+ | | ||
+ | private static final int INVENTORY_SIZE = 54; // 9 * 6 = 54 | ||
+ | |||
+ | public BiggerChestBlockEntity() { | ||
+ | super(ExampleMod.BIGGER_CHEST_ENTITY_TYPE); | ||
+ | this.inventory = DefaultedList.ofSize(INVENTORY_SIZE, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected Text getContainerName() { | ||
+ | return new TranslatableText(" | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected Container createContainer(int syncId, PlayerInventory playerInventory) { | ||
+ | return new BiggerChestContainer(syncId, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected DefaultedList< | ||
+ | return this.inventory; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void setInvStackList(DefaultedList< | ||
+ | this.inventory = list; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public int getInvSize() { | ||
+ | return INVENTORY_SIZE; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void fromTag(CompoundTag tag) { | ||
+ | super.fromTag(tag); | ||
+ | this.inventory = DefaultedList.ofSize(this.getInvSize(), | ||
+ | if (!this.deserializeLootTable(tag)) { | ||
+ | Inventories.fromTag(tag, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public CompoundTag toTag(CompoundTag tag) { | ||
+ | super.toTag(tag); | ||
+ | if (!this.serializeLootTable(tag)) { | ||
+ | Inventories.toTag(tag, | ||
+ | } | ||
+ | return tag; | ||
+ | } | ||
+ | } | ||
</ | </ | ||
Line 215: | Line 266: | ||
==== Orgnizing ==== | ==== Orgnizing ==== | ||
- | After following | + | After all the steps, you should have your ExampleMod Class and ExampleClientMod Class as such: |
- | + | ||
- | You should have your ExampleMod Class and ExampleClientMod Class as such: | + | |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 246: | Line 295: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | {{tutorial: | ||
It's over and enjoy your custom container! | It's over and enjoy your custom container! |
tutorial/containers.txt · Last modified: 2022/05/27 15:57 by solidblock