BlockBasedTestInstance .java

net.minecraft.gametest.framework.BlockBasedTestInstance (class, 92 lines): members: typeDescription, run, codec, CODEC. imports: TestBlock, TestBlockMode.

net.minecraft.gametest.frameworkclass92 linescommonextends GameTestInstance3.4 KB

API Surface (3)

void run(final GameTestHelper helper)
MapCodec<BlockBasedTestInstance> codec()
MutableComponent typeDescription()

Fields & Constants (1)

CODEC

String literals (3)

"test_block.error.missing"
"test_block.error.too_many"
"test_instance.type.block_based"

Full Source

BlockBasedTestInstance.java92 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.gametest.framework;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.ArrayList;
6
import java.util.List;
7
import java.util.function.Consumer;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Holder;
10
import net.minecraft.network.chat.Component;
11
import net.minecraft.network.chat.MutableComponent;
12
import net.minecraft.world.level.block.Blocks;
13
import net.minecraft.world.level.block.TestBlock;
14
import net.minecraft.world.level.block.entity.TestBlockEntity;
15
import net.minecraft.world.level.block.state.BlockState;
16
import net.minecraft.world.level.block.state.properties.TestBlockMode;
17
18
public class BlockBasedTestInstance extends GameTestInstance {
19
	public static final MapCodec<BlockBasedTestInstance> CODEC = RecordCodecBuilder.mapCodec(
20
		i -> i.group(TestData.CODEC.forGetter(GameTestInstance::info)).apply(i, BlockBasedTestInstance::new)
21
	);
22
23
	public BlockBasedTestInstance(final TestData<Holder<TestEnvironmentDefinition<?>>> testData) {
24
		super(testData);
25
	}
26
27
	@Override
28
	public void run(final GameTestHelper helper) {
29
		BlockPos startPos = this.findStartBlock(helper);
30
		TestBlockEntity blockEntity = helper.getBlockEntity(startPos, TestBlockEntity.class);
31
		blockEntity.trigger();
32
		helper.onEachTick(() -> {
33
			List<BlockPos> acceptBlocks = this.findTestBlocks(helper, TestBlockMode.ACCEPT);
34
			if (acceptBlocks.isEmpty()) {
35
				helper.fail(Component.translatable("test_block.error.missing", TestBlockMode.ACCEPT.getDisplayName()));
36
			}
37
38
			boolean acceptTriggered = acceptBlocks.stream().map(pos -> helper.getBlockEntity(pos, TestBlockEntity.class)).anyMatch(TestBlockEntity::hasTriggered);
39
			if (acceptTriggered) {
40
				helper.succeed();
41
			} else {
42
				this.forAllTriggeredTestBlocks(helper, TestBlockMode.FAIL, failEntity -> helper.fail(Component.literal(failEntity.getMessage())));
43
				this.forAllTriggeredTestBlocks(helper, TestBlockMode.LOG, TestBlockEntity::trigger);
44
			}
45
		});
46
	}
47
48
	private void forAllTriggeredTestBlocks(final GameTestHelper helper, final TestBlockMode mode, final Consumer<TestBlockEntity> action) {
49
		for (BlockPos failBlock : this.findTestBlocks(helper, mode)) {
50
			TestBlockEntity blockEntity = helper.getBlockEntity(failBlock, TestBlockEntity.class);
51
			if (blockEntity.hasTriggered()) {
52
				action.accept(blockEntity);
53
				blockEntity.reset();
54
			}
55
		}
56
	}
57
58
	private BlockPos findStartBlock(final GameTestHelper helper) {
59
		List<BlockPos> testBlocks = this.findTestBlocks(helper, TestBlockMode.START);
60
		if (testBlocks.isEmpty()) {
61
			helper.fail(Component.translatable("test_block.error.missing", TestBlockMode.START.getDisplayName()));
62
		}
63
64
		if (testBlocks.size() != 1) {
65
			helper.fail(Component.translatable("test_block.error.too_many", TestBlockMode.START.getDisplayName()));
66
		}
67
68
		return testBlocks.getFirst();
69
	}
70
71
	private List<BlockPos> findTestBlocks(final GameTestHelper helper, final TestBlockMode mode) {
72
		List<BlockPos> blocks = new ArrayList<>();
73
		helper.forEveryBlockInStructure(pos -> {
74
			BlockState state = helper.getBlockState(pos);
75
			if (state.is(Blocks.TEST_BLOCK) && state.getValue(TestBlock.MODE) == mode) {
76
				blocks.add(pos.immutable());
77
			}
78
		});
79
		return blocks;
80
	}
81
82
	@Override
83
	public MapCodec<BlockBasedTestInstance> codec() {
84
		return CODEC;
85
	}
86
87
	@Override
88
	protected MutableComponent typeDescription() {
89
		return Component.translatable("test_instance.type.block_based");
90
	}
91
}
92

Decompiled Minecraft 26.3 source of BlockBasedTestInstance (net/minecraft/gametest/framework/BlockBasedTestInstance.java) with Mojang official mappings, stored verbatim on the site.