| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import java.util.OptionalInt;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.world.level.LevelSimulatedReader;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 |
|
| 11 | public abstract class Column {
|
| 12 | public static Column.Range around(final int lowest, final int highest) {
|
| 13 | return new Column.Range(lowest - 1, highest + 1);
|
| 14 | }
|
| 15 |
|
| 16 | public static Column.Range inside(final int floor, final int ceiling) {
|
| 17 | return new Column.Range(floor, ceiling);
|
| 18 | }
|
| 19 |
|
| 20 | public static Column below(final int ceiling) {
|
| 21 | return new Column.Ray(ceiling, false);
|
| 22 | }
|
| 23 |
|
| 24 | public static Column fromHighest(final int highest) {
|
| 25 | return new Column.Ray(highest + 1, false);
|
| 26 | }
|
| 27 |
|
| 28 | public static Column above(final int floor) {
|
| 29 | return new Column.Ray(floor, true);
|
| 30 | }
|
| 31 |
|
| 32 | public static Column fromLowest(final int lowest) {
|
| 33 | return new Column.Ray(lowest - 1, true);
|
| 34 | }
|
| 35 |
|
| 36 | public static Column line() {
|
| 37 | return Column.Line.INSTANCE;
|
| 38 | }
|
| 39 |
|
| 40 | public static Column create(final OptionalInt floor, final OptionalInt ceiling) {
|
| 41 | if (floor.isPresent() && ceiling.isPresent()) {
|
| 42 | return inside(floor.getAsInt(), ceiling.getAsInt());
|
| 43 | } else if (floor.isPresent()) {
|
| 44 | return above(floor.getAsInt());
|
| 45 | } else {
|
| 46 | return ceiling.isPresent() ? below(ceiling.getAsInt()) : line();
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public abstract OptionalInt getCeiling();
|
| 51 |
|
| 52 | public abstract OptionalInt getFloor();
|
| 53 |
|
| 54 | public abstract OptionalInt getHeight();
|
| 55 |
|
| 56 | public Column withFloor(final OptionalInt floor) {
|
| 57 | return create(floor, this.getCeiling());
|
| 58 | }
|
| 59 |
|
| 60 | public Column withCeiling(final OptionalInt ceiling) {
|
| 61 | return create(this.getFloor(), ceiling);
|
| 62 | }
|
| 63 |
|
| 64 | public static Optional<Column> scan(
|
| 65 | final LevelSimulatedReader level, final BlockPos pos, final int searchRange, final Predicate<BlockState> insideColumn, final Predicate<BlockState> validEdge
|
| 66 | ) {
|
| 67 | BlockPos.MutableBlockPos mutablePos = pos.mutable();
|
| 68 | if (!level.isStateAtPosition(pos, insideColumn)) {
|
| 69 | return Optional.empty();
|
| 70 | }
|
| 71 |
|
| 72 | int nearestEmptyY = pos.getY();
|
| 73 | OptionalInt ceiling = scanDirection(level, searchRange, insideColumn, validEdge, mutablePos, nearestEmptyY, Direction.UP);
|
| 74 | OptionalInt floor = scanDirection(level, searchRange, insideColumn, validEdge, mutablePos, nearestEmptyY, Direction.DOWN);
|
| 75 | return Optional.of(create(floor, ceiling));
|
| 76 | }
|
| 77 |
|
| 78 | private static OptionalInt scanDirection(
|
| 79 | final LevelSimulatedReader level,
|
| 80 | final int searchRange,
|
| 81 | final Predicate<BlockState> insideColumn,
|
| 82 | final Predicate<BlockState> validEdge,
|
| 83 | final BlockPos.MutableBlockPos mutablePos,
|
| 84 | final int nearestEmptyY,
|
| 85 | final Direction direction
|
| 86 | ) {
|
| 87 | mutablePos.setY(nearestEmptyY);
|
| 88 |
|
| 89 | for (int i = 1; i < searchRange && level.isStateAtPosition(mutablePos, insideColumn); i++) {
|
| 90 | mutablePos.move(direction);
|
| 91 | }
|
| 92 |
|
| 93 | return level.isStateAtPosition(mutablePos, validEdge) ? OptionalInt.of(mutablePos.getY()) : OptionalInt.empty();
|
| 94 | }
|
| 95 |
|
| 96 | public static final class Line extends Column {
|
| 97 | private static final Column.Line INSTANCE = new Column.Line();
|
| 98 |
|
| 99 | private Line() {
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public OptionalInt getCeiling() {
|
| 104 | return OptionalInt.empty();
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public OptionalInt getFloor() {
|
| 109 | return OptionalInt.empty();
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public OptionalInt getHeight() {
|
| 114 | return OptionalInt.empty();
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public String toString() {
|
| 119 | return "C(-)";
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public static final class Range extends Column {
|
| 124 | private final int floor;
|
| 125 | private final int ceiling;
|
| 126 |
|
| 127 | public Range(final int floor, final int ceiling) {
|
| 128 | this.floor = floor;
|
| 129 | this.ceiling = ceiling;
|
| 130 | if (this.height() < 0) {
|
| 131 | throw new IllegalArgumentException("Column of negative height: " + this);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public OptionalInt getCeiling() {
|
| 137 | return OptionalInt.of(this.ceiling);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public OptionalInt getFloor() {
|
| 142 | return OptionalInt.of(this.floor);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public OptionalInt getHeight() {
|
| 147 | return OptionalInt.of(this.height());
|
| 148 | }
|
| 149 |
|
| 150 | public int ceiling() {
|
| 151 | return this.ceiling;
|
| 152 | }
|
| 153 |
|
| 154 | public int floor() {
|
| 155 | return this.floor;
|
| 156 | }
|
| 157 |
|
| 158 | public int height() {
|
| 159 | return this.ceiling - this.floor - 1;
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public String toString() {
|
| 164 | return "C(" + this.ceiling + "-" + this.floor + ")";
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | public static final class Ray extends Column {
|
| 169 | private final int edge;
|
| 170 | private final boolean pointingUp;
|
| 171 |
|
| 172 | public Ray(final int edge, final boolean pointingUp) {
|
| 173 | this.edge = edge;
|
| 174 | this.pointingUp = pointingUp;
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public OptionalInt getCeiling() {
|
| 179 | return this.pointingUp ? OptionalInt.empty() : OptionalInt.of(this.edge);
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public OptionalInt getFloor() {
|
| 184 | return this.pointingUp ? OptionalInt.of(this.edge) : OptionalInt.empty();
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public OptionalInt getHeight() {
|
| 189 | return OptionalInt.empty();
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public String toString() {
|
| 194 | return this.pointingUp ? "C(" + this.edge + "-)" : "C(-" + this.edge + ")";
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|