| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.world.level.ChunkPos;
|
| 5 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 6 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 7 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 8 |
|
| 9 | public abstract class SinglePieceStructure extends Structure {
|
| 10 | private final SinglePieceStructure.PieceConstructor constructor;
|
| 11 | private final int width;
|
| 12 | private final int depth;
|
| 13 |
|
| 14 | protected SinglePieceStructure(
|
| 15 | final SinglePieceStructure.PieceConstructor constructor, final int width, final int depth, final Structure.StructureSettings settings
|
| 16 | ) {
|
| 17 | super(settings);
|
| 18 | this.constructor = constructor;
|
| 19 | this.width = width;
|
| 20 | this.depth = depth;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 25 | if (!context.couldValidBiomeExistOnTopOfChunkCenter()) {
|
| 26 | return Optional.empty();
|
| 27 | } else {
|
| 28 | return getLowestY(context, this.width, this.depth) < context.chunkGenerator().getSeaLevel()
|
| 29 | ? Optional.empty()
|
| 30 | : onTopOfChunkCenterWithoutBiomeCheck(context, Heightmap.Types.WORLD_SURFACE_WG, builder -> this.generatePieces(builder, context));
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | private void generatePieces(final StructurePiecesBuilder builder, final Structure.GenerationContext context) {
|
| 35 | ChunkPos chunkPos = context.chunkPos();
|
| 36 | builder.addPiece(this.constructor.construct(context.random(), chunkPos.getMinBlockX(), chunkPos.getMinBlockZ()));
|
| 37 | }
|
| 38 |
|
| 39 | @FunctionalInterface
|
| 40 | protected interface PieceConstructor {
|
| 41 | StructurePiece construct(WorldgenRandom random, int x, int z);
|
| 42 | }
|
| 43 | }
|
| 44 |
|