| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.world.level.dimension.DimensionType;
|
| 7 |
|
| 8 | public interface VerticalAnchor {
|
| 9 | Codec<VerticalAnchor> CODEC = Codec.xor(
|
| 10 | VerticalAnchor.Absolute.CODEC,
|
| 11 | Codec.xor(VerticalAnchor.AboveBottom.CODEC, Codec.xor(VerticalAnchor.BelowTop.CODEC, VerticalAnchor.RelativeToSeaLevel.CODEC))
|
| 12 | )
|
| 13 | .xmap(VerticalAnchor::merge, VerticalAnchor::split);
|
| 14 | VerticalAnchor BOTTOM = aboveBottom(0);
|
| 15 | VerticalAnchor TOP = belowTop(0);
|
| 16 |
|
| 17 | static VerticalAnchor absolute(final int value) {
|
| 18 | return new VerticalAnchor.Absolute(value);
|
| 19 | }
|
| 20 |
|
| 21 | static VerticalAnchor aboveBottom(final int offset) {
|
| 22 | return new VerticalAnchor.AboveBottom(offset);
|
| 23 | }
|
| 24 |
|
| 25 | static VerticalAnchor belowTop(final int offset) {
|
| 26 | return new VerticalAnchor.BelowTop(offset);
|
| 27 | }
|
| 28 |
|
| 29 | static VerticalAnchor bottom() {
|
| 30 | return BOTTOM;
|
| 31 | }
|
| 32 |
|
| 33 | static VerticalAnchor top() {
|
| 34 | return TOP;
|
| 35 | }
|
| 36 |
|
| 37 | static VerticalAnchor relativeToSeaLevel(final int offset) {
|
| 38 | return new VerticalAnchor.RelativeToSeaLevel(offset);
|
| 39 | }
|
| 40 |
|
| 41 | static VerticalAnchor seaLevel() {
|
| 42 | return relativeToSeaLevel(0);
|
| 43 | }
|
| 44 |
|
| 45 | private static VerticalAnchor merge(
|
| 46 | final Either<VerticalAnchor.Absolute, Either<VerticalAnchor.AboveBottom, Either<VerticalAnchor.BelowTop, VerticalAnchor.RelativeToSeaLevel>>> either
|
| 47 | ) {
|
| 48 | return either.map(Function.identity(), e -> e.map(Function.identity(), Either::unwrap));
|
| 49 | }
|
| 50 |
|
| 51 | private static Either<VerticalAnchor.Absolute, Either<VerticalAnchor.AboveBottom, Either<VerticalAnchor.BelowTop, VerticalAnchor.RelativeToSeaLevel>>> split(
|
| 52 | final VerticalAnchor anchor
|
| 53 | ) {
|
| 54 | if (anchor instanceof VerticalAnchor.Absolute absolute) {
|
| 55 | return Either.left(absolute);
|
| 56 | } else if (anchor instanceof VerticalAnchor.AboveBottom aboveBottom) {
|
| 57 | return Either.right(Either.left(aboveBottom));
|
| 58 | } else {
|
| 59 | return anchor instanceof VerticalAnchor.BelowTop belowTop
|
| 60 | ? Either.right(Either.right(Either.left(belowTop)))
|
| 61 | : Either.right(Either.right(Either.right((VerticalAnchor.RelativeToSeaLevel)anchor)));
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | int resolveY(final WorldGenerationContext heightAccessor);
|
| 66 |
|
| 67 | record AboveBottom(int offset) implements VerticalAnchor {
|
| 68 | public static final Codec<VerticalAnchor.AboveBottom> CODEC = Codec.intRange(DimensionType.MIN_Y, DimensionType.MAX_Y)
|
| 69 | .fieldOf("above_bottom")
|
| 70 | .xmap(VerticalAnchor.AboveBottom::new, VerticalAnchor.AboveBottom::offset)
|
| 71 | .codec();
|
| 72 |
|
| 73 | @Override
|
| 74 | public int resolveY(final WorldGenerationContext heightAccessor) {
|
| 75 | return heightAccessor.getMinGenY() + this.offset;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public String toString() {
|
| 80 | return this.offset + " above bottom";
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | record Absolute(int y) implements VerticalAnchor {
|
| 85 | public static final Codec<VerticalAnchor.Absolute> CODEC = Codec.intRange(DimensionType.MIN_Y, DimensionType.MAX_Y)
|
| 86 | .fieldOf("absolute")
|
| 87 | .xmap(VerticalAnchor.Absolute::new, VerticalAnchor.Absolute::y)
|
| 88 | .codec();
|
| 89 |
|
| 90 | @Override
|
| 91 | public int resolveY(final WorldGenerationContext heightAccessor) {
|
| 92 | return this.y;
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public String toString() {
|
| 97 | return this.y + " absolute";
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | record BelowTop(int offset) implements VerticalAnchor {
|
| 102 | public static final Codec<VerticalAnchor.BelowTop> CODEC = Codec.intRange(DimensionType.MIN_Y, DimensionType.MAX_Y)
|
| 103 | .fieldOf("below_top")
|
| 104 | .xmap(VerticalAnchor.BelowTop::new, VerticalAnchor.BelowTop::offset)
|
| 105 | .codec();
|
| 106 |
|
| 107 | @Override
|
| 108 | public int resolveY(final WorldGenerationContext heightAccessor) {
|
| 109 | return heightAccessor.getGenDepth() - 1 + heightAccessor.getMinGenY() - this.offset;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public String toString() {
|
| 114 | return this.offset + " below top";
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | record RelativeToSeaLevel(int offset) implements VerticalAnchor {
|
| 119 | public static final Codec<VerticalAnchor.RelativeToSeaLevel> CODEC = Codec.intRange(DimensionType.MIN_Y, DimensionType.MAX_Y)
|
| 120 | .fieldOf("relative_to_sea_level")
|
| 121 | .xmap(VerticalAnchor.RelativeToSeaLevel::new, VerticalAnchor.RelativeToSeaLevel::offset)
|
| 122 | .codec();
|
| 123 |
|
| 124 | @Override
|
| 125 | public int resolveY(final WorldGenerationContext heightAccessor) {
|
| 126 | return heightAccessor.seaLevel() + this.offset;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public String toString() {
|
| 131 | return this.offset + " relative to sea level";
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|