| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.level.LevelReader;
|
| 9 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class GravityProcessor implements StructureProcessor {
|
| 13 | public static final MapCodec<GravityProcessor> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 14 | i -> i.group(
|
| 15 | Heightmap.Types.CODEC.optionalFieldOf("heightmap", Heightmap.Types.WORLD_SURFACE_WG).forGetter(p -> p.heightmap),
|
| 16 | Codec.INT.optionalFieldOf("offset", 0).forGetter(p -> p.offset)
|
| 17 | )
|
| 18 | .apply(i, GravityProcessor::new)
|
| 19 | );
|
| 20 | private final Heightmap.Types heightmap;
|
| 21 | private final int offset;
|
| 22 |
|
| 23 | public GravityProcessor(final Heightmap.Types heightmap, final int offset) {
|
| 24 | this.heightmap = heightmap;
|
| 25 | this.offset = offset;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public StructureTemplate.@Nullable StructureBlockInfo processBlock(
|
| 30 | final LevelReader level,
|
| 31 | final BlockPos targetPosition,
|
| 32 | final BlockPos referencePos,
|
| 33 | final BlockPos templateRelativePos,
|
| 34 | final StructureTemplate.StructureBlockInfo processedBlockInfo,
|
| 35 | final StructurePlaceSettings settings
|
| 36 | ) {
|
| 37 | Heightmap.Types heightmap;
|
| 38 | if (level instanceof ServerLevel) {
|
| 39 | if (this.heightmap == Heightmap.Types.WORLD_SURFACE_WG) {
|
| 40 | heightmap = Heightmap.Types.WORLD_SURFACE;
|
| 41 | } else if (this.heightmap == Heightmap.Types.OCEAN_FLOOR_WG) {
|
| 42 | heightmap = Heightmap.Types.OCEAN_FLOOR;
|
| 43 | } else {
|
| 44 | heightmap = this.heightmap;
|
| 45 | }
|
| 46 | } else {
|
| 47 | heightmap = this.heightmap;
|
| 48 | }
|
| 49 |
|
| 50 | BlockPos pos = processedBlockInfo.pos();
|
| 51 | int height = level.getHeight(heightmap, pos.getX(), pos.getZ()) + this.offset;
|
| 52 | int delta = templateRelativePos.getY();
|
| 53 | return new StructureTemplate.StructureBlockInfo(new BlockPos(pos.getX(), height + delta, pos.getZ()), processedBlockInfo.state(), processedBlockInfo.nbt());
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public MapCodec<GravityProcessor> codec() {
|
| 58 | return MAP_CODEC;
|
| 59 | }
|
| 60 | }
|
| 61 |
|