| 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 |
|