BlockCollisions .java

net.minecraft.world.level.BlockCollisions (class): members: onlySuffocatingBlocks, cachedBlockGetterPos, cachedBlockGetter, collisionGetter, entityShape.

net.minecraft.world.levelclass112 linescommonextends AbstractIterator<T>4.1 KB

API Surface (1)

T computeNext()

Fields & Constants (12)

box
context
cursor
pos
entityShape
collisionGetter
onlySuffocatingBlocks
cachedBlockGetter
cachedBlockGetterPos
resultProvider
source
VoxelShape

Full Source

BlockCollisions.java112 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import com.google.common.collect.AbstractIterator;
4
import java.util.function.BiFunction;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Cursor3D;
7
import net.minecraft.core.SectionPos;
8
import net.minecraft.util.Mth;
9
import net.minecraft.world.entity.Entity;
10
import net.minecraft.world.level.block.Blocks;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.phys.AABB;
13
import net.minecraft.world.phys.shapes.BooleanOp;
14
import net.minecraft.world.phys.shapes.CollisionContext;
15
import net.minecraft.world.phys.shapes.Shapes;
16
import net.minecraft.world.phys.shapes.VoxelShape;
17
import org.jspecify.annotations.Nullable;
18
19
public class BlockCollisions<T> extends AbstractIterator<T> {
20
	private final AABB box;
21
	private final CollisionContext context;
22
	private final Cursor3D cursor;
23
	private final BlockPos.MutableBlockPos pos;
24
	private final VoxelShape entityShape;
25
	private final CollisionGetter collisionGetter;
26
	private final boolean onlySuffocatingBlocks;
27
	private @Nullable BlockGetter cachedBlockGetter;
28
	private long cachedBlockGetterPos;
29
	private final BiFunction<BlockPos.MutableBlockPos, VoxelShape, T> resultProvider;
30
31
	public BlockCollisions(
32
		final CollisionGetter collisionGetter,
33
		final @Nullable Entity source,
34
		final AABB box,
35
		final boolean onlySuffocatingBlocks,
36
		final BiFunction<BlockPos.MutableBlockPos, VoxelShape, T> resultProvider
37
	) {
38
		this(collisionGetter, source == null ? CollisionContext.empty() : CollisionContext.of(source), box, onlySuffocatingBlocks, resultProvider);
39
	}
40
41
	public BlockCollisions(
42
		final CollisionGetter collisionGetter,
43
		final CollisionContext context,
44
		final AABB box,
45
		final boolean onlySuffocatingBlocks,
46
		final BiFunction<BlockPos.MutableBlockPos, VoxelShape, T> resultProvider
47
	) {
48
		this.context = context;
49
		this.pos = new BlockPos.MutableBlockPos();
50
		this.entityShape = Shapes.create(box);
51
		this.collisionGetter = collisionGetter;
52
		this.box = box;
53
		this.onlySuffocatingBlocks = onlySuffocatingBlocks;
54
		this.resultProvider = resultProvider;
55
		int x0 = Mth.floor(box.minX - 1.0E-7) - 1;
56
		int x1 = Mth.floor(box.maxX + 1.0E-7) + 1;
57
		int y0 = Mth.floor(box.minY - 1.0E-7) - 1;
58
		int y1 = Mth.floor(box.maxY + 1.0E-7) + 1;
59
		int z0 = Mth.floor(box.minZ - 1.0E-7) - 1;
60
		int z1 = Mth.floor(box.maxZ + 1.0E-7) + 1;
61
		this.cursor = new Cursor3D(x0, y0, z0, x1, y1, z1);
62
	}
63
64
	private @Nullable BlockGetter getChunk(final int x, final int z) {
65
		int chunkX = SectionPos.blockToSectionCoord(x);
66
		int chunkZ = SectionPos.blockToSectionCoord(z);
67
		long chunkPos = ChunkPos.pack(chunkX, chunkZ);
68
		if (this.cachedBlockGetter != null && this.cachedBlockGetterPos == chunkPos) {
69
			return this.cachedBlockGetter;
70
		}
71
72
		BlockGetter result = this.collisionGetter.getChunkForCollisions(chunkX, chunkZ);
73
		this.cachedBlockGetter = result;
74
		this.cachedBlockGetterPos = chunkPos;
75
		return result;
76
	}
77
78
	@Override
79
	protected T computeNext() {
80
		while (this.cursor.advance()) {
81
			int x = this.cursor.nextX();
82
			int y = this.cursor.nextY();
83
			int z = this.cursor.nextZ();
84
			int cursorFaceType = this.cursor.getNextType();
85
			if (cursorFaceType != 3) {
86
				BlockGetter chunk = this.getChunk(x, z);
87
				if (chunk != null) {
88
					this.pos.set(x, y, z);
89
					BlockState blockState = chunk.getBlockState(this.pos);
90
					if ((!this.onlySuffocatingBlocks || blockState.isSuffocating(chunk, this.pos))
91
						&& (cursorFaceType != 1 || blockState.hasLargeCollisionShape())
92
						&& (cursorFaceType != 2 || blockState.is(Blocks.MOVING_PISTON))) {
93
						VoxelShape blockShape = this.context.getCollisionShape(blockState, this.collisionGetter, this.pos);
94
						if (blockShape == Shapes.block()) {
95
							if (this.box.intersects(x, y, z, x + 1.0, y + 1.0, z + 1.0)) {
96
								return this.resultProvider.apply(this.pos, blockShape.move(this.pos));
97
							}
98
						} else {
99
							VoxelShape shape = blockShape.move(this.pos);
100
							if (!shape.isEmpty() && Shapes.joinIsNotEmpty(shape, this.entityShape, BooleanOp.AND)) {
101
								return this.resultProvider.apply(this.pos, shape);
102
							}
103
						}
104
					}
105
				}
106
			}
107
		}
108
109
		return this.endOfData();
110
	}
111
}
112

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