You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: develop/blocks/block-containers.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,17 +13,17 @@ In this tutorial we'll create a block that uses its container to duplicate any i
13
13
14
14
This should be familiar to the reader if they've followed the [Creating Your First Block](../blocks/first-block) and [Block Entities](../blocks/block-entities) guides. We'll create a `DuplicatorBlock` that extends `BaseEntityBlock` and implements `EntityBlock`.
Then, we need to create a `DuplicatorBlockEntity`, which needs to implement the `Container` interface. As most containers are generally expected to work the same way, you can copy and paste a helper called `ImplementedContainer` that does most of the work, leaving us with just a few methods to implement.
The `items` list is where the container's contents are stored. For this block we have it set to a size of 1 slot for the input.
29
29
@@ -33,15 +33,15 @@ Don't forget to register the block and block entity in their respective classes!
33
33
34
34
If we want the contents to persist between game reloads like a vanilla `BlockEntity`, we need to save it as NBT. Thankfully, Mojang provides a helper class called `ContainerHelper` with all the necessary logic.
## Interacting with the Container {#interacting-with-the-container}
39
39
40
40
Technically, the container is already functional. However, to insert items, we currently need to use hoppers. Let's make it so that we can insert items by right-clicking the block.
41
41
42
42
To do that, we need to override the `useItemOn` method in the `DuplicatorBlock`:
Here, if the player is holding an item and there is an empty slot, we move the item from the player's hand to the block's container and return `InteractionResult.SUCCESS`.
47
47
@@ -55,7 +55,7 @@ Let's now make it so that the block duplicates the stack you threw in it, but on
55
55
56
56
To do this, we'll add a `tick` function to the `DuplicatorBlockEntity`, and a field to store how much we've been waiting:
The `getSlotsForFace` returns an array of the slot _indices_ that can be interacted with from the given side. In this case, we only have a single slot (`0`), so we return an array with just that index.
79
79
80
80
Also, we should modify the `useItemOn` method of the `DuplicatorBlock` to actually respect the new behavior:
0 commit comments