PortalForcer .java

net.minecraft.world.level.portal.PortalForcer (class, 181 lines): members: findClosestPortalPosition, OVERWORLD_PORTAL_RADIUS, NETHER_PORTAL_RADIUS, level.

net.minecraft.world.level.portalclass181 linescommon7.3 KB

API Surface (2)

Optional<BlockPos> findClosestPortalPosition(final BlockPos approximateExitPos, final boolean toNether, final WorldBorder worldBorder)
Optional<BlockUtil.FoundRectangle> createPortal(final BlockPos origin, final Direction.Axis portalAxis)

Fields & Constants (14)

TICKET_RADIUS
NETHER_PORTAL_RADIUS
OVERWORLD_PORTAL_RADIUS
FRAME_HEIGHT
FRAME_WIDTH
FRAME_BOX
FRAME_HEIGHT_START
FRAME_HEIGHT_END
FRAME_WIDTH_START
FRAME_WIDTH_END
FRAME_BOX_START
FRAME_BOX_END
NOTHING_FOUND
level

Full Source

PortalForcer.java181 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.portal;
2
3
import java.util.Comparator;
4
import java.util.Optional;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.core.Vec3i;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.util.BlockUtil;
10
import net.minecraft.util.Mth;
11
import net.minecraft.world.entity.ai.village.poi.PoiManager;
12
import net.minecraft.world.entity.ai.village.poi.PoiRecord;
13
import net.minecraft.world.entity.ai.village.poi.PoiTypes;
14
import net.minecraft.world.level.block.Blocks;
15
import net.minecraft.world.level.block.NetherPortalBlock;
16
import net.minecraft.world.level.block.state.BlockState;
17
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
18
import net.minecraft.world.level.border.WorldBorder;
19
import net.minecraft.world.level.levelgen.Heightmap;
20
21
public class PortalForcer {
22
	public static final int TICKET_RADIUS = 3;
23
	private static final int NETHER_PORTAL_RADIUS = 16;
24
	private static final int OVERWORLD_PORTAL_RADIUS = 128;
25
	private static final int FRAME_HEIGHT = 5;
26
	private static final int FRAME_WIDTH = 4;
27
	private static final int FRAME_BOX = 3;
28
	private static final int FRAME_HEIGHT_START = -1;
29
	private static final int FRAME_HEIGHT_END = 4;
30
	private static final int FRAME_WIDTH_START = -1;
31
	private static final int FRAME_WIDTH_END = 3;
32
	private static final int FRAME_BOX_START = -1;
33
	private static final int FRAME_BOX_END = 2;
34
	private static final int NOTHING_FOUND = -1;
35
	private final ServerLevel level;
36
37
	public PortalForcer(final ServerLevel level) {
38
		this.level = level;
39
	}
40
41
	public Optional<BlockPos> findClosestPortalPosition(final BlockPos approximateExitPos, final boolean toNether, final WorldBorder worldBorder) {
42
		PoiManager poiManager = this.level.getPoiManager();
43
		int radius = toNether ? 16 : 128;
44
		poiManager.ensureLoadedAndValid(this.level, approximateExitPos, radius);
45
		return poiManager.getInSquare(type -> type.is(PoiTypes.NETHER_PORTAL), approximateExitPos, radius, PoiManager.Occupancy.ANY)
46
			.map(PoiRecord::getPos)
47
			.filter(worldBorder::isWithinBounds)
48
			.filter(pos -> this.level.getBlockState(pos).hasProperty(BlockStateProperties.HORIZONTAL_AXIS))
49
			.min(Comparator.<BlockPos>comparingDouble(p -> p.distSqr(approximateExitPos)).thenComparingInt(Vec3i::getY));
50
	}
51
52
	public Optional<BlockUtil.FoundRectangle> createPortal(final BlockPos origin, final Direction.Axis portalAxis) {
53
		Direction direction = Direction.get(Direction.AxisDirection.POSITIVE, portalAxis);
54
		double closestFullDistanceSqr = -1.0;
55
		BlockPos closestFullPosition = null;
56
		double closestPartialDistanceSqr = -1.0;
57
		BlockPos closestPartialPosition = null;
58
		WorldBorder worldBorder = this.level.getWorldBorder();
59
		int maxPlaceableY = Math.min(this.level.getMaxY(), this.level.getMinY() + this.level.getLogicalHeight() - 1);
60
		int edgeDistance = 1;
61
		BlockPos.MutableBlockPos mutable = origin.mutable();
62
63
		for (BlockPos.MutableBlockPos columnPos : BlockPos.spiralAround(origin, 16, Direction.EAST, Direction.SOUTH)) {
64
			int height = Math.min(maxPlaceableY, this.level.getHeight(Heightmap.Types.MOTION_BLOCKING, columnPos.getX(), columnPos.getZ()));
65
			if (worldBorder.isWithinBounds(columnPos) && worldBorder.isWithinBounds(columnPos.move(direction, 1))) {
66
				columnPos.move(direction.getOpposite(), 1);
67
68
				for (int y = height; y >= this.level.getMinY(); y--) {
69
					columnPos.setY(y);
70
					if (this.canPortalReplaceBlock(columnPos)) {
71
						int firstEmptyY = y;
72
73
						while (y > this.level.getMinY() && this.canPortalReplaceBlock(columnPos.move(Direction.DOWN))) {
74
							y--;
75
						}
76
77
						if (y + 4 <= maxPlaceableY) {
78
							int deltaY = firstEmptyY - y;
79
							if (deltaY <= 0 || deltaY >= 3) {
80
								columnPos.setY(y);
81
								if (this.canHostFrame(columnPos, mutable, direction, 0)) {
82
									double distance = origin.distSqr(columnPos);
83
									if (this.canHostFrame(columnPos, mutable, direction, -1)
84
										&& this.canHostFrame(columnPos, mutable, direction, 1)
85
										&& (closestFullDistanceSqr == -1.0 || closestFullDistanceSqr > distance)) {
86
										closestFullDistanceSqr = distance;
87
										closestFullPosition = columnPos.immutable();
88
									}
89
90
									if (closestFullDistanceSqr == -1.0 && (closestPartialDistanceSqr == -1.0 || closestPartialDistanceSqr > distance)) {
91
										closestPartialDistanceSqr = distance;
92
										closestPartialPosition = columnPos.immutable();
93
									}
94
								}
95
							}
96
						}
97
					}
98
				}
99
			}
100
		}
101
102
		if (closestFullDistanceSqr == -1.0 && closestPartialDistanceSqr != -1.0) {
103
			closestFullPosition = closestPartialPosition;
104
			closestFullDistanceSqr = closestPartialDistanceSqr;
105
		}
106
107
		if (closestFullDistanceSqr == -1.0) {
108
			int minStartY = Math.max(this.level.getMinY() - -1, 70);
109
			int maxStartY = maxPlaceableY - 9;
110
			if (maxStartY < minStartY) {
111
				return Optional.empty();
112
			}
113
114
			closestFullPosition = new BlockPos(
115
					origin.getX() - direction.getStepX() * 1, Mth.clamp(origin.getY(), minStartY, maxStartY), origin.getZ() - direction.getStepZ() * 1
116
				)
117
				.immutable();
118
			closestFullPosition = worldBorder.clampToBounds(closestFullPosition);
119
			Direction clockWise = direction.getClockWise();
120
121
			for (int box = -1; box < 2; box++) {
122
				for (int width = 0; width < 2; width++) {
123
					for (int height = -1; height < 3; height++) {
124
						BlockState blockState = height < 0 ? Blocks.OBSIDIAN.defaultBlockState() : Blocks.AIR.defaultBlockState();
125
						mutable.setWithOffset(
126
							closestFullPosition, width * direction.getStepX() + box * clockWise.getStepX(), height, width * direction.getStepZ() + box * clockWise.getStepZ()
127
						);
128
						this.level.setBlockAndUpdate(mutable, blockState);
129
					}
130
				}
131
			}
132
		}
133
134
		for (int width = -1; width < 3; width++) {
135
			for (int height = -1; height < 4; height++) {
136
				if (width == -1 || width == 2 || height == -1 || height == 3) {
137
					mutable.setWithOffset(closestFullPosition, width * direction.getStepX(), height, width * direction.getStepZ());
138
					this.level.setBlockAndUpdate(mutable, Blocks.OBSIDIAN.defaultBlockState());
139
				}
140
			}
141
		}
142
143
		BlockState portalBlockState = Blocks.NETHER_PORTAL.defaultBlockState().setValue(NetherPortalBlock.AXIS, portalAxis);
144
145
		for (int width = 0; width < 2; width++) {
146
			for (int height = 0; height < 3; height++) {
147
				mutable.setWithOffset(closestFullPosition, width * direction.getStepX(), height, width * direction.getStepZ());
148
				this.level.setBlock(mutable, portalBlockState, 18);
149
			}
150
		}
151
152
		return Optional.of(new BlockUtil.FoundRectangle(closestFullPosition.immutable(), 2, 3));
153
	}
154
155
	private boolean canPortalReplaceBlock(final BlockPos.MutableBlockPos pos) {
156
		BlockState blockState = this.level.getBlockState(pos);
157
		return blockState.canBeReplaced() && blockState.getFluidState().isEmpty();
158
	}
159
160
	private boolean canHostFrame(final BlockPos origin, final BlockPos.MutableBlockPos mutable, final Direction direction, final int offset) {
161
		Direction clockWise = direction.getClockWise();
162
163
		for (int width = -1; width < 3; width++) {
164
			for (int height = -1; height < 4; height++) {
165
				mutable.setWithOffset(
166
					origin, direction.getStepX() * width + clockWise.getStepX() * offset, height, direction.getStepZ() * width + clockWise.getStepZ() * offset
167
				);
168
				if (height < 0 && !this.level.getBlockState(mutable).isSolid()) {
169
					return false;
170
				}
171
172
				if (height >= 0 && !this.canPortalReplaceBlock(mutable)) {
173
					return false;
174
				}
175
			}
176
		}
177
178
		return true;
179
	}
180
}
181

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