User Tools

Site Tools


tutorial:containers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:containers [2020/02/27 13:38] – [Orgnizing] Add image mkpolitutorial:containers [2020/02/27 16:34] – adjusment mkpoli
Line 3: Line 3:
  
 ==== Block and BlockItem ==== ==== Block and BlockItem ====
-First we need to create Block and register it as well as its BlockItem.+First we need to create the Block and register it as well as its BlockItem.
  
 <code java [enable_line_numbers="true"] BiggerChestBlock.java> <code java [enable_line_numbers="true"] BiggerChestBlock.java>
Line 84: Line 84:
 } }
 </code> </code>
 +
 +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 ''onUse'' method in our Block class in order to open our chest.+BlockEntity is used for managing container inventories. Actually, it implements Inventory interface. It is required to save and load the inventory.
  
-<code java> +<code java [enable_line_numbers="true"] BiggerChestBlockEntity.java> 
-    +public class BiggerChestBlockEntity extends LootableContainerBlockEntity { 
 +    private DefaultedList<ItemStack> inventory; 
 +    private static final int INVENTORY_SIZE = 54; // 9 * 6 = 54 
 + 
 +    public BiggerChestBlockEntity() { 
 +        super(ExampleMod.BIGGER_CHEST_ENTITY_TYPE); 
 +        this.inventory = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY); 
 +    } 
 + 
 +    @Override 
 +    protected Text getContainerName() { 
 +        return new TranslatableText("container.chest"); 
 +    } 
 + 
 +    @Override 
 +    protected Container createContainer(int syncId, PlayerInventory playerInventory) { 
 +        return new BiggerChestContainer(syncId, playerInventory, (Inventory) this); 
 +    } 
 + 
 +    @Override 
 +    protected DefaultedList<ItemStack> getInvStackList() { 
 +        return this.inventory; 
 +    } 
 + 
 +    @Override 
 +    protected void setInvStackList(DefaultedList<ItemStack> list) { 
 +        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(), ItemStack.EMPTY); 
 +        if (!this.deserializeLootTable(tag)) { 
 +            Inventories.fromTag(tag, this.inventory); 
 +        } 
 +    } 
 + 
 +    @Override 
 +    public CompoundTag toTag(CompoundTag tag) { 
 +        super.toTag(tag); 
 +        if (!this.serializeLootTable(tag)) { 
 +            Inventories.toTag(tag, this.inventory); 
 +        } 
 +        return tag; 
 +    } 
 +}
 </code> </code>
  
Line 201: Line 254:
         [...]         [...]
         ContainerProviderRegistry.INSTANCE.registerFactory(BIGGER_CHEST, (syncId, identifier, player, buf) -> {         ContainerProviderRegistry.INSTANCE.registerFactory(BIGGER_CHEST, (syncId, identifier, player, buf) -> {
-            final BlockEntity blockEntity = player.world.getBlockEntity(buf.readBlockPos()); +            final World world = player.world
-            return((BiggerChestBlockEntityblockEntity).createContainer(syncId, player.inventory);+            final BlockPos pos = buf.readBlockPos(); 
 +            return world.getBlockState(pos).createContainerFactory(player.world, pos).createMenu(syncId, player.inventory, player);
         });         });
     }     }
Line 214: Line 268:
 </code> </code>
  
-==== 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="true"] ExampleMod.java> <code java [enable_line_numbers="true"] ExampleMod.java>
tutorial/containers.txt · Last modified: 2022/05/27 15:57 by solidblock