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 16:34] – adjusment 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 84: | Line 84: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | You may refer to other tutorials to modify the appearance and other properties of the Block by adding models or adjusting rendering later. | ||
==== BlockEntity ==== | ==== BlockEntity ==== | ||
- | We then need to add an '' | + | BlockEntity is used for managing container inventories. Actually, it implements Inventory interface. It is required |
- | <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 201: | Line 254: | ||
[...] | [...] | ||
ContainerProviderRegistry.INSTANCE.registerFactory(BIGGER_CHEST, | ContainerProviderRegistry.INSTANCE.registerFactory(BIGGER_CHEST, | ||
- | final BlockEntity blockEntity | + | final World world = player.world; |
- | return((BiggerChestBlockEntity) blockEntity).createContainer(syncId, player.inventory); | + | final BlockPos pos = buf.readBlockPos(); |
+ | return | ||
}); | }); | ||
} | } | ||
Line 214: | Line 268: | ||
</ | </ | ||
- | ==== Orgnizing | + | ==== Organizing |
- | 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 298: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | {{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