EndCityPieces .java

net.minecraft.world.level.levelgen.structure.structures.EndCityPieces (class, 421 lines): members: TOWER_BRIDGE_GENERATOR, HOUSE_TOWER_GENERATOR, genDepth.

net.minecraft.world.level.levelgen.structure.structuresclass421 linescommon16.7 KB

API Surface (12)

void init()
boolean generate(final StructureTemplateManager structureTemplateManager, final int genDepth, final EndCityPieces.EndCityPiece parent, final BlockPos offset, final List<StructurePiece> pieces, final RandomSource random)
void init()
boolean generate(final StructureTemplateManager structureTemplateManager, final int genDepth, final EndCityPieces.EndCityPiece parent, final BlockPos offset, final List<StructurePiece> pieces, final RandomSource random)
void init()
boolean generate(final StructureTemplateManager structureTemplateManager, final int genDepth, final EndCityPieces.EndCityPiece parent, final BlockPos offset, final List<StructurePiece> pieces, final RandomSource random)
void init()
boolean generate(final StructureTemplateManager structureTemplateManager, final int genDepth, final EndCityPieces.EndCityPiece parent, final BlockPos offset, final List<StructurePiece> pieces, final RandomSource random)
void startHouseTower(final StructureTemplateManager structureTemplateManager, final BlockPos origin, final Rotation rotation, final List<StructurePiece> pieces, final RandomSource random)
Identifier makeTemplateLocation()
void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag)
void handleDataMarker(final String markerId, final BlockPos position, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB)

Fields & Constants (16)

MAX_GEN_DEPTH
HOUSE_TOWER_GENERATOR
TOWER_BRIDGES
TOWER_GENERATOR
TOWER_BRIDGE_GENERATOR
FAT_TOWER_BRIDGES
FAT_TOWER_GENERATOR
structureTemplateManager
parent
offset
templateName
rotation
origin
pieces
generator
genDepth

String literals (23)

"base_floor"
"base_roof"
"second_floor_2"
"second_roof"
"third_floor_2"
"third_roof"
"tower_base"
"tower_piece"
"bridge_end"
"tower_top"
"bridge_piece"
"bridge_steep_stairs"
"bridge_gentle_stairs"
"ship"
"fat_tower_base"
"fat_tower_middle"
"fat_tower_top"
"second_floor_1"
"third_floor_1"
"end_city/"
"Chest"
"Sentry"
"Elytra"

Full Source

EndCityPieces.java421 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.structure.structures;
2
3
import com.mojang.datafixers.util.Pair;
4
import java.util.ArrayList;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.nbt.CompoundTag;
9
import net.minecraft.resources.Identifier;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.world.RandomizableContainer;
12
import net.minecraft.world.entity.EntitySpawnReason;
13
import net.minecraft.world.entity.EntityTypes;
14
import net.minecraft.world.entity.decoration.ItemFrame;
15
import net.minecraft.world.entity.monster.Shulker;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.Items;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.level.ServerLevelAccessor;
20
import net.minecraft.world.level.block.Rotation;
21
import net.minecraft.world.level.levelgen.structure.BoundingBox;
22
import net.minecraft.world.level.levelgen.structure.StructurePiece;
23
import net.minecraft.world.level.levelgen.structure.TemplateStructurePiece;
24
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
25
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType;
26
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
27
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
28
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
29
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
30
31
public class EndCityPieces {
32
	private static final int MAX_GEN_DEPTH = 8;
33
	private static final EndCityPieces.SectionGenerator HOUSE_TOWER_GENERATOR = new EndCityPieces.SectionGenerator() {
34
		@Override
35
		public void init() {
36
		}
37
38
		@Override
39
		public boolean generate(
40
			final StructureTemplateManager structureTemplateManager,
41
			final int genDepth,
42
			final EndCityPieces.EndCityPiece parent,
43
			final BlockPos offset,
44
			final List<StructurePiece> pieces,
45
			final RandomSource random
46
		) {
47
			if (genDepth > 8) {
48
				return false;
49
			}
50
51
			Rotation rotation = parent.placeSettings().getRotation();
52
			EndCityPieces.EndCityPiece lastPiece = EndCityPieces.addHelper(
53
				pieces, EndCityPieces.addPiece(structureTemplateManager, parent, offset, "base_floor", rotation, true)
54
			);
55
			int numFloors = random.nextInt(3);
56
			if (numFloors == 0) {
57
				lastPiece = EndCityPieces.addHelper(
58
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 4, -1), "base_roof", rotation, true)
59
				);
60
			} else if (numFloors == 1) {
61
				lastPiece = EndCityPieces.addHelper(
62
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 0, -1), "second_floor_2", rotation, false)
63
				);
64
				lastPiece = EndCityPieces.addHelper(
65
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 8, -1), "second_roof", rotation, false)
66
				);
67
				EndCityPieces.recursiveChildren(structureTemplateManager, EndCityPieces.TOWER_GENERATOR, genDepth + 1, lastPiece, null, pieces, random);
68
			} else if (numFloors == 2) {
69
				lastPiece = EndCityPieces.addHelper(
70
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 0, -1), "second_floor_2", rotation, false)
71
				);
72
				lastPiece = EndCityPieces.addHelper(
73
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 4, -1), "third_floor_2", rotation, false)
74
				);
75
				lastPiece = EndCityPieces.addHelper(
76
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 8, -1), "third_roof", rotation, true)
77
				);
78
				EndCityPieces.recursiveChildren(structureTemplateManager, EndCityPieces.TOWER_GENERATOR, genDepth + 1, lastPiece, null, pieces, random);
79
			}
80
81
			return true;
82
		}
83
	};
84
	private static final List<Pair<Rotation, BlockPos>> TOWER_BRIDGES = List.of(
85
		Pair.of(Rotation.NONE, new BlockPos(1, -1, 0)),
86
		Pair.of(Rotation.CLOCKWISE_90, new BlockPos(6, -1, 1)),
87
		Pair.of(Rotation.COUNTERCLOCKWISE_90, new BlockPos(0, -1, 5)),
88
		Pair.of(Rotation.CLOCKWISE_180, new BlockPos(5, -1, 6))
89
	);
90
	private static final EndCityPieces.SectionGenerator TOWER_GENERATOR = new EndCityPieces.SectionGenerator() {
91
		@Override
92
		public void init() {
93
		}
94
95
		@Override
96
		public boolean generate(
97
			final StructureTemplateManager structureTemplateManager,
98
			final int genDepth,
99
			final EndCityPieces.EndCityPiece parent,
100
			final BlockPos offset,
101
			final List<StructurePiece> pieces,
102
			final RandomSource random
103
		) {
104
			Rotation rotation = parent.placeSettings().getRotation();
105
			EndCityPieces.EndCityPiece lastPiece = parent;
106
			lastPiece = EndCityPieces.addHelper(
107
				pieces,
108
				EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(3 + random.nextInt(2), -3, 3 + random.nextInt(2)), "tower_base", rotation, true)
109
			);
110
			lastPiece = EndCityPieces.addHelper(
111
				pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, 7, 0), "tower_piece", rotation, true)
112
			);
113
			EndCityPieces.EndCityPiece bridgePiece = random.nextInt(3) == 0 ? lastPiece : null;
114
			int towerHeight = 1 + random.nextInt(3);
115
116
			for (int i = 0; i < towerHeight; i++) {
117
				lastPiece = EndCityPieces.addHelper(
118
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, 4, 0), "tower_piece", rotation, true)
119
				);
120
				if (i < towerHeight - 1 && random.nextBoolean()) {
121
					bridgePiece = lastPiece;
122
				}
123
			}
124
125
			if (bridgePiece != null) {
126
				for (Pair<Rotation, BlockPos> bridge : EndCityPieces.TOWER_BRIDGES) {
127
					if (random.nextBoolean()) {
128
						EndCityPieces.EndCityPiece bridgeStart = EndCityPieces.addHelper(
129
							pieces, EndCityPieces.addPiece(structureTemplateManager, bridgePiece, bridge.getSecond(), "bridge_end", rotation.getRotated(bridge.getFirst()), true)
130
						);
131
						EndCityPieces.recursiveChildren(structureTemplateManager, EndCityPieces.TOWER_BRIDGE_GENERATOR, genDepth + 1, bridgeStart, null, pieces, random);
132
					}
133
				}
134
135
				lastPiece = EndCityPieces.addHelper(
136
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 4, -1), "tower_top", rotation, true)
137
				);
138
			} else {
139
				if (genDepth != 7) {
140
					return EndCityPieces.recursiveChildren(structureTemplateManager, EndCityPieces.FAT_TOWER_GENERATOR, genDepth + 1, lastPiece, null, pieces, random);
141
				}
142
143
				lastPiece = EndCityPieces.addHelper(
144
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 4, -1), "tower_top", rotation, true)
145
				);
146
			}
147
148
			return true;
149
		}
150
	};
151
	private static final EndCityPieces.SectionGenerator TOWER_BRIDGE_GENERATOR = new EndCityPieces.SectionGenerator() {
152
		public boolean shipCreated;
153
154
		@Override
155
		public void init() {
156
			this.shipCreated = false;
157
		}
158
159
		@Override
160
		public boolean generate(
161
			final StructureTemplateManager structureTemplateManager,
162
			final int genDepth,
163
			final EndCityPieces.EndCityPiece parent,
164
			final BlockPos offset,
165
			final List<StructurePiece> pieces,
166
			final RandomSource random
167
		) {
168
			Rotation rotation = parent.placeSettings().getRotation();
169
			int bridgeLength = random.nextInt(4) + 1;
170
			EndCityPieces.EndCityPiece lastPiece = EndCityPieces.addHelper(
171
				pieces, EndCityPieces.addPiece(structureTemplateManager, parent, new BlockPos(0, 0, -4), "bridge_piece", rotation, true)
172
			);
173
			lastPiece.setGenDepth(-1);
174
			int nextY = 0;
175
176
			for (int i = 0; i < bridgeLength; i++) {
177
				if (random.nextBoolean()) {
178
					lastPiece = EndCityPieces.addHelper(
179
						pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, nextY, -4), "bridge_piece", rotation, true)
180
					);
181
					nextY = 0;
182
				} else {
183
					if (random.nextBoolean()) {
184
						lastPiece = EndCityPieces.addHelper(
185
							pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, nextY, -4), "bridge_steep_stairs", rotation, true)
186
						);
187
					} else {
188
						lastPiece = EndCityPieces.addHelper(
189
							pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, nextY, -8), "bridge_gentle_stairs", rotation, true)
190
						);
191
					}
192
193
					nextY = 4;
194
				}
195
			}
196
197
			if (!this.shipCreated && random.nextInt(10 - genDepth) == 0) {
198
				EndCityPieces.addHelper(
199
					pieces,
200
					EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-8 + random.nextInt(8), nextY, -70 + random.nextInt(10)), "ship", rotation, true)
201
				);
202
				this.shipCreated = true;
203
			} else if (!EndCityPieces.recursiveChildren(
204
				structureTemplateManager, EndCityPieces.HOUSE_TOWER_GENERATOR, genDepth + 1, lastPiece, new BlockPos(-3, nextY + 1, -11), pieces, random
205
			)) {
206
				return false;
207
			}
208
209
			lastPiece = EndCityPieces.addHelper(
210
				pieces,
211
				EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(4, nextY, 0), "bridge_end", rotation.getRotated(Rotation.CLOCKWISE_180), true)
212
			);
213
			lastPiece.setGenDepth(-1);
214
			return true;
215
		}
216
	};
217
	private static final List<Pair<Rotation, BlockPos>> FAT_TOWER_BRIDGES = List.of(
218
		Pair.of(Rotation.NONE, new BlockPos(4, -1, 0)),
219
		Pair.of(Rotation.CLOCKWISE_90, new BlockPos(12, -1, 4)),
220
		Pair.of(Rotation.COUNTERCLOCKWISE_90, new BlockPos(0, -1, 8)),
221
		Pair.of(Rotation.CLOCKWISE_180, new BlockPos(8, -1, 12))
222
	);
223
	private static final EndCityPieces.SectionGenerator FAT_TOWER_GENERATOR = new EndCityPieces.SectionGenerator() {
224
		@Override
225
		public void init() {
226
		}
227
228
		@Override
229
		public boolean generate(
230
			final StructureTemplateManager structureTemplateManager,
231
			final int genDepth,
232
			final EndCityPieces.EndCityPiece parent,
233
			final BlockPos offset,
234
			final List<StructurePiece> pieces,
235
			final RandomSource random
236
		) {
237
			Rotation rotation = parent.placeSettings().getRotation();
238
			EndCityPieces.EndCityPiece lastPiece = EndCityPieces.addHelper(
239
				pieces, EndCityPieces.addPiece(structureTemplateManager, parent, new BlockPos(-3, 4, -3), "fat_tower_base", rotation, true)
240
			);
241
			lastPiece = EndCityPieces.addHelper(
242
				pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, 4, 0), "fat_tower_middle", rotation, true)
243
			);
244
245
			for (int i = 0; i < 2 && random.nextInt(3) != 0; i++) {
246
				lastPiece = EndCityPieces.addHelper(
247
					pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(0, 8, 0), "fat_tower_middle", rotation, true)
248
				);
249
250
				for (Pair<Rotation, BlockPos> bridge : EndCityPieces.FAT_TOWER_BRIDGES) {
251
					if (random.nextBoolean()) {
252
						EndCityPieces.EndCityPiece bridgeStart = EndCityPieces.addHelper(
253
							pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, bridge.getSecond(), "bridge_end", rotation.getRotated(bridge.getFirst()), true)
254
						);
255
						EndCityPieces.recursiveChildren(structureTemplateManager, EndCityPieces.TOWER_BRIDGE_GENERATOR, genDepth + 1, bridgeStart, null, pieces, random);
256
					}
257
				}
258
			}
259
260
			lastPiece = EndCityPieces.addHelper(
261
				pieces, EndCityPieces.addPiece(structureTemplateManager, lastPiece, new BlockPos(-2, 8, -2), "fat_tower_top", rotation, true)
262
			);
263
			return true;
264
		}
265
	};
266
267
	private static EndCityPieces.EndCityPiece addPiece(
268
		final StructureTemplateManager structureTemplateManager,
269
		final EndCityPieces.EndCityPiece parent,
270
		final BlockPos offset,
271
		final String templateName,
272
		final Rotation rotation,
273
		final boolean overwrite
274
	) {
275
		EndCityPieces.EndCityPiece child = new EndCityPieces.EndCityPiece(structureTemplateManager, templateName, parent.templatePosition(), rotation, overwrite);
276
		BlockPos origin = parent.template().calculateConnectedPosition(parent.placeSettings(), offset, child.placeSettings(), BlockPos.ZERO);
277
		child.move(origin.getX(), origin.getY(), origin.getZ());
278
		return child;
279
	}
280
281
	public static void startHouseTower(
282
		final StructureTemplateManager structureTemplateManager,
283
		final BlockPos origin,
284
		final Rotation rotation,
285
		final List<StructurePiece> pieces,
286
		final RandomSource random
287
	) {
288
		FAT_TOWER_GENERATOR.init();
289
		HOUSE_TOWER_GENERATOR.init();
290
		TOWER_BRIDGE_GENERATOR.init();
291
		TOWER_GENERATOR.init();
292
		EndCityPieces.EndCityPiece lastPiece = addHelper(pieces, new EndCityPieces.EndCityPiece(structureTemplateManager, "base_floor", origin, rotation, true));
293
		lastPiece = addHelper(pieces, addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 0, -1), "second_floor_1", rotation, false));
294
		lastPiece = addHelper(pieces, addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 4, -1), "third_floor_1", rotation, false));
295
		lastPiece = addHelper(pieces, addPiece(structureTemplateManager, lastPiece, new BlockPos(-1, 8, -1), "third_roof", rotation, true));
296
		recursiveChildren(structureTemplateManager, TOWER_GENERATOR, 1, lastPiece, null, pieces, random);
297
	}
298
299
	private static EndCityPieces.EndCityPiece addHelper(final List<StructurePiece> pieces, final EndCityPieces.EndCityPiece piece) {
300
		pieces.add(piece);
301
		return piece;
302
	}
303
304
	private static boolean recursiveChildren(
305
		final StructureTemplateManager structureTemplateManager,
306
		final EndCityPieces.SectionGenerator generator,
307
		final int genDepth,
308
		final EndCityPieces.EndCityPiece parent,
309
		final BlockPos offset,
310
		final List<StructurePiece> pieces,
311
		final RandomSource random
312
	) {
313
		if (genDepth > 8) {
314
			return false;
315
		}
316
317
		List<StructurePiece> childPieces = new ArrayList<>();
318
		if (generator.generate(structureTemplateManager, genDepth, parent, offset, childPieces, random)) {
319
			boolean collision = false;
320
			int childTag = random.nextInt();
321
322
			for (StructurePiece child : childPieces) {
323
				child.setGenDepth(childTag);
324
				StructurePiece collisionPiece = StructurePiece.findCollisionPiece(pieces, child.getBoundingBox());
325
				if (collisionPiece != null && collisionPiece.getGenDepth() != parent.getGenDepth()) {
326
					collision = true;
327
					break;
328
				}
329
			}
330
331
			if (!collision) {
332
				pieces.addAll(childPieces);
333
				return true;
334
			}
335
		}
336
337
		return false;
338
	}
339
340
	public static class EndCityPiece extends TemplateStructurePiece {
341
		public EndCityPiece(
342
			final StructureTemplateManager structureTemplateManager,
343
			final String templateName,
344
			final BlockPos position,
345
			final Rotation rotation,
346
			final boolean overwrite
347
		) {
348
			super(
349
				StructurePieceType.END_CITY_PIECE, 0, structureTemplateManager, makeIdentifier(templateName), templateName, makeSettings(overwrite, rotation), position
350
			);
351
		}
352
353
		public EndCityPiece(final StructureTemplateManager structureTemplateManager, final CompoundTag tag) {
354
			super(
355
				StructurePieceType.END_CITY_PIECE,
356
				tag,
357
				structureTemplateManager,
358
				location -> makeSettings(tag.getBooleanOr("OW", false), tag.read("Rot", Rotation.LEGACY_CODEC).orElseThrow())
359
			);
360
		}
361
362
		private static StructurePlaceSettings makeSettings(final boolean overwrite, final Rotation rotation) {
363
			BlockIgnoreProcessor processor = overwrite ? BlockIgnoreProcessor.STRUCTURE_BLOCK : BlockIgnoreProcessor.STRUCTURE_AND_AIR;
364
			return new StructurePlaceSettings().setIgnoreEntities(true).addProcessor(processor).setRotation(rotation);
365
		}
366
367
		@Override
368
		protected Identifier makeTemplateLocation() {
369
			return makeIdentifier(this.templateName);
370
		}
371
372
		private static Identifier makeIdentifier(final String templateName) {
373
			return Identifier.withDefaultNamespace("end_city/" + templateName);
374
		}
375
376
		@Override
377
		protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
378
			super.addAdditionalSaveData(context, tag);
379
			tag.store("Rot", Rotation.LEGACY_CODEC, this.placeSettings.getRotation());
380
			tag.putBoolean("OW", this.placeSettings.getProcessors().get(0) == BlockIgnoreProcessor.STRUCTURE_BLOCK);
381
		}
382
383
		@Override
384
		protected void handleDataMarker(
385
			final String markerId, final BlockPos position, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB
386
		) {
387
			if (markerId.startsWith("Chest")) {
388
				BlockPos chestPosition = position.below();
389
				if (chunkBB.isInside(chestPosition)) {
390
					RandomizableContainer.setBlockEntityLootTable(level, random, chestPosition, BuiltInLootTables.END_CITY_TREASURE);
391
				}
392
			} else if (chunkBB.isInside(position) && Level.isInSpawnableBounds(position)) {
393
				if (markerId.startsWith("Sentry")) {
394
					Shulker sentry = EntityTypes.SHULKER.create(level.getLevel(), EntitySpawnReason.STRUCTURE);
395
					if (sentry != null) {
396
						sentry.setPos(position.getX() + 0.5, position.getY(), position.getZ() + 0.5);
397
						level.addFreshEntity(sentry);
398
					}
399
				} else if (markerId.startsWith("Elytra")) {
400
					ItemFrame itemFrame = new ItemFrame(level.getLevel(), position, this.placeSettings.getRotation().rotate(Direction.SOUTH));
401
					itemFrame.setItem(new ItemStack(Items.ELYTRA), false);
402
					level.addFreshEntity(itemFrame);
403
				}
404
			}
405
		}
406
	}
407
408
	private interface SectionGenerator {
409
		void init();
410
411
		boolean generate(
412
			StructureTemplateManager structureTemplateManager,
413
			int genDepth,
414
			EndCityPieces.EndCityPiece parent,
415
			BlockPos offset,
416
			List<StructurePiece> pieces,
417
			RandomSource random
418
		);
419
	}
420
}
421

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