| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Registry;
|
| 7 | import net.minecraft.core.registries.Registries;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.nbt.ListTag;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.level.ChunkPos;
|
| 13 | import net.minecraft.world.level.StructureManager;
|
| 14 | import net.minecraft.world.level.WorldGenLevel;
|
| 15 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 16 | import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer;
|
| 17 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
|
| 18 | import net.minecraft.world.level.levelgen.structure.structures.OceanMonumentStructure;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public final class StructureStart {
|
| 23 | public static final String INVALID_START_ID = "INVALID";
|
| 24 | public static final StructureStart INVALID_START = new StructureStart(null, new ChunkPos(0, 0), 0, new PiecesContainer(List.of()));
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 | private final Structure structure;
|
| 27 | private final PiecesContainer pieceContainer;
|
| 28 | private final ChunkPos chunkPos;
|
| 29 | private int references;
|
| 30 | private volatile @Nullable BoundingBox cachedBoundingBox;
|
| 31 |
|
| 32 | public StructureStart(final Structure structure, final ChunkPos chunkPos, final int references, final PiecesContainer pieceContainer) {
|
| 33 | this.structure = structure;
|
| 34 | this.chunkPos = chunkPos;
|
| 35 | this.references = references;
|
| 36 | this.pieceContainer = pieceContainer;
|
| 37 | }
|
| 38 |
|
| 39 | public static @Nullable StructureStart loadStaticStart(final StructurePieceSerializationContext context, final CompoundTag tag, final long seed) {
|
| 40 | String id = tag.getStringOr("id", "");
|
| 41 | if ("INVALID".equals(id)) {
|
| 42 | return INVALID_START;
|
| 43 | }
|
| 44 |
|
| 45 | Registry<Structure> structuresRegistry = context.registryAccess().lookupOrThrow(Registries.STRUCTURE);
|
| 46 | Structure stucture = structuresRegistry.getValue(Identifier.parse(id));
|
| 47 | if (stucture == null) {
|
| 48 | LOGGER.error("Unknown stucture id: {}", id);
|
| 49 | return null;
|
| 50 | }
|
| 51 |
|
| 52 | ChunkPos chunkPos = new ChunkPos(tag.getIntOr("ChunkX", 0), tag.getIntOr("ChunkZ", 0));
|
| 53 | int references = tag.getIntOr("references", 0);
|
| 54 | ListTag children = tag.getListOrEmpty("Children");
|
| 55 |
|
| 56 | try {
|
| 57 | PiecesContainer pieces = PiecesContainer.load(children, context);
|
| 58 | if (stucture instanceof OceanMonumentStructure) {
|
| 59 | pieces = OceanMonumentStructure.regeneratePiecesAfterLoad(chunkPos, seed, pieces);
|
| 60 | }
|
| 61 |
|
| 62 | return new StructureStart(stucture, chunkPos, references, pieces);
|
| 63 | } catch (Exception e) {
|
| 64 | LOGGER.error("Failed Start with id {}", id, e);
|
| 65 | return null;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public BoundingBox getBoundingBox() {
|
| 70 | BoundingBox boundingBox = this.cachedBoundingBox;
|
| 71 | if (boundingBox == null) {
|
| 72 | boundingBox = this.structure.adjustBoundingBox(this.pieceContainer.calculateBoundingBox());
|
| 73 | this.cachedBoundingBox = boundingBox;
|
| 74 | }
|
| 75 |
|
| 76 | return boundingBox;
|
| 77 | }
|
| 78 |
|
| 79 | public void placeInChunk(
|
| 80 | final WorldGenLevel level,
|
| 81 | final StructureManager structureManager,
|
| 82 | final ChunkGenerator generator,
|
| 83 | final RandomSource random,
|
| 84 | final BoundingBox chunkBB,
|
| 85 | final ChunkPos chunkPos
|
| 86 | ) {
|
| 87 | List<StructurePiece> pieces = this.pieceContainer.pieces();
|
| 88 | if (!pieces.isEmpty()) {
|
| 89 | BoundingBox centerBB = pieces.get(0).boundingBox;
|
| 90 | BlockPos centerPos = centerBB.getCenter();
|
| 91 | BlockPos referencePos = new BlockPos(centerPos.getX(), centerBB.minY(), centerPos.getZ());
|
| 92 |
|
| 93 | for (StructurePiece next : pieces) {
|
| 94 | if (next.getBoundingBox().intersects(chunkBB)) {
|
| 95 | next.postProcess(level, structureManager, generator, random, chunkBB, chunkPos, referencePos);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | this.structure.afterPlace(level, structureManager, generator, random, chunkBB, chunkPos, this.pieceContainer);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public CompoundTag createTag(final StructurePieceSerializationContext context, final ChunkPos chunkPos) {
|
| 104 | CompoundTag tag = new CompoundTag();
|
| 105 | if (this.isValid()) {
|
| 106 | tag.putString("id", context.registryAccess().lookupOrThrow(Registries.STRUCTURE).getKey(this.structure).toString());
|
| 107 | tag.putInt("ChunkX", chunkPos.x());
|
| 108 | tag.putInt("ChunkZ", chunkPos.z());
|
| 109 | tag.putInt("references", this.references);
|
| 110 | tag.put("Children", this.pieceContainer.save(context));
|
| 111 | return tag;
|
| 112 | } else {
|
| 113 | tag.putString("id", "INVALID");
|
| 114 | return tag;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | public boolean isValid() {
|
| 119 | return !this.pieceContainer.isEmpty();
|
| 120 | }
|
| 121 |
|
| 122 | public ChunkPos getChunkPos() {
|
| 123 | return this.chunkPos;
|
| 124 | }
|
| 125 |
|
| 126 | public boolean canBeReferenced() {
|
| 127 | return this.references < this.getMaxReferences();
|
| 128 | }
|
| 129 |
|
| 130 | public void addReference() {
|
| 131 | this.references++;
|
| 132 | }
|
| 133 |
|
| 134 | public int getReferences() {
|
| 135 | return this.references;
|
| 136 | }
|
| 137 |
|
| 138 | protected int getMaxReferences() {
|
| 139 | return 1;
|
| 140 | }
|
| 141 |
|
| 142 | public Structure getStructure() {
|
| 143 | return this.structure;
|
| 144 | }
|
| 145 |
|
| 146 | public List<StructurePiece> getPieces() {
|
| 147 | return this.pieceContainer.pieces();
|
| 148 | }
|
| 149 | }
|
| 150 |
|