| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import java.util.Objects;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.tags.BiomeTags;
|
| 9 | import net.minecraft.world.level.ChunkPos;
|
| 10 | import net.minecraft.world.level.biome.Biome;
|
| 11 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 12 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 13 | import net.minecraft.world.level.levelgen.RandomSupport;
|
| 14 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 15 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 16 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 17 | import net.minecraft.world.level.levelgen.structure.StructurePiece;
|
| 18 | import net.minecraft.world.level.levelgen.structure.StructureType;
|
| 19 | import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer;
|
| 20 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 21 |
|
| 22 | public class OceanMonumentStructure extends Structure {
|
| 23 | public static final MapCodec<OceanMonumentStructure> CODEC = simpleCodec(OceanMonumentStructure::new);
|
| 24 |
|
| 25 | public OceanMonumentStructure(final Structure.StructureSettings settings) {
|
| 26 | super(settings);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 31 | int offsetX = context.chunkPos().getBlockX(9);
|
| 32 | int offsetZ = context.chunkPos().getBlockZ(9);
|
| 33 |
|
| 34 | for (Holder<Biome> biome : context.biomeResolver().getBiomesWithin(offsetX, context.chunkGenerator().getSeaLevel(), offsetZ, 29)) {
|
| 35 | if (!biome.is(BiomeTags.REQUIRED_OCEAN_MONUMENT_SURROUNDING)) {
|
| 36 | return Optional.empty();
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | return onTopOfChunkCenter(context, Heightmap.Types.OCEAN_FLOOR_WG, builder -> generatePieces(builder, context));
|
| 41 | }
|
| 42 |
|
| 43 | private static StructurePiece createTopPiece(final ChunkPos chunkPos, final WorldgenRandom random) {
|
| 44 | int west = chunkPos.getMinBlockX() - 29;
|
| 45 | int north = chunkPos.getMinBlockZ() - 29;
|
| 46 | Direction orientation = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 47 | return new OceanMonumentPieces.MonumentBuilding(random, west, north, orientation);
|
| 48 | }
|
| 49 |
|
| 50 | private static void generatePieces(final StructurePiecesBuilder builder, final Structure.GenerationContext context) {
|
| 51 | builder.addPiece(createTopPiece(context.chunkPos(), context.random()));
|
| 52 | }
|
| 53 |
|
| 54 | public static PiecesContainer regeneratePiecesAfterLoad(final ChunkPos chunkPos, final long seed, final PiecesContainer savedPieces) {
|
| 55 | if (savedPieces.isEmpty()) {
|
| 56 | return savedPieces;
|
| 57 | }
|
| 58 |
|
| 59 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(RandomSupport.generateUniqueSeed()));
|
| 60 | random.setLargeFeatureSeed(seed, chunkPos.x(), chunkPos.z());
|
| 61 | StructurePiece oldTopPiece = savedPieces.pieces().get(0);
|
| 62 | BoundingBox oldBoundingBox = oldTopPiece.getBoundingBox();
|
| 63 | int west = oldBoundingBox.minX();
|
| 64 | int north = oldBoundingBox.minZ();
|
| 65 | Direction defaultOrientation = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 66 | Direction orientation = Objects.requireNonNullElse(oldTopPiece.getOrientation(), defaultOrientation);
|
| 67 | StructurePiece topPiece = new OceanMonumentPieces.MonumentBuilding(random, west, north, orientation);
|
| 68 | StructurePiecesBuilder result = new StructurePiecesBuilder();
|
| 69 | result.addPiece(topPiece);
|
| 70 | return result.build();
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public StructureType<?> type() {
|
| 75 | return StructureType.OCEAN_MONUMENT;
|
| 76 | }
|
| 77 | }
|
| 78 |
|