| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.function.Function;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.ExtraCodecs;
|
| 16 | import net.minecraft.world.level.ChunkPos;
|
| 17 | import net.minecraft.world.level.dimension.DimensionType;
|
| 18 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 19 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 20 | import net.minecraft.world.level.levelgen.heightproviders.HeightProvider;
|
| 21 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 22 | import net.minecraft.world.level.levelgen.structure.StructureType;
|
| 23 | import net.minecraft.world.level.levelgen.structure.pools.DimensionPadding;
|
| 24 | import net.minecraft.world.level.levelgen.structure.pools.JigsawPlacement;
|
| 25 | import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
|
| 26 | import net.minecraft.world.level.levelgen.structure.pools.alias.PoolAliasBinding;
|
| 27 | import net.minecraft.world.level.levelgen.structure.pools.alias.PoolAliasLookup;
|
| 28 | import net.minecraft.world.level.levelgen.structure.templatesystem.LiquidSettings;
|
| 29 |
|
| 30 | public final class JigsawStructure extends Structure {
|
| 31 | public static final DimensionPadding DEFAULT_DIMENSION_PADDING = DimensionPadding.ZERO;
|
| 32 | public static final LiquidSettings DEFAULT_LIQUID_SETTINGS = LiquidSettings.APPLY_WATERLOGGING;
|
| 33 | public static final int MAX_TOTAL_STRUCTURE_RANGE = 128;
|
| 34 | public static final int MIN_DEPTH = 0;
|
| 35 | public static final int MAX_DEPTH = 20;
|
| 36 | public static final MapCodec<JigsawStructure> CODEC = RecordCodecBuilder.<JigsawStructure>mapCodec(
|
| 37 | i -> i.group(
|
| 38 | settingsCodec(i),
|
| 39 | StructureTemplatePool.CODEC.fieldOf("start_pool").forGetter(j -> j.startPool),
|
| 40 | Identifier.CODEC.optionalFieldOf("start_jigsaw_name").forGetter(j -> j.startJigsawName),
|
| 41 | Codec.intRange(0, 20).fieldOf("size").forGetter(j -> j.maxDepth),
|
| 42 | HeightProvider.CODEC.fieldOf("start_height").forGetter(j -> j.startHeight),
|
| 43 | Codec.BOOL.fieldOf("use_expansion_hack").forGetter(j -> j.useExpansionHack),
|
| 44 | Heightmap.Types.CODEC.optionalFieldOf("project_start_to_heightmap").forGetter(j -> j.projectStartToHeightmap),
|
| 45 | JigsawStructure.MaxDistance.CODEC.fieldOf("max_distance_from_center").forGetter(j -> j.maxDistanceFromCenter),
|
| 46 | Codec.list(PoolAliasBinding.CODEC).optionalFieldOf("pool_aliases", List.of()).forGetter(j -> j.poolAliases),
|
| 47 | DimensionPadding.CODEC.optionalFieldOf("dimension_padding", DEFAULT_DIMENSION_PADDING).forGetter(j -> j.dimensionPadding),
|
| 48 | LiquidSettings.CODEC.optionalFieldOf("liquid_settings", DEFAULT_LIQUID_SETTINGS).forGetter(j -> j.liquidSettings)
|
| 49 | )
|
| 50 | .apply(i, JigsawStructure::new)
|
| 51 | )
|
| 52 | .validate(JigsawStructure::verifyRange);
|
| 53 | private final Holder<StructureTemplatePool> startPool;
|
| 54 | private final Optional<Identifier> startJigsawName;
|
| 55 | private final int maxDepth;
|
| 56 | private final HeightProvider startHeight;
|
| 57 | private final boolean useExpansionHack;
|
| 58 | private final Optional<Heightmap.Types> projectStartToHeightmap;
|
| 59 | private final JigsawStructure.MaxDistance maxDistanceFromCenter;
|
| 60 | private final List<PoolAliasBinding> poolAliases;
|
| 61 | private final DimensionPadding dimensionPadding;
|
| 62 | private final LiquidSettings liquidSettings;
|
| 63 |
|
| 64 | private static DataResult<JigsawStructure> verifyRange(final JigsawStructure structure) {
|
| 65 | int edgeNeeded = switch (structure.terrainAdaptation()) {
|
| 66 | case NONE -> 0;
|
| 67 | case BURY, BEARD_THIN, BEARD_BOX, ENCAPSULATE -> 12;
|
| 68 | };
|
| 69 | return structure.maxDistanceFromCenter.horizontal() + edgeNeeded > 128
|
| 70 | ? DataResult.error(() -> "Horizontal structure size including terrain adaptation must not exceed 128")
|
| 71 | : DataResult.success(structure);
|
| 72 | }
|
| 73 |
|
| 74 | public JigsawStructure(
|
| 75 | final Structure.StructureSettings settings,
|
| 76 | final Holder<StructureTemplatePool> startPool,
|
| 77 | final Optional<Identifier> startJigsawName,
|
| 78 | final int maxDepth,
|
| 79 | final HeightProvider startHeight,
|
| 80 | final boolean useExpansionHack,
|
| 81 | final Optional<Heightmap.Types> projectStartToHeightmap,
|
| 82 | final JigsawStructure.MaxDistance maxDistanceFromCenter,
|
| 83 | final List<PoolAliasBinding> poolAliases,
|
| 84 | final DimensionPadding dimensionPadding,
|
| 85 | final LiquidSettings liquidSettings
|
| 86 | ) {
|
| 87 | super(settings);
|
| 88 | this.startPool = startPool;
|
| 89 | this.startJigsawName = startJigsawName;
|
| 90 | this.maxDepth = maxDepth;
|
| 91 | this.startHeight = startHeight;
|
| 92 | this.useExpansionHack = useExpansionHack;
|
| 93 | this.projectStartToHeightmap = projectStartToHeightmap;
|
| 94 | this.maxDistanceFromCenter = maxDistanceFromCenter;
|
| 95 | this.poolAliases = poolAliases;
|
| 96 | this.dimensionPadding = dimensionPadding;
|
| 97 | this.liquidSettings = liquidSettings;
|
| 98 | }
|
| 99 |
|
| 100 | public JigsawStructure(
|
| 101 | final Structure.StructureSettings settings,
|
| 102 | final Holder<StructureTemplatePool> startPool,
|
| 103 | final int maxDepth,
|
| 104 | final HeightProvider startHeight,
|
| 105 | final boolean useExpansionHack,
|
| 106 | final Heightmap.Types projectStartToHeightmap
|
| 107 | ) {
|
| 108 | this(
|
| 109 | settings,
|
| 110 | startPool,
|
| 111 | Optional.empty(),
|
| 112 | maxDepth,
|
| 113 | startHeight,
|
| 114 | useExpansionHack,
|
| 115 | Optional.of(projectStartToHeightmap),
|
| 116 | new JigsawStructure.MaxDistance(80),
|
| 117 | List.of(),
|
| 118 | DEFAULT_DIMENSION_PADDING,
|
| 119 | DEFAULT_LIQUID_SETTINGS
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | public JigsawStructure(
|
| 124 | final Structure.StructureSettings settings,
|
| 125 | final Holder<StructureTemplatePool> startPool,
|
| 126 | final int maxDepth,
|
| 127 | final HeightProvider startHeight,
|
| 128 | final boolean useExpansionHack
|
| 129 | ) {
|
| 130 | this(
|
| 131 | settings,
|
| 132 | startPool,
|
| 133 | Optional.empty(),
|
| 134 | maxDepth,
|
| 135 | startHeight,
|
| 136 | useExpansionHack,
|
| 137 | Optional.empty(),
|
| 138 | new JigsawStructure.MaxDistance(80),
|
| 139 | List.of(),
|
| 140 | DEFAULT_DIMENSION_PADDING,
|
| 141 | DEFAULT_LIQUID_SETTINGS
|
| 142 | );
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context) {
|
| 147 | ChunkPos chunkPos = context.chunkPos();
|
| 148 | int height = this.startHeight.sample(context.random(), new WorldGenerationContext(context.chunkGenerator(), context.heightAccessor()));
|
| 149 | BlockPos startPos = new BlockPos(chunkPos.getMinBlockX(), height, chunkPos.getMinBlockZ());
|
| 150 | return JigsawPlacement.addPieces(
|
| 151 | context,
|
| 152 | this.startPool,
|
| 153 | this.startJigsawName,
|
| 154 | this.maxDepth,
|
| 155 | startPos,
|
| 156 | this.useExpansionHack,
|
| 157 | this.projectStartToHeightmap,
|
| 158 | this.maxDistanceFromCenter,
|
| 159 | PoolAliasLookup.create(this.poolAliases, startPos, context.seed()),
|
| 160 | this.dimensionPadding,
|
| 161 | this.liquidSettings
|
| 162 | );
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public StructureType<?> type() {
|
| 167 | return StructureType.JIGSAW;
|
| 168 | }
|
| 169 |
|
| 170 | @VisibleForTesting
|
| 171 | public Holder<StructureTemplatePool> getStartPool() {
|
| 172 | return this.startPool;
|
| 173 | }
|
| 174 |
|
| 175 | @VisibleForTesting
|
| 176 | public List<PoolAliasBinding> getPoolAliases() {
|
| 177 | return this.poolAliases;
|
| 178 | }
|
| 179 |
|
| 180 | public record MaxDistance(int horizontal, int vertical) {
|
| 181 | private static final Codec<Integer> HORIZONTAL_VALUE_CODEC = Codec.intRange(1, 128);
|
| 182 | private static final Codec<JigsawStructure.MaxDistance> FULL_CODEC = RecordCodecBuilder.create(
|
| 183 | i -> i.group(
|
| 184 | HORIZONTAL_VALUE_CODEC.fieldOf("horizontal").forGetter(JigsawStructure.MaxDistance::horizontal),
|
| 185 | ExtraCodecs.intRange(1, DimensionType.Y_SIZE).optionalFieldOf("vertical", DimensionType.Y_SIZE).forGetter(JigsawStructure.MaxDistance::vertical)
|
| 186 | )
|
| 187 | .apply(i, JigsawStructure.MaxDistance::new)
|
| 188 | );
|
| 189 | public static final Codec<JigsawStructure.MaxDistance> CODEC = Codec.either(FULL_CODEC, HORIZONTAL_VALUE_CODEC)
|
| 190 | .xmap(
|
| 191 | either -> either.map(Function.identity(), JigsawStructure.MaxDistance::new),
|
| 192 | distance -> distance.horizontal == distance.vertical ? Either.right(distance.horizontal) : Either.left(distance)
|
| 193 | );
|
| 194 |
|
| 195 | public MaxDistance(final int value) {
|
| 196 | this(value, value);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|