PiecesContainer .java

net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer (record, 85 lines): members: pieces, JIGSAW_RENAME, isInsidePiece, RENAMES, load, save.

net.minecraft.world.level.levelgen.structure.piecesrecord85 linescommon2.8 KB

API Surface (6)

record PiecesContainer(List<StructurePiece> pieces)
boolean isEmpty()
boolean isInsidePiece(final BlockPos startPos)
Tag save(final StructurePieceSerializationContext context)
PiecesContainer load(final ListTag children, final StructurePieceSerializationContext context)
BoundingBox calculateBoundingBox()

Fields & Constants (3)

LOGGER
JIGSAW_RENAME
RENAMES

String literals (3)

"jigsaw"
"bastionremnant"
"runtime"

Full Source

PiecesContainer.java85 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.structure.pieces;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.Lists;
5
import com.mojang.logging.LogUtils;
6
import java.util.List;
7
import java.util.Locale;
8
import java.util.Map;
9
import net.minecraft.core.BlockPos;
10
import net.minecraft.core.registries.BuiltInRegistries;
11
import net.minecraft.nbt.CompoundTag;
12
import net.minecraft.nbt.ListTag;
13
import net.minecraft.nbt.Tag;
14
import net.minecraft.resources.Identifier;
15
import net.minecraft.world.level.levelgen.structure.BoundingBox;
16
import net.minecraft.world.level.levelgen.structure.StructurePiece;
17
import org.slf4j.Logger;
18
19
public record PiecesContainer(List<StructurePiece> pieces) {
20
	private static final Logger LOGGER = LogUtils.getLogger();
21
	private static final Identifier JIGSAW_RENAME = Identifier.withDefaultNamespace("jigsaw");
22
	private static final Map<Identifier, Identifier> RENAMES = ImmutableMap.<Identifier, Identifier>builder()
23
		.put(Identifier.withDefaultNamespace("nvi"), JIGSAW_RENAME)
24
		.put(Identifier.withDefaultNamespace("pcp"), JIGSAW_RENAME)
25
		.put(Identifier.withDefaultNamespace("bastionremnant"), JIGSAW_RENAME)
26
		.put(Identifier.withDefaultNamespace("runtime"), JIGSAW_RENAME)
27
		.build();
28
29
	public PiecesContainer(final List<StructurePiece> pieces) {
30
		this.pieces = List.copyOf(pieces);
31
	}
32
33
	public boolean isEmpty() {
34
		return this.pieces.isEmpty();
35
	}
36
37
	public boolean isInsidePiece(final BlockPos startPos) {
38
		for (StructurePiece piece : this.pieces) {
39
			if (piece.getBoundingBox().isInside(startPos)) {
40
				return true;
41
			}
42
		}
43
44
		return false;
45
	}
46
47
	public Tag save(final StructurePieceSerializationContext context) {
48
		ListTag childrenTags = new ListTag();
49
50
		for (StructurePiece piece : this.pieces) {
51
			childrenTags.add(piece.createTag(context));
52
		}
53
54
		return childrenTags;
55
	}
56
57
	public static PiecesContainer load(final ListTag children, final StructurePieceSerializationContext context) {
58
		List<StructurePiece> pieces = Lists.newArrayList();
59
60
		for (int i = 0; i < children.size(); i++) {
61
			CompoundTag pieceTag = children.getCompoundOrEmpty(i);
62
			String oldId = pieceTag.getStringOr("id", "").toLowerCase(Locale.ROOT);
63
			Identifier oldPieceKey = Identifier.parse(oldId);
64
			Identifier pieceId = RENAMES.getOrDefault(oldPieceKey, oldPieceKey);
65
			StructurePieceType pieceType = BuiltInRegistries.STRUCTURE_PIECE.getValue(pieceId);
66
			if (pieceType == null) {
67
				LOGGER.error("Unknown structure piece id: {}", pieceId);
68
			} else {
69
				try {
70
					StructurePiece piece = pieceType.load(context, pieceTag);
71
					pieces.add(piece);
72
				} catch (Exception e) {
73
					LOGGER.error("Exception loading structure piece with id {}", pieceId, e);
74
				}
75
			}
76
		}
77
78
		return new PiecesContainer(pieces);
79
	}
80
81
	public BoundingBox calculateBoundingBox() {
82
		return StructurePiece.createBoundingBox(this.pieces.stream());
83
	}
84
}
85

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