Skip to content

Commit 976a41e

Browse files
committed
fix: transclusions from #415
1 parent a646b3f commit 976a41e

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
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,53 +33,53 @@ 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);
7070
}
7171

7272
return InteractionResult.SUCCESS;
7373
}
74-
// :::useon
74+
// #endregion useon
7575

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

82-
// :::block
82+
// #region block
8383
// ...
8484
}
85-
// :::block
85+
// #endregion block

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

Lines changed: 16 additions & 16 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
private final NonNullList<ItemStack> items = NonNullList.withSize(1, ItemStack.EMPTY);
32-
// :::be
33-
// :::tick
32+
// #endregion be
33+
// #region tick
3434
private int timeSinceDropped = 0;
3535

36-
// :::tick
37-
// :::be
36+
// #endregion tick
37+
// #region be
3838

3939
public DuplicatorBlockEntity(BlockPos pos, BlockState state) {
4040
super(ModBlockEntities.DUPLICATOR_BLOCK_ENTITY, pos, state);
@@ -44,9 +44,9 @@ public DuplicatorBlockEntity(BlockPos pos, BlockState state) {
4444
public NonNullList<ItemStack> getItems() {
4545
return items;
4646
}
47-
// :::be
47+
// #endregion be
4848

49-
// :::save
49+
// #region save
5050
@Override
5151
protected void loadAdditional(ValueInput input) {
5252
super.loadAdditional(input);
@@ -58,9 +58,9 @@ protected void saveAdditional(ValueOutput output) {
5858
ContainerHelper.saveAllItems(output, items);
5959
super.saveAdditional(output);
6060
}
61-
// :::save
61+
// #endregion save
6262

63-
// :::tick
63+
// #region tick
6464
public static void tick(Level world, BlockPos blockPos, BlockState blockState, DuplicatorBlockEntity duplicatorBlockEntity) {
6565
if (duplicatorBlockEntity.isEmpty()) return;
6666
duplicatorBlockEntity.timeSinceDropped++;
@@ -73,9 +73,9 @@ public static void tick(Level world, BlockPos blockPos, BlockState blockState, D
7373
Block.popResourceFromFace(world, blockPos, Direction.UP, duplicate);
7474
Block.popResourceFromFace(world, blockPos, Direction.UP, duplicate);
7575
}
76-
// :::tick
76+
// #endregion tick
7777

78-
// :::accept
78+
// #region accept
7979
@Override
8080
public int[] getSlotsForFace(Direction side) {
8181
return new int[]{ 0 };
@@ -90,7 +90,7 @@ public boolean canPlaceItemThroughFace(int slot, ItemStack stack, @Nullable Dire
9090
public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction dir) {
9191
return true;
9292
}
93-
// :::accept
94-
// :::be
93+
// #endregion accept
94+
// #region be
9595
}
96-
// :::be
96+
// #endregion be

0 commit comments

Comments
 (0)