| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.ChunkPos;
|
| 10 | import net.minecraft.world.level.StructureManager;
|
| 11 | import net.minecraft.world.level.WorldGenLevel;
|
| 12 | import net.minecraft.world.level.block.Blocks;
|
| 13 | import net.minecraft.world.level.block.Rotation;
|
| 14 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 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.StructureType;
|
| 18 | import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer;
|
| 19 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 20 |
|
| 21 | public class WoodlandMansionStructure extends Structure {
|
| 22 | public static final MapCodec<WoodlandMansionStructure> CODEC = simpleCodec(WoodlandMansionStructure::new);
|
| 23 | private static final int START_OFFSET_IN_CHUNK = 7;
|
| 24 |
|
| 25 | public WoodlandMansionStructure(final Structure.StructureSettings settings) {
|
| 26 | super(settings);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 31 | ChunkPos chunkPos = context.chunkPos();
|
| 32 | int blockX = chunkPos.getBlockX(7);
|
| 33 | int blockZ = chunkPos.getBlockZ(7);
|
| 34 | if (!context.couldValidBiomeExistInTerrainColumn(blockX, blockZ)) {
|
| 35 | return Optional.empty();
|
| 36 | }
|
| 37 |
|
| 38 | Rotation rotation = Rotation.getRandom(context.random());
|
| 39 | BlockPos startPos = this.getLowestYIn5by5Box(context, blockX, blockZ, rotation);
|
| 40 | return startPos.getY() < 60
|
| 41 | ? Optional.empty()
|
| 42 | : Optional.of(new Structure.GenerationStub(startPos, builder -> this.generatePieces(builder, context, startPos, rotation)));
|
| 43 | }
|
| 44 |
|
| 45 | private void generatePieces(final StructurePiecesBuilder builder, final Structure.GenerationContext context, final BlockPos startPos, final Rotation rotation) {
|
| 46 | List<WoodlandMansionPieces.WoodlandMansionPiece> wmPieces = Lists.newLinkedList();
|
| 47 | WoodlandMansionPieces.generateMansion(context.structureTemplateManager(), startPos, rotation, wmPieces, context.random());
|
| 48 | wmPieces.forEach(builder::addPiece);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public void afterPlace(
|
| 53 | final WorldGenLevel level,
|
| 54 | final StructureManager structureManager,
|
| 55 | final ChunkGenerator generator,
|
| 56 | final RandomSource random,
|
| 57 | final BoundingBox chunkBB,
|
| 58 | final ChunkPos chunkPos,
|
| 59 | final PiecesContainer pieces
|
| 60 | ) {
|
| 61 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 62 | int minY = level.getMinY();
|
| 63 | BoundingBox boundingBox = pieces.calculateBoundingBox();
|
| 64 | int yStart = boundingBox.minY();
|
| 65 |
|
| 66 | for (int x = chunkBB.minX(); x <= chunkBB.maxX(); x++) {
|
| 67 | for (int z = chunkBB.minZ(); z <= chunkBB.maxZ(); z++) {
|
| 68 | pos.set(x, yStart, z);
|
| 69 | if (!level.isEmptyBlock(pos) && boundingBox.isInside(pos) && pieces.isInsidePiece(pos)) {
|
| 70 | for (int y = yStart - 1; y > minY; y--) {
|
| 71 | pos.setY(y);
|
| 72 | if (!level.isEmptyBlock(pos) && !level.getBlockState(pos).liquid()) {
|
| 73 | break;
|
| 74 | }
|
| 75 |
|
| 76 | level.setBlock(pos, Blocks.COBBLESTONE.defaultBlockState(), 2);
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public StructureType<?> type() {
|
| 85 | return StructureType.WOODLAND_MANSION;
|
| 86 | }
|
| 87 | }
|
| 88 |
|