Skip to content

Commit 87c2422

Browse files
committed
example from #415
1 parent 4f93e09 commit 87c2422

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

develop/blocks/block-containers.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ In this tutorial we'll create a block that uses its container to duplicate any i
1313

1414
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`.
1515

16-
@[code transcludeWith=:::block](@/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java)
16+
<<< @/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java#block
1717

1818
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.
1919

2020
::: details Show `ImplementedContainer`
2121

22-
@[code](@/reference/latest/src/main/java/com/example/docs/container/ImplementedContainer.java)
22+
<<< @/reference/latest/src/main/java/com/example/docs/container/ImplementedContainer.java
2323

2424
:::
2525

26-
@[code transcludeWith=:::be](@/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java)
26+
<<< @/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java#be
2727

2828
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.
2929

@@ -33,15 +33,15 @@ Don't forget to register the block and block entity in their respective classes!
3333

3434
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.
3535

36-
@[code transcludeWith=:::save](@/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java)
36+
<<< @/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java#save
3737

3838
## Interacting with the Container {#interacting-with-the-container}
3939

4040
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.
4141

4242
To do that, we need to override the `useItemOn` method in the `DuplicatorBlock`:
4343

44-
@[code transcludeWith=:::useon](@/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java)
44+
<<< @/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java#useon
4545

4646
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`.
4747

@@ -55,7 +55,7 @@ Let's now make it so that the block duplicates the stack you threw in it, but on
5555

5656
To do this, we'll add a `tick` function to the `DuplicatorBlockEntity`, and a field to store how much we've been waiting:
5757

58-
@[code transcludeWith=:::tick](@/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java)
58+
<<< @/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java#tick
5959

6060
The `DuplicatorBlock` should now have a `getTicker` method that returns a reference to `DuplicatorBlockEntity::tick`.
6161

@@ -73,13 +73,13 @@ To create this behavior, we need to implement the `WorldlyContainer` interface i
7373

7474
Let's modify the `DuplicatorBlockEntity` to only accept items from the top:
7575

76-
@[code transcludeWith=:::accept](@/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java)
76+
<<< @/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java#accept
7777

7878
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.
7979

8080
Also, we should modify the `useItemOn` method of the `DuplicatorBlock` to actually respect the new behavior:
8181

82-
@[code transcludeWith=:::place](@/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java)
82+
<<< @/reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java#place
8383

8484
Now, if we try to insert items from the side instead of the top, it won't work!
8585

reference/latest/src/main/java/com/example/docs/block/custom/DuplicatorBlock.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import com.example.docs.block.entity.ModBlockEntities;
2121
import com.example.docs.block.entity.custom.DuplicatorBlockEntity;
2222

23-
// :::block
23+
// #region block
2424
public class DuplicatorBlock extends BaseEntityBlock {
25-
// :::block
25+
// #endregion block
2626

2727
public DuplicatorBlock(Properties settings) {
2828
super(settings);
@@ -33,37 +33,37 @@ protected MapCodec<? extends BaseEntityBlock> codec() {
3333
return simpleCodec(DuplicatorBlock::new);
3434
}
3535

36-
// :::block
36+
// #region block
3737
@Nullable
3838
@Override
3939
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
4040
return new DuplicatorBlockEntity(pos, state);
4141
}
4242

43-
// :::block
43+
// #endregion block
4444

4545
@Override
4646
protected RenderShape getRenderShape(BlockState state) {
4747
return RenderShape.MODEL;
4848
}
4949

50-
// :::useon
50+
// #region useon
5151
@Override
5252
protected InteractionResult useItemOn(ItemStack stack, BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
5353
if (!(world.getBlockEntity(pos) instanceof DuplicatorBlockEntity duplicatorBlockEntity)) {
5454
return InteractionResult.PASS;
5555
}
5656

57-
// :::useon
57+
// #endregion useon
5858

59-
// :::place
59+
// #region place
6060
if (!duplicatorBlockEntity.canPlaceItemThroughFace(0, stack, hit.getDirection())) {
6161
return InteractionResult.PASS;
6262
}
6363

64-
// :::place
64+
// #endregion place
6565

66-
// :::useon
66+
// #region useon
6767
if (!player.getItemInHand(hand).isEmpty() && duplicatorBlockEntity.isEmpty()) {
6868
duplicatorBlockEntity.setItem(0, player.getItemInHand(hand).copy());
6969
player.getItemInHand(hand).setCount(0);
@@ -72,15 +72,15 @@ protected InteractionResult useItemOn(ItemStack stack, BlockState state, Level w
7272
return InteractionResult.SUCCESS;
7373
}
7474

75-
// :::useon
75+
// #endregion useon
7676

7777
@Nullable
7878
@Override
7979
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, BlockState state, BlockEntityType<T> type) {
8080
return createTickerHelper(type, ModBlockEntities.DUPLICATOR_BLOCK_ENTITY, DuplicatorBlockEntity::tick);
8181
}
8282

83-
// :::block
83+
// #region block
8484
// ...
8585
}
86-
// :::block
86+
// #endregion block

reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121
/*
2222
The following is a dummy piece of code to not have `implements WorldlyContainer` in the first code block where we implement `ImplementedContainer`.
2323
lmk if you have a better idea on how to handle this.
24-
// :::be
24+
// #region be
2525
public class DuplicatorBlockEntity extends BlockEntity implements ImplementedContainer {
26-
// :::be
26+
// #endregion be
2727
*/
2828

2929
public class DuplicatorBlockEntity extends BlockEntity implements ImplementedContainer, WorldlyContainer {
30-
// :::be
30+
// #region be
3131

3232
private final NonNullList<ItemStack> items = NonNullList.withSize(1, ItemStack.EMPTY);
3333

34-
// :::tick
34+
// #region tick
3535
private int timeSinceDropped = 0;
3636

37-
// :::tick
37+
// #endregion tick
3838

3939
public DuplicatorBlockEntity(BlockPos pos, BlockState state) {
4040
super(ModBlockEntities.DUPLICATOR_BLOCK_ENTITY, pos, state);
@@ -45,9 +45,9 @@ public NonNullList<ItemStack> getItems() {
4545
return items;
4646
}
4747

48-
// :::be
48+
// #endregion be
4949

50-
// :::save
50+
// #region save
5151
@Override
5252
protected void loadAdditional(ValueInput input) {
5353
super.loadAdditional(input);
@@ -60,9 +60,9 @@ protected void saveAdditional(ValueOutput output) {
6060
super.saveAdditional(output);
6161
}
6262

63-
// :::save
63+
// #endregion save
6464

65-
// :::tick
65+
// #region tick
6666
public static void tick(Level world, BlockPos blockPos, BlockState blockState, DuplicatorBlockEntity duplicatorBlockEntity) {
6767
if (duplicatorBlockEntity.isEmpty()) return;
6868
duplicatorBlockEntity.timeSinceDropped++;
@@ -76,9 +76,9 @@ public static void tick(Level world, BlockPos blockPos, BlockState blockState, D
7676
Block.popResourceFromFace(world, blockPos, Direction.UP, duplicate);
7777
}
7878

79-
// :::tick
79+
// #endregion tick
8080

81-
// :::accept
81+
// #region accept
8282
@Override
8383
public int[] getSlotsForFace(Direction side) {
8484
return new int[]{ 0 };
@@ -94,8 +94,8 @@ public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction dir)
9494
return true;
9595
}
9696

97-
// :::accept
97+
// #endregion accept
9898

99-
// :::be
99+
// #region be
100100
}
101-
// :::be
101+
// #endregion be

0 commit comments

Comments
 (0)