BlockGetter .java

net.minecraft.world.level.BlockGetter (interface): members: forEachBlockIntersectedBetween, clipWithInteractionOverride, addCollisionsAlongTravel, clip.

net.minecraft.world.levelinterface306 linescommonextends LevelHeightAccessor11.3 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (3)

blockShape
consumer
aabbAtTarget

Full Source

BlockGetter.java306 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
4
import it.unimi.dsi.fastutil.longs.LongSet;
5
import java.util.Optional;
6
import java.util.function.BiFunction;
7
import java.util.function.Function;
8
import java.util.function.Supplier;
9
import java.util.stream.Stream;
10
import net.fabricmc.fabric.api.blockgetter.v2.FabricBlockGetter;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.core.Direction;
13
import net.minecraft.core.Vec3i;
14
import net.minecraft.util.Mth;
15
import net.minecraft.world.level.block.entity.BlockEntity;
16
import net.minecraft.world.level.block.entity.BlockEntityType;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.level.material.FluidState;
19
import net.minecraft.world.phys.AABB;
20
import net.minecraft.world.phys.BlockHitResult;
21
import net.minecraft.world.phys.Vec3;
22
import net.minecraft.world.phys.shapes.VoxelShape;
23
import org.jspecify.annotations.Nullable;
24
25
public interface BlockGetter extends LevelHeightAccessor, FabricBlockGetter {
26
	@Nullable BlockEntity getBlockEntity(BlockPos pos);
27
28
	default <T extends BlockEntity> Optional<T> getBlockEntity(final BlockPos pos, final BlockEntityType<T> type) {
29
		BlockEntity blockEntity = this.getBlockEntity(pos);
30
		return blockEntity != null && blockEntity.getType() == type ? Optional.of((T)blockEntity) : Optional.empty();
31
	}
32
33
	BlockState getBlockState(final BlockPos pos);
34
35
	FluidState getFluidState(BlockPos pos);
36
37
	default int getLightEmission(final BlockPos pos) {
38
		return this.getBlockState(pos).getLightEmission();
39
	}
40
41
	default Stream<BlockState> getBlockStates(final AABB box) {
42
		return BlockPos.betweenClosedStream(box).map(this::getBlockState);
43
	}
44
45
	default BlockHitResult isBlockInLine(final ClipBlockStateContext c) {
46
		return traverseBlocks(
47
			c.getFrom(),
48
			c.getTo(),
49
			c,
50
			(context, pos) -> {
51
				BlockState blockState = this.getBlockState(pos);
52
				Vec3 delta = context.getFrom().subtract(context.getTo());
53
				return context.isTargetBlock().test(blockState)
54
					? new BlockHitResult(context.getTo(), Direction.getApproximateNearest(delta.x, delta.y, delta.z), BlockPos.containing(context.getTo()), false)
55
					: null;
56
			},
57
			context -> {
58
				Vec3 delta = context.getFrom().subtract(context.getTo());
59
				return BlockHitResult.miss(context.getTo(), Direction.getApproximateNearest(delta.x, delta.y, delta.z), BlockPos.containing(context.getTo()));
60
			}
61
		);
62
	}
63
64
	default BlockHitResult clip(final ClipContext c) {
65
		return traverseBlocks(c.getFrom(), c.getTo(), c, (context, pos) -> {
66
			BlockState blockState = this.getBlockState(pos);
67
			FluidState fluidState = this.getFluidState(pos);
68
			Vec3 from = context.getFrom();
69
			Vec3 to = context.getTo();
70
			VoxelShape blockShape = context.getBlockShape(blockState, this, pos);
71
			BlockHitResult blockResult = this.clipWithInteractionOverride(from, to, pos, blockShape, blockState);
72
			VoxelShape fluidShape = context.getFluidShape(fluidState, this, pos);
73
			BlockHitResult liquidResult = fluidShape.clip(from, to, pos);
74
			double blockDistanceSquared = blockResult == null ? Double.MAX_VALUE : context.getFrom().distanceToSqr(blockResult.getLocation());
75
			double liquidDistanceSquared = liquidResult == null ? Double.MAX_VALUE : context.getFrom().distanceToSqr(liquidResult.getLocation());
76
			return blockDistanceSquared <= liquidDistanceSquared ? blockResult : liquidResult;
77
		}, context -> {
78
			Vec3 delta = context.getFrom().subtract(context.getTo());
79
			return BlockHitResult.miss(context.getTo(), Direction.getApproximateNearest(delta.x, delta.y, delta.z), BlockPos.containing(context.getTo()));
80
		});
81
	}
82
83
	default @Nullable BlockHitResult clipWithInteractionOverride(
84
		final Vec3 from, final Vec3 to, final BlockPos pos, final VoxelShape blockShape, final BlockState blockState
85
	) {
86
		BlockHitResult result = blockShape.clip(from, to, pos);
87
		if (result != null) {
88
			BlockHitResult hitOverride = blockState.getInteractionShape(this, pos).clip(from, to, pos);
89
			if (hitOverride != null && hitOverride.getLocation().subtract(from).lengthSqr() < result.getLocation().subtract(from).lengthSqr()) {
90
				return result.withDirection(hitOverride.getDirection());
91
			}
92
		}
93
94
		return result;
95
	}
96
97
	default double getBlockFloorHeight(final VoxelShape blockShape, final Supplier<VoxelShape> belowBlockShape) {
98
		if (!blockShape.isEmpty()) {
99
			return blockShape.max(Direction.Axis.Y);
100
		}
101
102
		double belowFloor = belowBlockShape.get().max(Direction.Axis.Y);
103
		return belowFloor >= 1.0 ? belowFloor - 1.0 : Double.NEGATIVE_INFINITY;
104
	}
105
106
	default double getBlockFloorHeight(final BlockPos pos) {
107
		return this.getBlockFloorHeight(this.getBlockState(pos).getCollisionShape(this, pos), () -> {
108
			BlockPos below = pos.below();
109
			return this.getBlockState(below).getCollisionShape(this, below);
110
		});
111
	}
112
113
	static <T, C> T traverseBlocks(
114
		final Vec3 from, final Vec3 to, final C context, final BiFunction<C, BlockPos, @Nullable T> consumer, final Function<C, T> missFactory
115
	) {
116
		if (from.equals(to)) {
117
			return missFactory.apply(context);
118
		}
119
120
		double toX = Mth.lerp(-1.0E-7, to.x, from.x);
121
		double toY = Mth.lerp(-1.0E-7, to.y, from.y);
122
		double toZ = Mth.lerp(-1.0E-7, to.z, from.z);
123
		double fromX = Mth.lerp(-1.0E-7, from.x, to.x);
124
		double fromY = Mth.lerp(-1.0E-7, from.y, to.y);
125
		double fromZ = Mth.lerp(-1.0E-7, from.z, to.z);
126
		int currentBlockX = Mth.floor(fromX);
127
		int currentBlockY = Mth.floor(fromY);
128
		int currentBlockZ = Mth.floor(fromZ);
129
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(currentBlockX, currentBlockY, currentBlockZ);
130
		T first = consumer.apply(context, pos);
131
		if (first != null) {
132
			return first;
133
		}
134
135
		double dx = toX - fromX;
136
		double dy = toY - fromY;
137
		double dz = toZ - fromZ;
138
		int signX = Mth.sign(dx);
139
		int signY = Mth.sign(dy);
140
		int signZ = Mth.sign(dz);
141
		double tDeltaX = signX == 0 ? Double.MAX_VALUE : signX / dx;
142
		double tDeltaY = signY == 0 ? Double.MAX_VALUE : signY / dy;
143
		double tDeltaZ = signZ == 0 ? Double.MAX_VALUE : signZ / dz;
144
		double tX = tDeltaX * (signX > 0 ? 1.0 - Mth.frac(fromX) : Mth.frac(fromX));
145
		double tY = tDeltaY * (signY > 0 ? 1.0 - Mth.frac(fromY) : Mth.frac(fromY));
146
		double tZ = tDeltaZ * (signZ > 0 ? 1.0 - Mth.frac(fromZ) : Mth.frac(fromZ));
147
148
		while (tX <= 1.0 || tY <= 1.0 || tZ <= 1.0) {
149
			if (tX < tY) {
150
				if (tX < tZ) {
151
					currentBlockX += signX;
152
					tX += tDeltaX;
153
				} else {
154
					currentBlockZ += signZ;
155
					tZ += tDeltaZ;
156
				}
157
			} else if (tY < tZ) {
158
				currentBlockY += signY;
159
				tY += tDeltaY;
160
			} else {
161
				currentBlockZ += signZ;
162
				tZ += tDeltaZ;
163
			}
164
165
			T result = consumer.apply(context, pos.set(currentBlockX, currentBlockY, currentBlockZ));
166
			if (result != null) {
167
				return result;
168
			}
169
		}
170
171
		return missFactory.apply(context);
172
	}
173
174
	static boolean forEachBlockIntersectedBetween(final Vec3 from, final Vec3 to, final AABB aabbAtTarget, final BlockGetter.BlockStepVisitor visitor) {
175
		Vec3 travel = to.subtract(from);
176
		if (travel.lengthSqr() < Mth.square(1.0E-5F)) {
177
			for (BlockPos blockPos : BlockPos.betweenClosed(aabbAtTarget)) {
178
				if (!visitor.visit(blockPos, 0)) {
179
					return false;
180
				}
181
			}
182
183
			return true;
184
		} else {
185
			LongSet visitedBlocks = new LongOpenHashSet();
186
187
			for (BlockPos blockPos : BlockPos.betweenCornersInDirection(aabbAtTarget.move(travel.scale(-1.0)), travel)) {
188
				if (!visitor.visit(blockPos, 0)) {
189
					return false;
190
				}
191
192
				visitedBlocks.add(blockPos.asLong());
193
			}
194
195
			int iterations = addCollisionsAlongTravel(visitedBlocks, travel, aabbAtTarget, visitor);
196
			if (iterations < 0) {
197
				return false;
198
			}
199
200
			for (BlockPos blockPos : BlockPos.betweenCornersInDirection(aabbAtTarget, travel)) {
201
				if (visitedBlocks.add(blockPos.asLong()) && !visitor.visit(blockPos, iterations + 1)) {
202
					return false;
203
				}
204
			}
205
206
			return true;
207
		}
208
	}
209
210
	private static int addCollisionsAlongTravel(
211
		final LongSet visitedBlocks, final Vec3 deltaMove, final AABB aabbAtTarget, final BlockGetter.BlockStepVisitor visitor
212
	) {
213
		double boxSizeX = aabbAtTarget.getXsize();
214
		double boxSizeY = aabbAtTarget.getYsize();
215
		double boxSizeZ = aabbAtTarget.getZsize();
216
		Vec3i cornerDir = getFurthestCorner(deltaMove);
217
		Vec3 toCenter = aabbAtTarget.getCenter();
218
		Vec3 toCorner = new Vec3(
219
			toCenter.x() + boxSizeX * 0.5 * cornerDir.getX(), toCenter.y() + boxSizeY * 0.5 * cornerDir.getY(), toCenter.z() + boxSizeZ * 0.5 * cornerDir.getZ()
220
		);
221
		Vec3 fromCorner = toCorner.subtract(deltaMove);
222
		int cornerVisitedBlockX = Mth.floor(fromCorner.x);
223
		int cornerVisitedBlockY = Mth.floor(fromCorner.y);
224
		int cornerVisitedBlockZ = Mth.floor(fromCorner.z);
225
		int signX = Mth.sign(deltaMove.x);
226
		int signY = Mth.sign(deltaMove.y);
227
		int signZ = Mth.sign(deltaMove.z);
228
		double tDeltaX = signX == 0 ? Double.MAX_VALUE : signX / deltaMove.x;
229
		double tDeltaY = signY == 0 ? Double.MAX_VALUE : signY / deltaMove.y;
230
		double tDeltaZ = signZ == 0 ? Double.MAX_VALUE : signZ / deltaMove.z;
231
		double tX = tDeltaX * (signX > 0 ? 1.0 - Mth.frac(fromCorner.x) : Mth.frac(fromCorner.x));
232
		double tY = tDeltaY * (signY > 0 ? 1.0 - Mth.frac(fromCorner.y) : Mth.frac(fromCorner.y));
233
		double tZ = tDeltaZ * (signZ > 0 ? 1.0 - Mth.frac(fromCorner.z) : Mth.frac(fromCorner.z));
234
		int iterations = 0;
235
236
		while (tX <= 1.0 || tY <= 1.0 || tZ <= 1.0) {
237
			if (tX < tY) {
238
				if (tX < tZ) {
239
					cornerVisitedBlockX += signX;
240
					tX += tDeltaX;
241
				} else {
242
					cornerVisitedBlockZ += signZ;
243
					tZ += tDeltaZ;
244
				}
245
			} else if (tY < tZ) {
246
				cornerVisitedBlockY += signY;
247
				tY += tDeltaY;
248
			} else {
249
				cornerVisitedBlockZ += signZ;
250
				tZ += tDeltaZ;
251
			}
252
253
			Optional<Vec3> hitPointOpt = AABB.clip(
254
				cornerVisitedBlockX,
255
				cornerVisitedBlockY,
256
				cornerVisitedBlockZ,
257
				cornerVisitedBlockX + 1,
258
				cornerVisitedBlockY + 1,
259
				cornerVisitedBlockZ + 1,
260
				fromCorner,
261
				toCorner
262
			);
263
			if (!hitPointOpt.isEmpty()) {
264
				iterations++;
265
				Vec3 hitPoint = hitPointOpt.get();
266
				double cornerHitX = Mth.clamp(hitPoint.x, cornerVisitedBlockX + 1.0E-5F, cornerVisitedBlockX + 1.0 - 1.0E-5F);
267
				double cornerHitY = Mth.clamp(hitPoint.y, cornerVisitedBlockY + 1.0E-5F, cornerVisitedBlockY + 1.0 - 1.0E-5F);
268
				double cornerHitZ = Mth.clamp(hitPoint.z, cornerVisitedBlockZ + 1.0E-5F, cornerVisitedBlockZ + 1.0 - 1.0E-5F);
269
				int oppositeCornerX = Mth.floor(cornerHitX - boxSizeX * cornerDir.getX());
270
				int oppositeCornerY = Mth.floor(cornerHitY - boxSizeY * cornerDir.getY());
271
				int oppositeCornerZ = Mth.floor(cornerHitZ - boxSizeZ * cornerDir.getZ());
272
				int currentIteration = iterations;
273
274
				for (BlockPos pos : BlockPos.betweenCornersInDirection(
275
					cornerVisitedBlockX, cornerVisitedBlockY, cornerVisitedBlockZ, oppositeCornerX, oppositeCornerY, oppositeCornerZ, deltaMove
276
				)) {
277
					if (visitedBlocks.add(pos.asLong()) && !visitor.visit(pos, currentIteration)) {
278
						return -1;
279
					}
280
				}
281
			}
282
		}
283
284
		return iterations;
285
	}
286
287
	private static Vec3i getFurthestCorner(final Vec3 direction) {
288
		double xDot = Math.abs(Vec3.X_AXIS.dot(direction));
289
		double yDot = Math.abs(Vec3.Y_AXIS.dot(direction));
290
		double zDot = Math.abs(Vec3.Z_AXIS.dot(direction));
291
		int xSign = direction.x >= 0.0 ? 1 : -1;
292
		int ySign = direction.y >= 0.0 ? 1 : -1;
293
		int zSign = direction.z >= 0.0 ? 1 : -1;
294
		if (xDot <= yDot && xDot <= zDot) {
295
			return new Vec3i(-xSign, -zSign, ySign);
296
		} else {
297
			return yDot <= zDot ? new Vec3i(zSign, -ySign, -xSign) : new Vec3i(-ySign, xSign, -zSign);
298
		}
299
	}
300
301
	@FunctionalInterface
302
	interface BlockStepVisitor {
303
		boolean visit(BlockPos pos, int iteration);
304
	}
305
}
306

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