| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Iterators;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import io.netty.buffer.ByteBuf;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.Comparator;
|
| 11 | import java.util.Iterator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.function.IntFunction;
|
| 14 | import java.util.function.Predicate;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 17 | import net.minecraft.network.codec.StreamCodec;
|
| 18 | import net.minecraft.util.ByIdMap;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.StringRepresentable;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.world.entity.Entity;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import org.jetbrains.annotations.Contract;
|
| 26 | import org.joml.Matrix4fc;
|
| 27 | import org.joml.Quaternionf;
|
| 28 | import org.joml.Vector3f;
|
| 29 | import org.joml.Vector3fc;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public enum Direction implements Directional, StringRepresentable {
|
| 33 | DOWN(0, 1, -1, "down", Direction.AxisDirection.NEGATIVE, Direction.Axis.Y, new Vec3i(0, -1, 0)),
|
| 34 | UP(1, 0, -1, "up", Direction.AxisDirection.POSITIVE, Direction.Axis.Y, new Vec3i(0, 1, 0)),
|
| 35 | NORTH(2, 3, 2, "north", Direction.AxisDirection.NEGATIVE, Direction.Axis.Z, new Vec3i(0, 0, -1)),
|
| 36 | SOUTH(3, 2, 0, "south", Direction.AxisDirection.POSITIVE, Direction.Axis.Z, new Vec3i(0, 0, 1)),
|
| 37 | WEST(4, 5, 1, "west", Direction.AxisDirection.NEGATIVE, Direction.Axis.X, new Vec3i(-1, 0, 0)),
|
| 38 | EAST(5, 4, 3, "east", Direction.AxisDirection.POSITIVE, Direction.Axis.X, new Vec3i(1, 0, 0));
|
| 39 |
|
| 40 | public static final StringRepresentable.EnumCodec<Direction> CODEC = StringRepresentable.fromEnum(Direction::values);
|
| 41 | public static final Codec<Direction> VERTICAL_CODEC = CODEC.validate(Direction::verifyVertical);
|
| 42 | public static final IntFunction<Direction> BY_ID = ByIdMap.continuous(Direction::get3DDataValue, values(), ByIdMap.OutOfBoundsStrategy.WRAP);
|
| 43 | public static final StreamCodec<ByteBuf, Direction> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Direction::get3DDataValue);
|
| 44 | @Deprecated
|
| 45 | public static final Codec<Direction> LEGACY_ID_CODEC = Codec.BYTE.xmap(Direction::from3DDataValue, d -> (byte)d.get3DDataValue());
|
| 46 | @Deprecated
|
| 47 | public static final Codec<Direction> LEGACY_ID_CODEC_2D = Codec.BYTE.xmap(Direction::from2DDataValue, d -> (byte)d.get2DDataValue());
|
| 48 | private static final ImmutableList<Direction.Axis> YXZ_AXIS_ORDER = ImmutableList.of(Direction.Axis.Y, Direction.Axis.X, Direction.Axis.Z);
|
| 49 | private static final ImmutableList<Direction.Axis> YZX_AXIS_ORDER = ImmutableList.of(Direction.Axis.Y, Direction.Axis.Z, Direction.Axis.X);
|
| 50 | private final int data3d;
|
| 51 | private final int oppositeIndex;
|
| 52 | private final int data2d;
|
| 53 | private final String name;
|
| 54 | private final Direction.Axis axis;
|
| 55 | private final Direction.AxisDirection axisDirection;
|
| 56 | private final Vec3i normal;
|
| 57 | private final Vec3 normalVec3;
|
| 58 | private final Vector3fc normalVec3f;
|
| 59 | private static final Direction[] VALUES = values();
|
| 60 | private static final Direction[] BY_3D_DATA = Arrays.stream(VALUES).sorted(Comparator.comparingInt(d -> d.data3d)).toArray(Direction[]::new);
|
| 61 | private static final Direction[] BY_2D_DATA = Arrays.stream(VALUES)
|
| 62 | .filter(d -> d.getAxis().isHorizontal())
|
| 63 | .sorted(Comparator.comparingInt(d -> d.data2d))
|
| 64 | .toArray(Direction[]::new);
|
| 65 |
|
| 66 | Direction(
|
| 67 | final int data3d,
|
| 68 | final int oppositeIndex,
|
| 69 | final int data2d,
|
| 70 | final String name,
|
| 71 | final Direction.AxisDirection axisDirection,
|
| 72 | final Direction.Axis axis,
|
| 73 | final Vec3i normal
|
| 74 | ) {
|
| 75 | this.data3d = data3d;
|
| 76 | this.data2d = data2d;
|
| 77 | this.oppositeIndex = oppositeIndex;
|
| 78 | this.name = name;
|
| 79 | this.axis = axis;
|
| 80 | this.axisDirection = axisDirection;
|
| 81 | this.normal = normal;
|
| 82 | this.normalVec3 = Vec3.atLowerCornerOf(normal);
|
| 83 | this.normalVec3f = new Vector3f(normal.getX(), normal.getY(), normal.getZ());
|
| 84 | }
|
| 85 |
|
| 86 | public static Direction[] orderedByNearest(final Entity entity) {
|
| 87 | float pitch = entity.getViewXRot(1.0F) * (float) (Math.PI / 180.0);
|
| 88 | float yaw = -entity.getViewYRot(1.0F) * (float) (Math.PI / 180.0);
|
| 89 | float pitchSin = Mth.sin(pitch);
|
| 90 | float pitchCos = Mth.cos(pitch);
|
| 91 | float yawSin = Mth.sin(yaw);
|
| 92 | float yawCos = Mth.cos(yaw);
|
| 93 | boolean xPos = yawSin > 0.0F;
|
| 94 | boolean yPos = pitchSin < 0.0F;
|
| 95 | boolean zPos = yawCos > 0.0F;
|
| 96 | float xYaw = xPos ? yawSin : -yawSin;
|
| 97 | float yMag = yPos ? -pitchSin : pitchSin;
|
| 98 | float zYaw = zPos ? yawCos : -yawCos;
|
| 99 | float xMag = xYaw * pitchCos;
|
| 100 | float zMag = zYaw * pitchCos;
|
| 101 | Direction axisX = xPos ? EAST : WEST;
|
| 102 | Direction axisY = yPos ? UP : DOWN;
|
| 103 | Direction axisZ = zPos ? SOUTH : NORTH;
|
| 104 | if (xYaw > zYaw) {
|
| 105 | if (yMag > xMag) {
|
| 106 | return makeDirectionArray(axisY, axisX, axisZ);
|
| 107 | } else {
|
| 108 | return zMag > yMag ? makeDirectionArray(axisX, axisZ, axisY) : makeDirectionArray(axisX, axisY, axisZ);
|
| 109 | }
|
| 110 | } else if (yMag > zMag) {
|
| 111 | return makeDirectionArray(axisY, axisZ, axisX);
|
| 112 | } else {
|
| 113 | return xMag > yMag ? makeDirectionArray(axisZ, axisX, axisY) : makeDirectionArray(axisZ, axisY, axisX);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | private static Direction[] makeDirectionArray(final Direction axis1, final Direction axis2, final Direction axis3) {
|
| 118 | return new Direction[]{axis1, axis2, axis3, axis3.getOpposite(), axis2.getOpposite(), axis1.getOpposite()};
|
| 119 | }
|
| 120 |
|
| 121 | public static Direction rotate(final Matrix4fc matrix, final Direction facing) {
|
| 122 | Vector3f vec = matrix.transformDirection(facing.normalVec3f, new Vector3f());
|
| 123 | return getApproximateNearest(vec.x(), vec.y(), vec.z());
|
| 124 | }
|
| 125 |
|
| 126 | public static Collection<Direction> allShuffled(final RandomSource random) {
|
| 127 | return Util.shuffledCopy(values(), random);
|
| 128 | }
|
| 129 |
|
| 130 | public static Stream<Direction> stream() {
|
| 131 | return Stream.of(VALUES);
|
| 132 | }
|
| 133 |
|
| 134 | public static float getYRot(final Direction direction) {
|
| 135 | return switch (direction) {
|
| 136 | case NORTH -> 180.0F;
|
| 137 | case SOUTH -> 0.0F;
|
| 138 | case WEST -> 90.0F;
|
| 139 | case EAST -> -90.0F;
|
| 140 | default -> throw new IllegalStateException("No y-Rot for vertical axis: " + direction);
|
| 141 | };
|
| 142 | }
|
| 143 |
|
| 144 | public Quaternionf getRotation() {
|
| 145 | return switch (this) {
|
| 146 | case DOWN -> new Quaternionf().rotationX((float) Math.PI);
|
| 147 | case UP -> new Quaternionf();
|
| 148 | case NORTH -> new Quaternionf().rotationXYZ((float) (Math.PI / 2), 0.0F, (float) Math.PI);
|
| 149 | case SOUTH -> new Quaternionf().rotationX((float) (Math.PI / 2));
|
| 150 | case WEST -> new Quaternionf().rotationXYZ((float) (Math.PI / 2), 0.0F, (float) (Math.PI / 2));
|
| 151 | case EAST -> new Quaternionf().rotationXYZ((float) (Math.PI / 2), 0.0F, (float) (-Math.PI / 2));
|
| 152 | };
|
| 153 | }
|
| 154 |
|
| 155 | public int get3DDataValue() {
|
| 156 | return this.data3d;
|
| 157 | }
|
| 158 |
|
| 159 | public int get2DDataValue() {
|
| 160 | return this.data2d;
|
| 161 | }
|
| 162 |
|
| 163 | public Direction.AxisDirection getAxisDirection() {
|
| 164 | return this.axisDirection;
|
| 165 | }
|
| 166 |
|
| 167 | public static Direction getFacingAxis(final Entity entity, final Direction.Axis axis) {
|
| 168 | return switch (axis) {
|
| 169 | case X -> EAST.isFacingAngle(entity.getViewYRot(1.0F)) ? EAST : WEST;
|
| 170 | case Y -> entity.getViewXRot(1.0F) < 0.0F ? UP : DOWN;
|
| 171 | case Z -> SOUTH.isFacingAngle(entity.getViewYRot(1.0F)) ? SOUTH : NORTH;
|
| 172 | };
|
| 173 | }
|
| 174 |
|
| 175 | public Direction getOpposite() {
|
| 176 | return from3DDataValue(this.oppositeIndex);
|
| 177 | }
|
| 178 |
|
| 179 | public Direction getClockWise(final Direction.Axis axis) {
|
| 180 | return switch (axis) {
|
| 181 | case X -> this != WEST && this != EAST ? this.getClockWiseX() : this;
|
| 182 | case Y -> this != UP && this != DOWN ? this.getClockWise() : this;
|
| 183 | case Z -> this != NORTH && this != SOUTH ? this.getClockWiseZ() : this;
|
| 184 | };
|
| 185 | }
|
| 186 |
|
| 187 | public Direction getCounterClockWise(final Direction.Axis axis) {
|
| 188 | return switch (axis) {
|
| 189 | case X -> this != WEST && this != EAST ? this.getCounterClockWiseX() : this;
|
| 190 | case Y -> this != UP && this != DOWN ? this.getCounterClockWise() : this;
|
| 191 | case Z -> this != NORTH && this != SOUTH ? this.getCounterClockWiseZ() : this;
|
| 192 | };
|
| 193 | }
|
| 194 |
|
| 195 | public Direction getClockWise() {
|
| 196 | return switch (this) {
|
| 197 | case NORTH -> EAST;
|
| 198 | case SOUTH -> WEST;
|
| 199 | case WEST -> NORTH;
|
| 200 | case EAST -> SOUTH;
|
| 201 | default -> throw new IllegalStateException("Unable to get Y-rotated facing of " + this);
|
| 202 | };
|
| 203 | }
|
| 204 |
|
| 205 | private Direction getClockWiseX() {
|
| 206 | return switch (this) {
|
| 207 | case DOWN -> SOUTH;
|
| 208 | case UP -> NORTH;
|
| 209 | case NORTH -> DOWN;
|
| 210 | case SOUTH -> UP;
|
| 211 | default -> throw new IllegalStateException("Unable to get X-rotated facing of " + this);
|
| 212 | };
|
| 213 | }
|
| 214 |
|
| 215 | private Direction getCounterClockWiseX() {
|
| 216 | return switch (this) {
|
| 217 | case DOWN -> NORTH;
|
| 218 | case UP -> SOUTH;
|
| 219 | case NORTH -> UP;
|
| 220 | case SOUTH -> DOWN;
|
| 221 | default -> throw new IllegalStateException("Unable to get X-rotated facing of " + this);
|
| 222 | };
|
| 223 | }
|
| 224 |
|
| 225 | private Direction getClockWiseZ() {
|
| 226 | return switch (this) {
|
| 227 | case DOWN -> WEST;
|
| 228 | case UP -> EAST;
|
| 229 | default -> throw new IllegalStateException("Unable to get Z-rotated facing of " + this);
|
| 230 | case WEST -> UP;
|
| 231 | case EAST -> DOWN;
|
| 232 | };
|
| 233 | }
|
| 234 |
|
| 235 | private Direction getCounterClockWiseZ() {
|
| 236 | return switch (this) {
|
| 237 | case DOWN -> EAST;
|
| 238 | case UP -> WEST;
|
| 239 | default -> throw new IllegalStateException("Unable to get Z-rotated facing of " + this);
|
| 240 | case WEST -> DOWN;
|
| 241 | case EAST -> UP;
|
| 242 | };
|
| 243 | }
|
| 244 |
|
| 245 | public Direction getCounterClockWise() {
|
| 246 | return switch (this) {
|
| 247 | case NORTH -> WEST;
|
| 248 | case SOUTH -> EAST;
|
| 249 | case WEST -> SOUTH;
|
| 250 | case EAST -> NORTH;
|
| 251 | default -> throw new IllegalStateException("Unable to get CCW facing of " + this);
|
| 252 | };
|
| 253 | }
|
| 254 |
|
| 255 | @Override
|
| 256 | public int getStepX() {
|
| 257 | return this.normal.getX();
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | public int getStepY() {
|
| 262 | return this.normal.getY();
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | public int getStepZ() {
|
| 267 | return this.normal.getZ();
|
| 268 | }
|
| 269 |
|
| 270 | @Override
|
| 271 | public Vec3i getStep() {
|
| 272 | return this.getUnitVec3i();
|
| 273 | }
|
| 274 |
|
| 275 | public Vector3f step() {
|
| 276 | return new Vector3f(this.normalVec3f);
|
| 277 | }
|
| 278 |
|
| 279 | public String getName() {
|
| 280 | return this.name;
|
| 281 | }
|
| 282 |
|
| 283 | public Direction.Axis getAxis() {
|
| 284 | return this.axis;
|
| 285 | }
|
| 286 |
|
| 287 | public static @Nullable Direction byName(final String name) {
|
| 288 | return CODEC.byName(name);
|
| 289 | }
|
| 290 |
|
| 291 | public static Direction from3DDataValue(final int data) {
|
| 292 | return BY_3D_DATA[Mth.abs(data % BY_3D_DATA.length)];
|
| 293 | }
|
| 294 |
|
| 295 | public static Direction from2DDataValue(final int data) {
|
| 296 | return BY_2D_DATA[Mth.abs(data % BY_2D_DATA.length)];
|
| 297 | }
|
| 298 |
|
| 299 | public static Direction fromYRot(final double yRot) {
|
| 300 | return from2DDataValue(Mth.floor(yRot / 90.0 + 0.5) & 3);
|
| 301 | }
|
| 302 |
|
| 303 | public static Direction fromAxisAndDirection(final Direction.Axis axis, final Direction.AxisDirection direction) {
|
| 304 | return switch (axis) {
|
| 305 | case X -> direction == Direction.AxisDirection.POSITIVE ? EAST : WEST;
|
| 306 | case Y -> direction == Direction.AxisDirection.POSITIVE ? UP : DOWN;
|
| 307 | case Z -> direction == Direction.AxisDirection.POSITIVE ? SOUTH : NORTH;
|
| 308 | };
|
| 309 | }
|
| 310 |
|
| 311 | public float toYRot() {
|
| 312 | return (this.data2d & 3) * 90;
|
| 313 | }
|
| 314 |
|
| 315 | public static Direction getRandom(final RandomSource random) {
|
| 316 | return Util.getRandom(VALUES, random);
|
| 317 | }
|
| 318 |
|
| 319 | public static Direction getApproximateNearest(final double dx, final double dy, final double dz) {
|
| 320 | return getApproximateNearest((float)dx, (float)dy, (float)dz);
|
| 321 | }
|
| 322 |
|
| 323 | public static Direction getApproximateNearest(final float dx, final float dy, final float dz) {
|
| 324 | Direction result = NORTH;
|
| 325 | float highestDot = Float.MIN_VALUE;
|
| 326 |
|
| 327 | for (Direction direction : VALUES) {
|
| 328 | float dot = dx * direction.normal.getX() + dy * direction.normal.getY() + dz * direction.normal.getZ();
|
| 329 | if (dot > highestDot) {
|
| 330 | highestDot = dot;
|
| 331 | result = direction;
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | return result;
|
| 336 | }
|
| 337 |
|
| 338 | public static Direction getApproximateNearest(final Vec3 vec) {
|
| 339 | return getApproximateNearest(vec.x, vec.y, vec.z);
|
| 340 | }
|
| 341 |
|
| 342 | @Contract("_,_,_,!null->!null;_,_,_,_->_")
|
| 343 | public static @Nullable Direction getNearest(final int x, final int y, final int z, final @Nullable Direction orElse) {
|
| 344 | int absX = Math.abs(x);
|
| 345 | int absY = Math.abs(y);
|
| 346 | int absZ = Math.abs(z);
|
| 347 | if (absX > absZ && absX > absY) {
|
| 348 | return x < 0 ? WEST : EAST;
|
| 349 | } else if (absZ > absX && absZ > absY) {
|
| 350 | return z < 0 ? NORTH : SOUTH;
|
| 351 | } else if (absY > absX && absY > absZ) {
|
| 352 | return y < 0 ? DOWN : UP;
|
| 353 | } else {
|
| 354 | return orElse;
|
| 355 | }
|
| 356 | }
|
| 357 |
|
| 358 | @Contract("_,!null->!null;_,_->_")
|
| 359 | public static @Nullable Direction getNearest(final Vec3i vec, final @Nullable Direction orElse) {
|
| 360 | return getNearest(vec.getX(), vec.getY(), vec.getZ(), orElse);
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public String toString() {
|
| 365 | return this.name;
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | public String getSerializedName() {
|
| 370 | return this.name;
|
| 371 | }
|
| 372 |
|
| 373 | private static DataResult<Direction> verifyVertical(final Direction v) {
|
| 374 | return v.getAxis().isVertical() ? DataResult.success(v) : DataResult.error(() -> "Expected a vertical direction");
|
| 375 | }
|
| 376 |
|
| 377 | public static Direction get(final Direction.AxisDirection axisDirection, final Direction.Axis axis) {
|
| 378 | for (Direction direction : VALUES) {
|
| 379 | if (direction.getAxisDirection() == axisDirection && direction.getAxis() == axis) {
|
| 380 | return direction;
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | throw new IllegalArgumentException("No such direction: " + axisDirection + " " + axis);
|
| 385 | }
|
| 386 |
|
| 387 | public static ImmutableList<Direction.Axis> axisStepOrder(final Vec3 movement) {
|
| 388 | return Math.abs(movement.x) < Math.abs(movement.z) ? YZX_AXIS_ORDER : YXZ_AXIS_ORDER;
|
| 389 | }
|
| 390 |
|
| 391 | public Vec3i getUnitVec3i() {
|
| 392 | return this.normal;
|
| 393 | }
|
| 394 |
|
| 395 | public Vec3 getUnitVec3() {
|
| 396 | return this.normalVec3;
|
| 397 | }
|
| 398 |
|
| 399 | public Vector3fc getUnitVec3f() {
|
| 400 | return this.normalVec3f;
|
| 401 | }
|
| 402 |
|
| 403 | public boolean isFacingAngle(final float yAngle) {
|
| 404 | float radians = yAngle * (float) (Math.PI / 180.0);
|
| 405 | float dx = -Mth.sin(radians);
|
| 406 | float dz = Mth.cos(radians);
|
| 407 | return this.normal.getX() * dx + this.normal.getZ() * dz > 0.0F;
|
| 408 | }
|
| 409 |
|
| 410 | public enum Axis implements Predicate<Direction>, StringRepresentable {
|
| 411 | X("x"),
|
| 412 | Y("y"),
|
| 413 | Z("z");
|
| 414 |
|
| 415 | public static final Direction.Axis[] VALUES = values();
|
| 416 | public static final StringRepresentable.EnumCodec<Direction.Axis> CODEC = StringRepresentable.fromEnum(Direction.Axis::values);
|
| 417 | private final String name;
|
| 418 |
|
| 419 | Axis(final String name) {
|
| 420 | this.name = name;
|
| 421 | }
|
| 422 |
|
| 423 | public static Direction.@Nullable Axis byName(final String name) {
|
| 424 | return CODEC.byName(name);
|
| 425 | }
|
| 426 |
|
| 427 | public String getName() {
|
| 428 | return this.name;
|
| 429 | }
|
| 430 |
|
| 431 | public boolean isVertical() {
|
| 432 | return this == Y;
|
| 433 | }
|
| 434 |
|
| 435 | public boolean isHorizontal() {
|
| 436 | return this == X || this == Z;
|
| 437 | }
|
| 438 |
|
| 439 | public Direction getPositive() {
|
| 440 | return switch (this) {
|
| 441 | case X -> Direction.EAST;
|
| 442 | case Y -> Direction.UP;
|
| 443 | case Z -> Direction.SOUTH;
|
| 444 | };
|
| 445 | }
|
| 446 |
|
| 447 | public Direction getNegative() {
|
| 448 | return switch (this) {
|
| 449 | case X -> Direction.WEST;
|
| 450 | case Y -> Direction.DOWN;
|
| 451 | case Z -> Direction.NORTH;
|
| 452 | };
|
| 453 | }
|
| 454 |
|
| 455 | public Direction[] getDirections() {
|
| 456 | return new Direction[]{this.getPositive(), this.getNegative()};
|
| 457 | }
|
| 458 |
|
| 459 | @Override
|
| 460 | public String toString() {
|
| 461 | return this.name;
|
| 462 | }
|
| 463 |
|
| 464 | public static Direction.Axis getRandom(final RandomSource random) {
|
| 465 | return Util.getRandom(VALUES, random);
|
| 466 | }
|
| 467 |
|
| 468 | public boolean test(final @Nullable Direction input) {
|
| 469 | return input != null && input.getAxis() == this;
|
| 470 | }
|
| 471 |
|
| 472 | public Direction.Plane getPlane() {
|
| 473 | return switch (this) {
|
| 474 | case X, Z -> Direction.Plane.HORIZONTAL;
|
| 475 | case Y -> Direction.Plane.VERTICAL;
|
| 476 | };
|
| 477 | }
|
| 478 |
|
| 479 | @Override
|
| 480 | public String getSerializedName() {
|
| 481 | return this.name;
|
| 482 | }
|
| 483 |
|
| 484 | public int choose(final int x, final int y, final int z) {
|
| 485 | return switch (this) {
|
| 486 | case X -> x;
|
| 487 | case Y -> y;
|
| 488 | case Z -> z;
|
| 489 | };
|
| 490 | }
|
| 491 |
|
| 492 | public double choose(final double x, final double y, final double z) {
|
| 493 | return switch (this) {
|
| 494 | case X -> x;
|
| 495 | case Y -> y;
|
| 496 | case Z -> z;
|
| 497 | };
|
| 498 | }
|
| 499 |
|
| 500 | public boolean choose(final boolean x, final boolean y, final boolean z) {
|
| 501 | return switch (this) {
|
| 502 | case X -> x;
|
| 503 | case Y -> y;
|
| 504 | case Z -> z;
|
| 505 | };
|
| 506 | }
|
| 507 | }
|
| 508 |
|
| 509 | public enum AxisDirection {
|
| 510 | POSITIVE(1, "Towards positive"),
|
| 511 | NEGATIVE(-1, "Towards negative");
|
| 512 |
|
| 513 | private final int step;
|
| 514 | private final String name;
|
| 515 |
|
| 516 | AxisDirection(final int step, final String name) {
|
| 517 | this.step = step;
|
| 518 | this.name = name;
|
| 519 | }
|
| 520 |
|
| 521 | public int getStep() {
|
| 522 | return this.step;
|
| 523 | }
|
| 524 |
|
| 525 | public String getName() {
|
| 526 | return this.name;
|
| 527 | }
|
| 528 |
|
| 529 | @Override
|
| 530 | public String toString() {
|
| 531 | return this.name;
|
| 532 | }
|
| 533 |
|
| 534 | public Direction.AxisDirection opposite() {
|
| 535 | return this == POSITIVE ? NEGATIVE : POSITIVE;
|
| 536 | }
|
| 537 | }
|
| 538 |
|
| 539 | public enum Plane implements Predicate<Direction>, Iterable<Direction> {
|
| 540 | HORIZONTAL(new Direction[]{Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST}, new Direction.Axis[]{Direction.Axis.X, Direction.Axis.Z}),
|
| 541 | VERTICAL(new Direction[]{Direction.UP, Direction.DOWN}, new Direction.Axis[]{Direction.Axis.Y});
|
| 542 |
|
| 543 | private final Direction[] faces;
|
| 544 | private final Direction.Axis[] axis;
|
| 545 |
|
| 546 | Plane(final Direction[] faces, final Direction.Axis[] axis) {
|
| 547 | this.faces = faces;
|
| 548 | this.axis = axis;
|
| 549 | }
|
| 550 |
|
| 551 | public Direction getRandomDirection(final RandomSource random) {
|
| 552 | return Util.getRandom(this.faces, random);
|
| 553 | }
|
| 554 |
|
| 555 | public Direction.Axis getRandomAxis(final RandomSource random) {
|
| 556 | return Util.getRandom(this.axis, random);
|
| 557 | }
|
| 558 |
|
| 559 | public boolean test(final @Nullable Direction input) {
|
| 560 | return input != null && input.getAxis().getPlane() == this;
|
| 561 | }
|
| 562 |
|
| 563 | @Override
|
| 564 | public Iterator<Direction> iterator() {
|
| 565 | return Iterators.forArray(this.faces);
|
| 566 | }
|
| 567 |
|
| 568 | public Stream<Direction> stream() {
|
| 569 | return Arrays.stream(this.faces);
|
| 570 | }
|
| 571 |
|
| 572 | public List<Direction> shuffledCopy(final RandomSource random) {
|
| 573 | return Util.shuffledCopy(this.faces, random);
|
| 574 | }
|
| 575 |
|
| 576 | public int length() {
|
| 577 | return this.faces.length;
|
| 578 | }
|
| 579 | }
|
| 580 | }
|
| 581 |
|