| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.EnumSet;
|
| 7 | import java.util.Set;
|
| 8 |
|
| 9 | public record CompositeDirection(Set<Direction> directions, Vec3i step) implements Directional {
|
| 10 | public static final CompositeDirection NORTH = new CompositeDirection(Direction.NORTH);
|
| 11 | public static final CompositeDirection EAST = new CompositeDirection(Direction.EAST);
|
| 12 | public static final CompositeDirection SOUTH = new CompositeDirection(Direction.SOUTH);
|
| 13 | public static final CompositeDirection WEST = new CompositeDirection(Direction.WEST);
|
| 14 | public static final CompositeDirection NORTH_EAST = NORTH.compose(EAST);
|
| 15 | public static final CompositeDirection SOUTH_EAST = SOUTH.compose(EAST);
|
| 16 | public static final CompositeDirection SOUTH_WEST = SOUTH.compose(WEST);
|
| 17 | public static final CompositeDirection NORTH_WEST = NORTH.compose(WEST);
|
| 18 | public static final CompositeDirection UP = new CompositeDirection(Direction.UP);
|
| 19 | public static final CompositeDirection NORTH_UP = NORTH.compose(UP);
|
| 20 | public static final CompositeDirection EAST_UP = EAST.compose(UP);
|
| 21 | public static final CompositeDirection SOUTH_UP = SOUTH.compose(UP);
|
| 22 | public static final CompositeDirection WEST_UP = WEST.compose(UP);
|
| 23 | public static final CompositeDirection NORTH_EAST_UP = NORTH_EAST.compose(UP);
|
| 24 | public static final CompositeDirection SOUTH_EAST_UP = SOUTH_EAST.compose(UP);
|
| 25 | public static final CompositeDirection SOUTH_WEST_UP = SOUTH_WEST.compose(UP);
|
| 26 | public static final CompositeDirection NORTH_WEST_UP = NORTH_WEST.compose(UP);
|
| 27 | public static final CompositeDirection DOWN = new CompositeDirection(Direction.DOWN);
|
| 28 | public static final CompositeDirection NORTH_DOWN = NORTH.compose(DOWN);
|
| 29 | public static final CompositeDirection EAST_DOWN = EAST.compose(DOWN);
|
| 30 | public static final CompositeDirection SOUTH_DOWN = SOUTH.compose(DOWN);
|
| 31 | public static final CompositeDirection WEST_DOWN = WEST.compose(DOWN);
|
| 32 | public static final CompositeDirection NORTH_EAST_DOWN = NORTH_EAST.compose(DOWN);
|
| 33 | public static final CompositeDirection SOUTH_EAST_DOWN = SOUTH_EAST.compose(DOWN);
|
| 34 | public static final CompositeDirection SOUTH_WEST_DOWN = SOUTH_WEST.compose(DOWN);
|
| 35 | public static final CompositeDirection NORTH_WEST_DOWN = NORTH_WEST.compose(DOWN);
|
| 36 |
|
| 37 | public CompositeDirection {
|
| 38 | if (directions.isEmpty()) {
|
| 39 | throw new IllegalArgumentException("Directions cannot be empty");
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | @VisibleForTesting
|
| 44 | CompositeDirection(final Direction... directions) {
|
| 45 | Set<Direction> immutableDirections = Sets.immutableEnumSet(Arrays.asList(directions));
|
| 46 | int x = 0;
|
| 47 | int y = 0;
|
| 48 | int z = 0;
|
| 49 |
|
| 50 | for (Direction direction : immutableDirections) {
|
| 51 | x += direction.getStepX();
|
| 52 | y += direction.getStepY();
|
| 53 | z += direction.getStepZ();
|
| 54 | }
|
| 55 |
|
| 56 | this(immutableDirections, new Vec3i(x, y, z));
|
| 57 | }
|
| 58 |
|
| 59 | public CompositeDirection compose(final CompositeDirection other) {
|
| 60 | Set<Direction> newDirections = EnumSet.copyOf(this.directions);
|
| 61 | newDirections.addAll(other.directions);
|
| 62 | Vec3i step = new Vec3i(this.getStepX(), this.getStepY(), this.getStepZ());
|
| 63 | Vec3i additionalStep = other.getStep();
|
| 64 | step.setX(step.getX() + additionalStep.getX()).setY(step.getY() + additionalStep.getY()).setZ(step.getZ() + additionalStep.getZ());
|
| 65 | return new CompositeDirection(Sets.immutableEnumSet(newDirections), step);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public int getStepX() {
|
| 70 | return this.step.getX();
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public int getStepY() {
|
| 75 | return this.step.getY();
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public int getStepZ() {
|
| 80 | return this.step.getZ();
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public Vec3i getStep() {
|
| 85 | return this.step;
|
| 86 | }
|
| 87 |
|
| 88 | public enum Direction8 implements Directional {
|
| 89 | NORTH(CompositeDirection.NORTH),
|
| 90 | NORTH_EAST(CompositeDirection.NORTH_EAST),
|
| 91 | EAST(CompositeDirection.EAST),
|
| 92 | SOUTH_EAST(CompositeDirection.SOUTH_EAST),
|
| 93 | SOUTH(CompositeDirection.SOUTH),
|
| 94 | SOUTH_WEST(CompositeDirection.SOUTH_WEST),
|
| 95 | WEST(CompositeDirection.WEST),
|
| 96 | NORTH_WEST(CompositeDirection.NORTH_WEST);
|
| 97 |
|
| 98 | private final CompositeDirection compositeDirection;
|
| 99 |
|
| 100 | Direction8(final CompositeDirection compositeDirection) {
|
| 101 | this.compositeDirection = compositeDirection;
|
| 102 | }
|
| 103 |
|
| 104 | public Set<Direction> getDirections() {
|
| 105 | return this.compositeDirection.directions();
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public int getStepX() {
|
| 110 | return this.compositeDirection.getStepX();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public int getStepY() {
|
| 115 | return this.compositeDirection.getStepY();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public int getStepZ() {
|
| 120 | return this.compositeDirection.getStepZ();
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public Vec3i getStep() {
|
| 125 | return this.compositeDirection.getStep();
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|