RuinedPortalStructure .java

net.minecraft.world.level.levelgen.structure.structures.RuinedPortalStructure (class, 255 lines): members: STRUCTURE_LOCATION_GIANT_PORTALS, MIN_Y_INDEX.

net.minecraft.world.level.levelgen.structure.structuresclass255 linescommonextends Structure9.9 KB

API Surface (3)

Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context)
StructureType<?> type()
record Setup(RuinedPortalPiece.VerticalPlacement placement, float airPocketProbability, float mossiness, boolean overgrown, boolean vines, boolean canBeCold, boolean replaceWithBlackstone, float weight)

Fields & Constants (21)

STRUCTURE_LOCATION_PORTALS
STRUCTURE_LOCATION_GIANT_PORTALS
PROBABILITY_OF_GIANT_PORTAL
MIN_Y_INDEX
CODEC
setups
random
generator
verticalPlacement
airPocket
surfaceYAtCenter
ySpan
boundingBox
heightAccessor
placement
airPocketProbability
mossiness
overgrown
vines
canBeCold
replaceWithBlackstone

String literals (22)

"ruined_portal/portal_1"
"ruined_portal/portal_2"
"ruined_portal/portal_3"
"ruined_portal/portal_4"
"ruined_portal/portal_5"
"ruined_portal/portal_6"
"ruined_portal/portal_7"
"ruined_portal/portal_8"
"ruined_portal/portal_9"
"ruined_portal/portal_10"
"ruined_portal/giant_portal_1"
"ruined_portal/giant_portal_2"
"ruined_portal/giant_portal_3"
"setups"
"placement"
"air_pocket_probability"
"mossiness"
"overgrown"
"vines"
"can_be_cold"
"replace_with_blackstone"
"weight"

Full Source

RuinedPortalStructure.java255 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.structure.structures;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.MapCodec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import java.util.List;
8
import java.util.Optional;
9
import java.util.stream.Collectors;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Holder;
12
import net.minecraft.core.QuartPos;
13
import net.minecraft.resources.Identifier;
14
import net.minecraft.util.ExtraCodecs;
15
import net.minecraft.util.Mth;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.util.Util;
18
import net.minecraft.world.level.LevelHeightAccessor;
19
import net.minecraft.world.level.NoiseColumn;
20
import net.minecraft.world.level.biome.Biome;
21
import net.minecraft.world.level.block.Mirror;
22
import net.minecraft.world.level.block.Rotation;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.minecraft.world.level.chunk.ChunkGenerator;
25
import net.minecraft.world.level.levelgen.Heightmap;
26
import net.minecraft.world.level.levelgen.RandomState;
27
import net.minecraft.world.level.levelgen.WorldgenRandom;
28
import net.minecraft.world.level.levelgen.structure.BoundingBox;
29
import net.minecraft.world.level.levelgen.structure.Structure;
30
import net.minecraft.world.level.levelgen.structure.StructureType;
31
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
32
33
public class RuinedPortalStructure extends Structure {
34
	private static final String[] STRUCTURE_LOCATION_PORTALS = new String[]{
35
		"ruined_portal/portal_1",
36
		"ruined_portal/portal_2",
37
		"ruined_portal/portal_3",
38
		"ruined_portal/portal_4",
39
		"ruined_portal/portal_5",
40
		"ruined_portal/portal_6",
41
		"ruined_portal/portal_7",
42
		"ruined_portal/portal_8",
43
		"ruined_portal/portal_9",
44
		"ruined_portal/portal_10"
45
	};
46
	private static final String[] STRUCTURE_LOCATION_GIANT_PORTALS = new String[]{
47
		"ruined_portal/giant_portal_1", "ruined_portal/giant_portal_2", "ruined_portal/giant_portal_3"
48
	};
49
	private static final float PROBABILITY_OF_GIANT_PORTAL = 0.05F;
50
	private static final int MIN_Y_INDEX = 15;
51
	private final List<RuinedPortalStructure.Setup> setups;
52
	public static final MapCodec<RuinedPortalStructure> CODEC = RecordCodecBuilder.mapCodec(
53
		i -> i.group(settingsCodec(i), ExtraCodecs.nonEmptyList(RuinedPortalStructure.Setup.CODEC.listOf()).fieldOf("setups").forGetter(s -> s.setups))
54
			.apply(i, RuinedPortalStructure::new)
55
	);
56
57
	public RuinedPortalStructure(final Structure.StructureSettings settings, final List<RuinedPortalStructure.Setup> setups) {
58
		super(settings);
59
		this.setups = setups;
60
	}
61
62
	public RuinedPortalStructure(final Structure.StructureSettings settings, final RuinedPortalStructure.Setup setup) {
63
		this(settings, List.of(setup));
64
	}
65
66
	@Override
67
	public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
68
		WorldgenRandom random = context.random();
69
		RuinedPortalStructure.Setup chosenSetup = null;
70
		if (this.setups.size() > 1) {
71
			float total = 0.0F;
72
73
			for (RuinedPortalStructure.Setup s : this.setups) {
74
				total += s.weight();
75
			}
76
77
			float pick = random.nextFloat();
78
79
			for (RuinedPortalStructure.Setup s : this.setups) {
80
				pick -= s.weight() / total;
81
				if (pick < 0.0F) {
82
					chosenSetup = s;
83
					break;
84
				}
85
			}
86
		} else {
87
			chosenSetup = this.setups.get(0);
88
		}
89
90
		if (chosenSetup == null) {
91
			throw new IllegalStateException();
92
		}
93
94
		RuinedPortalStructure.Setup setup = chosenSetup;
95
		boolean airPocket = sample(random, setup.airPocketProbability());
96
		Identifier templateLocation;
97
		if (random.nextFloat() < 0.05F) {
98
			templateLocation = Identifier.withDefaultNamespace(STRUCTURE_LOCATION_GIANT_PORTALS[random.nextInt(STRUCTURE_LOCATION_GIANT_PORTALS.length)]);
99
		} else {
100
			templateLocation = Identifier.withDefaultNamespace(STRUCTURE_LOCATION_PORTALS[random.nextInt(STRUCTURE_LOCATION_PORTALS.length)]);
101
		}
102
103
		StructureTemplate template = context.structureTemplateManager().getOrCreate(templateLocation);
104
		Rotation rotation = Util.getRandom(Rotation.values(), random);
105
		Mirror mirror = random.nextFloat() < 0.5F ? Mirror.NONE : Mirror.FRONT_BACK;
106
		BlockPos pivot = new BlockPos(template.getSize().getX() / 2, 0, template.getSize().getZ() / 2);
107
		ChunkGenerator chunkGenerator = context.chunkGenerator();
108
		LevelHeightAccessor heightAccessor = context.heightAccessor();
109
		RandomState randomState = context.randomState();
110
		BlockPos basePosition = context.chunkPos().getWorldPosition();
111
		BoundingBox boundingBox = template.getBoundingBox(basePosition, rotation, pivot, mirror);
112
		BlockPos center = boundingBox.getCenter();
113
		int surfaceY = chunkGenerator.getBaseHeight(center.getX(), center.getZ(), RuinedPortalPiece.getHeightMapType(setup.placement()), heightAccessor, randomState)
114
			- 1;
115
		int projectedY = findSuitableY(
116
			random, chunkGenerator, setup.placement(), airPocket, surfaceY, boundingBox.getYSpan(), boundingBox, heightAccessor, randomState
117
		);
118
		BlockPos origin = new BlockPos(basePosition.getX(), projectedY, basePosition.getZ());
119
		return Optional.of(
120
			new Structure.GenerationStub(
121
				origin,
122
				builder -> {
123
					RuinedPortalPiece.Properties properties = new RuinedPortalPiece.Properties(
124
						setup.canBeCold()
125
							&& isCold(
126
								origin,
127
								context.biomeResolver().getNoiseBiome(QuartPos.fromBlock(origin.getX()), QuartPos.fromBlock(origin.getY()), QuartPos.fromBlock(origin.getZ())),
128
								chunkGenerator.getSeaLevel()
129
							),
130
						setup.mossiness(),
131
						airPocket,
132
						setup.overgrown(),
133
						setup.vines(),
134
						setup.replaceWithBlackstone()
135
					);
136
					builder.addPiece(
137
						new RuinedPortalPiece(
138
							context.registryAccess(), context.structureTemplateManager(), origin, setup.placement(), properties, templateLocation, template, rotation, mirror, pivot
139
						)
140
					);
141
				}
142
			)
143
		);
144
	}
145
146
	private static boolean sample(final WorldgenRandom random, final float limit) {
147
		if (limit == 0.0F) {
148
			return false;
149
		} else {
150
			return limit == 1.0F ? true : random.nextFloat() < limit;
151
		}
152
	}
153
154
	private static boolean isCold(final BlockPos pos, final Holder<Biome> biome, final int seaLevel) {
155
		return biome.value().coldEnoughToSnow(pos, seaLevel);
156
	}
157
158
	private static int findSuitableY(
159
		final RandomSource random,
160
		final ChunkGenerator generator,
161
		final RuinedPortalPiece.VerticalPlacement verticalPlacement,
162
		final boolean airPocket,
163
		final int surfaceYAtCenter,
164
		final int ySpan,
165
		final BoundingBox boundingBox,
166
		final LevelHeightAccessor heightAccessor,
167
		final RandomState randomState
168
	) {
169
		int minY = heightAccessor.getMinY() + 15;
170
		int newY;
171
		if (verticalPlacement == RuinedPortalPiece.VerticalPlacement.IN_NETHER) {
172
			if (airPocket) {
173
				newY = Mth.randomBetweenInclusive(random, 32, 100);
174
			} else if (random.nextFloat() < 0.5F) {
175
				newY = Mth.randomBetweenInclusive(random, 27, 29);
176
			} else {
177
				newY = Mth.randomBetweenInclusive(random, 29, 100);
178
			}
179
		} else if (verticalPlacement == RuinedPortalPiece.VerticalPlacement.IN_MOUNTAIN) {
180
			int maxY = surfaceYAtCenter - ySpan;
181
			newY = getRandomWithinInterval(random, 70, maxY);
182
		} else if (verticalPlacement == RuinedPortalPiece.VerticalPlacement.UNDERGROUND) {
183
			int maxY = surfaceYAtCenter - ySpan;
184
			newY = getRandomWithinInterval(random, minY, maxY);
185
		} else if (verticalPlacement == RuinedPortalPiece.VerticalPlacement.PARTLY_BURIED) {
186
			newY = surfaceYAtCenter - ySpan + Mth.randomBetweenInclusive(random, 2, 8);
187
		} else {
188
			newY = surfaceYAtCenter;
189
		}
190
191
		List<BlockPos> bottomCorners = ImmutableList.of(
192
			new BlockPos(boundingBox.minX(), 0, boundingBox.minZ()),
193
			new BlockPos(boundingBox.maxX(), 0, boundingBox.minZ()),
194
			new BlockPos(boundingBox.minX(), 0, boundingBox.maxZ()),
195
			new BlockPos(boundingBox.maxX(), 0, boundingBox.maxZ())
196
		);
197
		List<NoiseColumn> columns = bottomCorners.stream()
198
			.map(p -> generator.getBaseColumn(p.getX(), p.getZ(), heightAccessor, randomState))
199
			.collect(Collectors.toList());
200
		Heightmap.Types heightmap = verticalPlacement == RuinedPortalPiece.VerticalPlacement.ON_OCEAN_FLOOR
201
			? Heightmap.Types.OCEAN_FLOOR_WG
202
			: Heightmap.Types.WORLD_SURFACE_WG;
203
204
		int projectedY;
205
		for (projectedY = newY; projectedY > minY; projectedY--) {
206
			int cornersOnSolidGround = 0;
207
208
			for (NoiseColumn column : columns) {
209
				BlockState blockState = column.getBlock(projectedY);
210
				if (heightmap.isOpaque().test(blockState)) {
211
					if (++cornersOnSolidGround == 3) {
212
						return projectedY;
213
					}
214
				}
215
			}
216
		}
217
218
		return projectedY;
219
	}
220
221
	private static int getRandomWithinInterval(final RandomSource random, final int minPreferred, final int max) {
222
		return minPreferred < max ? Mth.randomBetweenInclusive(random, minPreferred, max) : max;
223
	}
224
225
	@Override
226
	public StructureType<?> type() {
227
		return StructureType.RUINED_PORTAL;
228
	}
229
230
	public record Setup(
231
		RuinedPortalPiece.VerticalPlacement placement,
232
		float airPocketProbability,
233
		float mossiness,
234
		boolean overgrown,
235
		boolean vines,
236
		boolean canBeCold,
237
		boolean replaceWithBlackstone,
238
		float weight
239
	) {
240
		public static final Codec<RuinedPortalStructure.Setup> CODEC = RecordCodecBuilder.create(
241
			i -> i.group(
242
					RuinedPortalPiece.VerticalPlacement.CODEC.fieldOf("placement").forGetter(RuinedPortalStructure.Setup::placement),
243
					Codec.floatRange(0.0F, 1.0F).fieldOf("air_pocket_probability").forGetter(RuinedPortalStructure.Setup::airPocketProbability),
244
					Codec.floatRange(0.0F, 1.0F).fieldOf("mossiness").forGetter(RuinedPortalStructure.Setup::mossiness),
245
					Codec.BOOL.fieldOf("overgrown").forGetter(RuinedPortalStructure.Setup::overgrown),
246
					Codec.BOOL.fieldOf("vines").forGetter(RuinedPortalStructure.Setup::vines),
247
					Codec.BOOL.fieldOf("can_be_cold").forGetter(RuinedPortalStructure.Setup::canBeCold),
248
					Codec.BOOL.fieldOf("replace_with_blackstone").forGetter(RuinedPortalStructure.Setup::replaceWithBlackstone),
249
					ExtraCodecs.POSITIVE_FLOAT.fieldOf("weight").forGetter(RuinedPortalStructure.Setup::weight)
250
				)
251
				.apply(i, RuinedPortalStructure.Setup::new)
252
		);
253
	}
254
}
255

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