PortalShape .java

net.minecraft.world.level.portal.PortalShape (class): members: SAFE_TRAVEL_MAX_VERTICAL_DELTA, SAFE_TRAVEL_MAX_ENTITY_XY, findCollisionFreePosition, FRAME.

net.minecraft.world.level.portalclass239 linescommon9.1 KB

API Surface (8)

Optional<PortalShape> findEmptyPortalShape(final LevelAccessor level, final BlockPos pos, final Direction.Axis preferredAxis)
Optional<PortalShape> findPortalShape(final LevelAccessor level, final BlockPos pos, final Predicate<PortalShape> isValid, final Direction.Axis preferredAxis)
PortalShape findAnyShape(final BlockGetter level, final BlockPos pos, final Direction.Axis axis)
boolean isValid()
void createPortalBlocks(final LevelAccessor level)
boolean isComplete()
Vec3 getRelativePosition(final BlockUtil.FoundRectangle largestRectangleAround, final Direction.Axis axis, final Vec3 position, final EntityDimensions dimensions)
Vec3 findCollisionFreePosition(final Vec3 bottomCenter, final ServerLevel serverLevel, final Entity entity, final EntityDimensions dimensions)

Fields & Constants (17)

MIN_WIDTH
MAX_WIDTH
MIN_HEIGHT
MAX_HEIGHT
FRAME
SAFE_TRAVEL_MAX_ENTITY_XY
SAFE_TRAVEL_MAX_VERTICAL_DELTA
axis
rightDir
numPortalBlocks
bottomLeft
height
width
isValid
level
pos
position

Full Source

PortalShape.java239 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.portal;
2
3
import java.util.Optional;
4
import java.util.function.Predicate;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.tags.BlockTags;
9
import net.minecraft.util.BlockUtil;
10
import net.minecraft.util.Mth;
11
import net.minecraft.world.entity.Entity;
12
import net.minecraft.world.entity.EntityDimensions;
13
import net.minecraft.world.level.BlockGetter;
14
import net.minecraft.world.level.LevelAccessor;
15
import net.minecraft.world.level.block.Blocks;
16
import net.minecraft.world.level.block.NetherPortalBlock;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.phys.AABB;
19
import net.minecraft.world.phys.Vec3;
20
import net.minecraft.world.phys.shapes.Shapes;
21
import net.minecraft.world.phys.shapes.VoxelShape;
22
import org.apache.commons.lang3.mutable.MutableInt;
23
import org.jspecify.annotations.Nullable;
24
25
public class PortalShape {
26
	private static final int MIN_WIDTH = 2;
27
	public static final int MAX_WIDTH = 21;
28
	private static final int MIN_HEIGHT = 3;
29
	public static final int MAX_HEIGHT = 21;
30
	public static final Predicate<BlockState> FRAME = state -> state.is(BlockTags.NETHER_PORTAL_FRAME);
31
	private static final float SAFE_TRAVEL_MAX_ENTITY_XY = 4.0F;
32
	private static final double SAFE_TRAVEL_MAX_VERTICAL_DELTA = 1.0;
33
	private final Direction.Axis axis;
34
	private final Direction rightDir;
35
	private final int numPortalBlocks;
36
	private final BlockPos bottomLeft;
37
	private final int height;
38
	private final int width;
39
40
	private PortalShape(
41
		final Direction.Axis axis, final int portalBlockCount, final Direction rightDir, final BlockPos bottomLeft, final int width, final int height
42
	) {
43
		this.axis = axis;
44
		this.numPortalBlocks = portalBlockCount;
45
		this.rightDir = rightDir;
46
		this.bottomLeft = bottomLeft;
47
		this.width = width;
48
		this.height = height;
49
	}
50
51
	public static Optional<PortalShape> findEmptyPortalShape(final LevelAccessor level, final BlockPos pos, final Direction.Axis preferredAxis) {
52
		return findPortalShape(level, pos, shape -> shape.isValid() && shape.numPortalBlocks == 0, preferredAxis);
53
	}
54
55
	public static Optional<PortalShape> findPortalShape(
56
		final LevelAccessor level, final BlockPos pos, final Predicate<PortalShape> isValid, final Direction.Axis preferredAxis
57
	) {
58
		Optional<PortalShape> firstAxis = Optional.of(findAnyShape(level, pos, preferredAxis)).filter(isValid);
59
		if (firstAxis.isPresent()) {
60
			return firstAxis;
61
		}
62
63
		Direction.Axis otherAxis = preferredAxis == Direction.Axis.X ? Direction.Axis.Z : Direction.Axis.X;
64
		return Optional.of(findAnyShape(level, pos, otherAxis)).filter(isValid);
65
	}
66
67
	public static PortalShape findAnyShape(final BlockGetter level, final BlockPos pos, final Direction.Axis axis) {
68
		Direction rightDir = axis == Direction.Axis.X ? Direction.WEST : Direction.SOUTH;
69
		BlockPos bottomLeft = calculateBottomLeft(level, rightDir, pos);
70
		if (bottomLeft == null) {
71
			return new PortalShape(axis, 0, rightDir, pos, 0, 0);
72
		}
73
74
		int width = calculateWidth(level, bottomLeft, rightDir);
75
		if (width == 0) {
76
			return new PortalShape(axis, 0, rightDir, bottomLeft, 0, 0);
77
		}
78
79
		MutableInt portalBlockCountOutput = new MutableInt();
80
		int height = calculateHeight(level, bottomLeft, rightDir, width, portalBlockCountOutput);
81
		return new PortalShape(axis, portalBlockCountOutput.intValue(), rightDir, bottomLeft, width, height);
82
	}
83
84
	private static @Nullable BlockPos calculateBottomLeft(final BlockGetter level, final Direction rightDir, BlockPos pos) {
85
		int minY = Math.max(level.getMinY(), pos.getY() - 21);
86
87
		while (pos.getY() > minY && isEmpty(level.getBlockState(pos.below()))) {
88
			pos = pos.below();
89
		}
90
91
		Direction leftDir = rightDir.getOpposite();
92
		int edge = getDistanceUntilEdgeAboveFrame(level, pos, leftDir) - 1;
93
		return edge < 0 ? null : pos.relative(leftDir, edge);
94
	}
95
96
	private static int calculateWidth(final BlockGetter level, final BlockPos bottomLeft, final Direction rightDir) {
97
		int width = getDistanceUntilEdgeAboveFrame(level, bottomLeft, rightDir);
98
		return width >= 2 && width <= 21 ? width : 0;
99
	}
100
101
	private static int getDistanceUntilEdgeAboveFrame(final BlockGetter level, final BlockPos pos, final Direction direction) {
102
		BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
103
104
		for (int width = 0; width <= 21; width++) {
105
			blockPos.set(pos).move(direction, width);
106
			BlockState blockState = level.getBlockState(blockPos);
107
			if (!isEmpty(blockState)) {
108
				if (FRAME.test(blockState)) {
109
					return width;
110
				}
111
				break;
112
			}
113
114
			BlockState belowState = level.getBlockState(blockPos.move(Direction.DOWN));
115
			if (!FRAME.test(belowState)) {
116
				break;
117
			}
118
		}
119
120
		return 0;
121
	}
122
123
	private static int calculateHeight(
124
		final BlockGetter level, final BlockPos bottomLeft, final Direction rightDir, final int width, final MutableInt portalBlockCount
125
	) {
126
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
127
		int height = getDistanceUntilTop(level, bottomLeft, rightDir, pos, width, portalBlockCount);
128
		return height >= 3 && height <= 21 && hasTopFrame(level, bottomLeft, rightDir, pos, width, height) ? height : 0;
129
	}
130
131
	private static boolean hasTopFrame(
132
		final BlockGetter level, final BlockPos bottomLeft, final Direction rightDir, final BlockPos.MutableBlockPos pos, final int width, final int height
133
	) {
134
		for (int i = 0; i < width; i++) {
135
			BlockPos.MutableBlockPos framePos = pos.set(bottomLeft).move(Direction.UP, height).move(rightDir, i);
136
			if (!FRAME.test(level.getBlockState(framePos))) {
137
				return false;
138
			}
139
		}
140
141
		return true;
142
	}
143
144
	private static int getDistanceUntilTop(
145
		final BlockGetter level,
146
		final BlockPos bottomLeft,
147
		final Direction rightDir,
148
		final BlockPos.MutableBlockPos pos,
149
		final int width,
150
		final MutableInt portalBlockCount
151
	) {
152
		for (int height = 0; height < 21; height++) {
153
			pos.set(bottomLeft).move(Direction.UP, height).move(rightDir, -1);
154
			if (!FRAME.test(level.getBlockState(pos))) {
155
				return height;
156
			}
157
158
			pos.set(bottomLeft).move(Direction.UP, height).move(rightDir, width);
159
			if (!FRAME.test(level.getBlockState(pos))) {
160
				return height;
161
			}
162
163
			for (int i = 0; i < width; i++) {
164
				pos.set(bottomLeft).move(Direction.UP, height).move(rightDir, i);
165
				BlockState state = level.getBlockState(pos);
166
				if (!isEmpty(state)) {
167
					return height;
168
				}
169
170
				if (state.is(Blocks.NETHER_PORTAL)) {
171
					portalBlockCount.increment();
172
				}
173
			}
174
		}
175
176
		return 21;
177
	}
178
179
	private static boolean isEmpty(final BlockState state) {
180
		return state.isAir() || state.is(BlockTags.FIRE) || state.is(Blocks.NETHER_PORTAL);
181
	}
182
183
	public boolean isValid() {
184
		return this.width >= 2 && this.width <= 21 && this.height >= 3 && this.height <= 21;
185
	}
186
187
	public void createPortalBlocks(final LevelAccessor level) {
188
		BlockState portalState = Blocks.NETHER_PORTAL.defaultBlockState().setValue(NetherPortalBlock.AXIS, this.axis);
189
		BlockPos.betweenClosed(this.bottomLeft, this.bottomLeft.relative(Direction.UP, this.height - 1).relative(this.rightDir, this.width - 1))
190
			.forEach(pos -> level.setBlock(pos, portalState, 18));
191
	}
192
193
	public boolean isComplete() {
194
		return this.isValid() && this.numPortalBlocks == this.width * this.height;
195
	}
196
197
	public static Vec3 getRelativePosition(
198
		final BlockUtil.FoundRectangle largestRectangleAround, final Direction.Axis axis, final Vec3 position, final EntityDimensions dimensions
199
	) {
200
		double width = (double)largestRectangleAround.axis1Size - dimensions.width();
201
		double height = (double)largestRectangleAround.axis2Size - dimensions.height();
202
		BlockPos bottomMin = largestRectangleAround.minCorner;
203
		double relativeRight;
204
		if (width > 0.0) {
205
			double bottomStart = bottomMin.get(axis) + dimensions.width() / 2.0;
206
			relativeRight = Mth.clamp(Mth.inverseLerp(position.get(axis) - bottomStart, 0.0, width), 0.0, 1.0);
207
		} else {
208
			relativeRight = 0.5;
209
		}
210
211
		double relativeUp;
212
		if (height > 0.0) {
213
			Direction.Axis heightAxis = Direction.Axis.Y;
214
			relativeUp = Mth.clamp(Mth.inverseLerp(position.get(heightAxis) - bottomMin.get(heightAxis), 0.0, height), 0.0, 1.0);
215
		} else {
216
			relativeUp = 0.0;
217
		}
218
219
		Direction.Axis forwardAxis = axis == Direction.Axis.X ? Direction.Axis.Z : Direction.Axis.X;
220
		double relativeForward = position.get(forwardAxis) - (bottomMin.get(forwardAxis) + 0.5);
221
		return new Vec3(relativeRight, relativeUp, relativeForward);
222
	}
223
224
	public static Vec3 findCollisionFreePosition(final Vec3 bottomCenter, final ServerLevel serverLevel, final Entity entity, final EntityDimensions dimensions) {
225
		if (!(dimensions.width() > 4.0F) && !(dimensions.height() > 4.0F)) {
226
			double halfHeight = dimensions.height() / 2.0;
227
			Vec3 center = bottomCenter.add(0.0, halfHeight, 0.0);
228
			VoxelShape allowedCenters = Shapes.create(AABB.ofSize(center, dimensions.width(), 0.0, dimensions.width()).expandTowards(0.0, 1.0, 0.0).inflate(1.0E-6));
229
			Optional<Vec3> collisionFreePosition = serverLevel.findFreePosition(
230
				entity, allowedCenters, center, dimensions.width(), dimensions.height(), dimensions.width()
231
			);
232
			Optional<Vec3> collisionFreeBottomCenter = collisionFreePosition.map(vec -> vec.subtract(0.0, halfHeight, 0.0));
233
			return collisionFreeBottomCenter.orElse(bottomCenter);
234
		} else {
235
			return bottomCenter;
236
		}
237
	}
238
}
239

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