BlockScanUtils .java

net.minecraft.world.level.blockscan.BlockScanUtils (class): members: findBlocksInSection, fromX, fromY, fromZ, toX, toY, toZ, section, maxX, maxZ, minZ.

net.minecraft.world.level.blockscanclass137 linescommon4.8 KB

API Surface (1)

boolean findBlocksInSection(final LevelChunkSection section, final BlockPos origin, final int fromX, final int fromY, final int fromZ, final int toX, final int toY, final int toZ, final Predicate<BlockState> predicate, final BlockStateConsumer consumer)

Fields & Constants (16)

predicate
level
minX
minY
minZ
maxX
maxY
maxZ
section
origin
fromX
fromY
fromZ
toX
toY
toZ

Full Source

BlockScanUtils.java137 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.blockscan;
2
3
import java.util.function.Predicate;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.SectionPos;
6
import net.minecraft.world.level.LevelReader;
7
import net.minecraft.world.level.block.Blocks;
8
import net.minecraft.world.level.block.state.BlockState;
9
import net.minecraft.world.level.chunk.ChunkAccess;
10
import net.minecraft.world.level.chunk.LevelChunkSection;
11
12
public class BlockScanUtils {
13
	static boolean findBlocks(
14
		final LevelReader level, final BlockPos from, final BlockPos to, final Predicate<BlockState> predicate, final BlockStateConsumer consumer
15
	) {
16
		int minX = Math.min(from.getX(), to.getX());
17
		int minY = Math.min(from.getY(), to.getY());
18
		int minZ = Math.min(from.getZ(), to.getZ());
19
		int maxX = Math.max(from.getX(), to.getX());
20
		int maxY = Math.max(from.getY(), to.getY());
21
		int maxZ = Math.max(from.getZ(), to.getZ());
22
		return sectionBasedScan(level, minX, minY, minZ, maxX, maxY, maxZ, predicate, consumer);
23
	}
24
25
	private static boolean sectionBasedScan(
26
		final LevelReader level,
27
		final int minX,
28
		final int minY,
29
		final int minZ,
30
		final int maxX,
31
		final int maxY,
32
		final int maxZ,
33
		final Predicate<BlockState> predicate,
34
		final BlockStateConsumer consumer
35
	) {
36
		int minSectionX = SectionPos.blockToSectionCoord(minX);
37
		int minSectionY = Math.max(level.getMinSectionY(), SectionPos.blockToSectionCoord(minY));
38
		int minSectionZ = SectionPos.blockToSectionCoord(minZ);
39
		int maxSectionX = SectionPos.blockToSectionCoord(maxX);
40
		int maxSectionY = Math.min(level.getMaxSectionY(), SectionPos.blockToSectionCoord(maxY));
41
		int maxSectionZ = SectionPos.blockToSectionCoord(maxZ);
42
43
		for (int x = minSectionX; x <= maxSectionX; x++) {
44
			for (int z = minSectionZ; z <= maxSectionZ; z++) {
45
				ChunkAccess chunk = level.getChunk(x, z);
46
47
				for (int y = minSectionY; y <= maxSectionY; y++) {
48
					LevelChunkSection section = chunk.getSection(chunk.getSectionIndexFromSectionY(y));
49
					if (section.maybeHas(predicate)) {
50
						int sectionOriginX = SectionPos.sectionToBlockCoord(x);
51
						int sectionOriginY = SectionPos.sectionToBlockCoord(y);
52
						int sectionOriginZ = SectionPos.sectionToBlockCoord(z);
53
						int fromX = SectionPos.sectionRelative(Math.max(sectionOriginX, minX));
54
						int fromY = SectionPos.sectionRelative(Math.max(sectionOriginY, minY));
55
						int fromZ = SectionPos.sectionRelative(Math.max(sectionOriginZ, minZ));
56
						int toX = SectionPos.sectionRelative(Math.min(SectionPos.sectionToBlockCoord(x, 15), maxX));
57
						int toY = SectionPos.sectionRelative(Math.min(SectionPos.sectionToBlockCoord(y, 15), maxY));
58
						int toZ = SectionPos.sectionRelative(Math.min(SectionPos.sectionToBlockCoord(z, 15), maxZ));
59
						BlockPos origin = new BlockPos(sectionOriginX, sectionOriginY, sectionOriginZ);
60
						if (findBlocksInSection(section, origin, fromX, fromY, fromZ, toX, toY, toZ, predicate, consumer)) {
61
							return true;
62
						}
63
					}
64
				}
65
			}
66
		}
67
68
		boolean belowWorld = minY < level.getMinY();
69
		boolean aboveWorld = maxY > level.getMaxY();
70
		if ((belowWorld || aboveWorld) && predicate.test(Blocks.AIR.defaultBlockState())) {
71
			if (belowWorld) {
72
				for (BlockPos pos : BlockPos.betweenClosed(minX, minY, minZ, maxX, level.getMinY() - 1, maxZ)) {
73
					if (consumer.apply(pos, Blocks.AIR.defaultBlockState()).shouldAbort()) {
74
						return true;
75
					}
76
				}
77
			}
78
79
			if (aboveWorld) {
80
				for (BlockPos pos : BlockPos.betweenClosed(minX, level.getMaxY() + 1, minZ, maxX, maxY, maxZ)) {
81
					if (consumer.apply(pos, Blocks.AIR.defaultBlockState()).shouldAbort()) {
82
						return true;
83
					}
84
				}
85
			}
86
		}
87
88
		return false;
89
	}
90
91
	public static boolean findBlocksInSection(
92
		final LevelChunkSection section,
93
		final BlockPos origin,
94
		final int fromX,
95
		final int fromY,
96
		final int fromZ,
97
		final int toX,
98
		final int toY,
99
		final int toZ,
100
		final Predicate<BlockState> predicate,
101
		final BlockStateConsumer consumer
102
	) {
103
		BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
104
105
		for (int y = fromY; y <= toY; y++) {
106
			for (int z = fromZ; z <= toZ; z++) {
107
				for (int x = fromX; x <= toX; x++) {
108
					BlockState blockState = section.getBlockState(x, y, z);
109
					if (predicate.test(blockState)) {
110
						mutablePos.setWithOffset(origin, x, y, z);
111
						if (consumer.apply(mutablePos, blockState).shouldAbort()) {
112
							return true;
113
						}
114
					}
115
				}
116
			}
117
		}
118
119
		return false;
120
	}
121
122
	static boolean findBlocksWithCache(
123
		final LevelReader level, final Iterable<BlockPos> positions, final Predicate<BlockState> predicate, final BlockStateConsumer consumer
124
	) {
125
		FilteredSectionCache sectionCache = new FilteredSectionCache(level, predicate);
126
127
		for (BlockPos pos : positions) {
128
			BlockState state = sectionCache.getBlockState(pos);
129
			if (state != null && consumer.apply(pos, state).shouldAbort()) {
130
				return true;
131
			}
132
		}
133
134
		return false;
135
	}
136
}
137

Decompiled Minecraft 26.3 source of BlockScanUtils (net/minecraft/world/level/blockscan/BlockScanUtils.java) with Mojang official mappings, stored verbatim on the site.