| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.IntFunction;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.util.ByIdMap;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.StringRepresentable;
|
| 13 | import net.minecraft.world.level.ChunkPos;
|
| 14 | import net.minecraft.world.level.block.Block;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 18 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 19 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 20 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 21 | import net.minecraft.world.level.levelgen.structure.StructureType;
|
| 22 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 23 |
|
| 24 | public class MineshaftStructure extends Structure {
|
| 25 | public static final MapCodec<MineshaftStructure> CODEC = RecordCodecBuilder.mapCodec(
|
| 26 | i -> i.group(settingsCodec(i), MineshaftStructure.Type.CODEC.fieldOf("mineshaft_type").forGetter(c -> c.type)).apply(i, MineshaftStructure::new)
|
| 27 | );
|
| 28 | private final MineshaftStructure.Type type;
|
| 29 |
|
| 30 | public MineshaftStructure(final Structure.StructureSettings settings, final MineshaftStructure.Type type) {
|
| 31 | super(settings);
|
| 32 | this.type = type;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 37 | context.random().nextDouble();
|
| 38 | ChunkPos chunkPos = context.chunkPos();
|
| 39 | BlockPos startPos = new BlockPos(chunkPos.getMiddleBlockX(), 50, chunkPos.getMinBlockZ());
|
| 40 | StructurePiecesBuilder mineshaftPiecesBuilder = new StructurePiecesBuilder();
|
| 41 | int yOffset = this.generatePiecesAndAdjust(mineshaftPiecesBuilder, context);
|
| 42 | return Optional.of(new Structure.GenerationStub(startPos.offset(0, yOffset, 0), Either.right(mineshaftPiecesBuilder)));
|
| 43 | }
|
| 44 |
|
| 45 | private int generatePiecesAndAdjust(final StructurePiecesBuilder builder, final Structure.GenerationContext context) {
|
| 46 | ChunkPos chunkPos = context.chunkPos();
|
| 47 | WorldgenRandom random = context.random();
|
| 48 | ChunkGenerator chunkGenerator = context.chunkGenerator();
|
| 49 | MineshaftPieces.MineShaftRoom mineShaftRoom = new MineshaftPieces.MineShaftRoom(0, random, chunkPos.getBlockX(2), chunkPos.getBlockZ(2), this.type);
|
| 50 | builder.addPiece(mineShaftRoom);
|
| 51 | mineShaftRoom.addChildren(mineShaftRoom, builder, random);
|
| 52 | int seaLevel = chunkGenerator.getSeaLevel();
|
| 53 | if (this.type == MineshaftStructure.Type.MESA) {
|
| 54 | BlockPos center = builder.getBoundingBox().getCenter();
|
| 55 | int surfaceHeight = chunkGenerator.getBaseHeight(
|
| 56 | center.getX(), center.getZ(), Heightmap.Types.WORLD_SURFACE_WG, context.heightAccessor(), context.randomState()
|
| 57 | );
|
| 58 | int targetYForCenter = surfaceHeight <= seaLevel ? seaLevel : Mth.randomBetweenInclusive(random, seaLevel, surfaceHeight);
|
| 59 | int dy = targetYForCenter - center.getY();
|
| 60 | builder.offsetPiecesVertically(dy);
|
| 61 | return dy;
|
| 62 | } else {
|
| 63 | return builder.moveBelowSeaLevel(seaLevel, chunkGenerator.getMinY(), random, 10);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public StructureType<?> type() {
|
| 69 | return StructureType.MINESHAFT;
|
| 70 | }
|
| 71 |
|
| 72 | public enum Type implements StringRepresentable {
|
| 73 | NORMAL("normal", Blocks.OAK_LOG, Blocks.OAK_PLANKS, Blocks.OAK_FENCE),
|
| 74 | MESA("mesa", Blocks.DARK_OAK_LOG, Blocks.DARK_OAK_PLANKS, Blocks.DARK_OAK_FENCE);
|
| 75 |
|
| 76 | public static final Codec<MineshaftStructure.Type> CODEC = StringRepresentable.fromEnum(MineshaftStructure.Type::values);
|
| 77 | private static final IntFunction<MineshaftStructure.Type> BY_ID = ByIdMap.continuous(Enum::ordinal, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 78 | private final String name;
|
| 79 | private final BlockState woodState;
|
| 80 | private final BlockState planksState;
|
| 81 | private final BlockState fenceState;
|
| 82 |
|
| 83 | Type(final String name, final Block wood, final Block plank, final Block fence) {
|
| 84 | this.name = name;
|
| 85 | this.woodState = wood.defaultBlockState();
|
| 86 | this.planksState = plank.defaultBlockState();
|
| 87 | this.fenceState = fence.defaultBlockState();
|
| 88 | }
|
| 89 |
|
| 90 | public String getName() {
|
| 91 | return this.name;
|
| 92 | }
|
| 93 |
|
| 94 | public static MineshaftStructure.Type byId(final int id) {
|
| 95 | return BY_ID.apply(id);
|
| 96 | }
|
| 97 |
|
| 98 | public BlockState getWoodState() {
|
| 99 | return this.woodState;
|
| 100 | }
|
| 101 |
|
| 102 | public BlockState getPlanksState() {
|
| 103 | return this.planksState;
|
| 104 | }
|
| 105 |
|
| 106 | public BlockState getFenceState() {
|
| 107 | return this.fenceState;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public String getSerializedName() {
|
| 112 | return this.name;
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|