| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.world.level.block.Rotation;
|
| 9 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 10 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 11 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 12 | import net.minecraft.world.level.levelgen.structure.StructureType;
|
| 13 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 14 |
|
| 15 | public class ShipwreckStructure extends Structure {
|
| 16 | public static final MapCodec<ShipwreckStructure> CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(settingsCodec(i), Codec.BOOL.fieldOf("is_beached").forGetter(s -> s.isBeached)).apply(i, ShipwreckStructure::new)
|
| 18 | );
|
| 19 | public final boolean isBeached;
|
| 20 |
|
| 21 | public ShipwreckStructure(final Structure.StructureSettings settings, final boolean isBeached) {
|
| 22 | super(settings);
|
| 23 | this.isBeached = isBeached;
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 28 | Heightmap.Types type = this.isBeached ? Heightmap.Types.WORLD_SURFACE_WG : Heightmap.Types.OCEAN_FLOOR_WG;
|
| 29 | return onTopOfChunkCenter(context, type, builder -> this.generatePieces(builder, context));
|
| 30 | }
|
| 31 |
|
| 32 | private void generatePieces(final StructurePiecesBuilder builder, final Structure.GenerationContext context) {
|
| 33 | Rotation rotation = Rotation.getRandom(context.random());
|
| 34 | BlockPos offset = new BlockPos(context.chunkPos().getMinBlockX(), 90, context.chunkPos().getMinBlockZ());
|
| 35 | ShipwreckPieces.ShipwreckPiece piece = ShipwreckPieces.addRandomPiece(
|
| 36 | context.structureTemplateManager(), offset, rotation, builder, context.random(), this.isBeached
|
| 37 | );
|
| 38 | if (piece.isTooBigToFitInWorldGenRegion()) {
|
| 39 | BoundingBox bb = piece.getBoundingBox();
|
| 40 | int height;
|
| 41 | if (this.isBeached) {
|
| 42 | int minY = Structure.getLowestY(context, bb.minX(), bb.getXSpan(), bb.minZ(), bb.getZSpan());
|
| 43 | height = piece.calculateBeachedPosition(minY, context.random());
|
| 44 | } else {
|
| 45 | height = Structure.getMeanFirstOccupiedHeight(context, bb.minX(), bb.getXSpan(), bb.minZ(), bb.getZSpan());
|
| 46 | }
|
| 47 |
|
| 48 | piece.adjustPositionHeight(height);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public StructureType<?> type() {
|
| 54 | return StructureType.SHIPWRECK;
|
| 55 | }
|
| 56 | }
|
| 57 |
|