| 1 | package net.minecraft.world.level.redstone;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.EnumMap;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Objects;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.Vec3i;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.util.Util;
|
| 15 |
|
| 16 | public class Orientation {
|
| 17 | public static final StreamCodec<ByteBuf, Orientation> STREAM_CODEC = ByteBufCodecs.idMapper(Orientation::fromIndex, Orientation::getIndex);
|
| 18 | private static final Orientation[] ORIENTATIONS = Util.make(() -> {
|
| 19 | Orientation[] orientations = new Orientation[48];
|
| 20 | generateContext(new Orientation(Direction.UP, Direction.NORTH, Orientation.SideBias.LEFT), orientations);
|
| 21 | return orientations;
|
| 22 | });
|
| 23 | private final Direction up;
|
| 24 | private final Direction front;
|
| 25 | private final Direction side;
|
| 26 | private final Orientation.SideBias sideBias;
|
| 27 | private final int index;
|
| 28 | private final List<Direction> neighbors;
|
| 29 | private final List<Direction> horizontalNeighbors;
|
| 30 | private final List<Direction> verticalNeighbors;
|
| 31 | private final Map<Direction, Orientation> withFront = new EnumMap<>(Direction.class);
|
| 32 | private final Map<Direction, Orientation> withUp = new EnumMap<>(Direction.class);
|
| 33 | private final Map<Orientation.SideBias, Orientation> withSideBias = new EnumMap<>(Orientation.SideBias.class);
|
| 34 |
|
| 35 | private Orientation(final Direction up, final Direction front, final Orientation.SideBias sideBias) {
|
| 36 | this.up = up;
|
| 37 | this.front = front;
|
| 38 | this.sideBias = sideBias;
|
| 39 | this.index = generateIndex(up, front, sideBias);
|
| 40 | Vec3i rightVector = front.getUnitVec3i().cross(up.getUnitVec3i());
|
| 41 | Direction side = Direction.getNearest(rightVector, null);
|
| 42 | Objects.requireNonNull(side);
|
| 43 | if (this.sideBias == Orientation.SideBias.RIGHT) {
|
| 44 | this.side = side;
|
| 45 | } else {
|
| 46 | this.side = side.getOpposite();
|
| 47 | }
|
| 48 |
|
| 49 | this.neighbors = List.of(this.front.getOpposite(), this.front, this.side, this.side.getOpposite(), this.up.getOpposite(), this.up);
|
| 50 | this.horizontalNeighbors = this.neighbors.stream().filter(d -> d.getAxis() != this.up.getAxis()).toList();
|
| 51 | this.verticalNeighbors = this.neighbors.stream().filter(d -> d.getAxis() == this.up.getAxis()).toList();
|
| 52 | }
|
| 53 |
|
| 54 | public static Orientation of(final Direction up, final Direction front, final Orientation.SideBias sideBias) {
|
| 55 | return ORIENTATIONS[generateIndex(up, front, sideBias)];
|
| 56 | }
|
| 57 |
|
| 58 | public Orientation withUp(final Direction up) {
|
| 59 | return this.withUp.get(up);
|
| 60 | }
|
| 61 |
|
| 62 | public Orientation withFront(final Direction front) {
|
| 63 | return this.withFront.get(front);
|
| 64 | }
|
| 65 |
|
| 66 | public Orientation withFrontPreserveUp(final Direction front) {
|
| 67 | return front.getAxis() == this.up.getAxis() ? this : this.withFront.get(front);
|
| 68 | }
|
| 69 |
|
| 70 | public Orientation withFrontAdjustSideBias(final Direction front) {
|
| 71 | Orientation withFront = this.withFront(front);
|
| 72 | return this.front == withFront.side ? withFront.withMirror() : withFront;
|
| 73 | }
|
| 74 |
|
| 75 | public Orientation withSideBias(final Orientation.SideBias sideBias) {
|
| 76 | return this.withSideBias.get(sideBias);
|
| 77 | }
|
| 78 |
|
| 79 | public Orientation withMirror() {
|
| 80 | return this.withSideBias(this.sideBias.getOpposite());
|
| 81 | }
|
| 82 |
|
| 83 | public Direction getFront() {
|
| 84 | return this.front;
|
| 85 | }
|
| 86 |
|
| 87 | public Direction getUp() {
|
| 88 | return this.up;
|
| 89 | }
|
| 90 |
|
| 91 | public Direction getSide() {
|
| 92 | return this.side;
|
| 93 | }
|
| 94 |
|
| 95 | public Orientation.SideBias getSideBias() {
|
| 96 | return this.sideBias;
|
| 97 | }
|
| 98 |
|
| 99 | public List<Direction> getDirections() {
|
| 100 | return this.neighbors;
|
| 101 | }
|
| 102 |
|
| 103 | public List<Direction> getHorizontalDirections() {
|
| 104 | return this.horizontalNeighbors;
|
| 105 | }
|
| 106 |
|
| 107 | public List<Direction> getVerticalDirections() {
|
| 108 | return this.verticalNeighbors;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public String toString() {
|
| 113 | return "[up=" + this.up + ",front=" + this.front + ",sideBias=" + this.sideBias + "]";
|
| 114 | }
|
| 115 |
|
| 116 | public int getIndex() {
|
| 117 | return this.index;
|
| 118 | }
|
| 119 |
|
| 120 | public static Orientation fromIndex(final int index) {
|
| 121 | return ORIENTATIONS[index];
|
| 122 | }
|
| 123 |
|
| 124 | public static Orientation random(final RandomSource rand) {
|
| 125 | return Util.getRandom(ORIENTATIONS, rand);
|
| 126 | }
|
| 127 |
|
| 128 | private static Orientation generateContext(final Orientation self, final Orientation[] lookup) {
|
| 129 | if (lookup[self.getIndex()] != null) {
|
| 130 | return lookup[self.getIndex()];
|
| 131 | }
|
| 132 |
|
| 133 | lookup[self.getIndex()] = self;
|
| 134 |
|
| 135 | for (Orientation.SideBias sideBias : Orientation.SideBias.values()) {
|
| 136 | self.withSideBias.put(sideBias, generateContext(new Orientation(self.up, self.front, sideBias), lookup));
|
| 137 | }
|
| 138 |
|
| 139 | for (Direction facing : Direction.values()) {
|
| 140 | Direction up = self.up;
|
| 141 | if (facing == self.up) {
|
| 142 | up = self.front.getOpposite();
|
| 143 | }
|
| 144 |
|
| 145 | if (facing == self.up.getOpposite()) {
|
| 146 | up = self.front;
|
| 147 | }
|
| 148 |
|
| 149 | self.withFront.put(facing, generateContext(new Orientation(up, facing, self.sideBias), lookup));
|
| 150 | }
|
| 151 |
|
| 152 | for (Direction facing : Direction.values()) {
|
| 153 | Direction front = self.front;
|
| 154 | if (facing == self.front) {
|
| 155 | front = self.up.getOpposite();
|
| 156 | }
|
| 157 |
|
| 158 | if (facing == self.front.getOpposite()) {
|
| 159 | front = self.up;
|
| 160 | }
|
| 161 |
|
| 162 | self.withUp.put(facing, generateContext(new Orientation(facing, front, self.sideBias), lookup));
|
| 163 | }
|
| 164 |
|
| 165 | return self;
|
| 166 | }
|
| 167 |
|
| 168 | @VisibleForTesting
|
| 169 | protected static int generateIndex(final Direction up, final Direction front, final Orientation.SideBias sideBias) {
|
| 170 | if (up.getAxis() == front.getAxis()) {
|
| 171 | throw new IllegalStateException("Up-vector and front-vector can not be on the same axis");
|
| 172 | }
|
| 173 |
|
| 174 | int frontAxisKey;
|
| 175 | if (up.getAxis() == Direction.Axis.Y) {
|
| 176 | frontAxisKey = front.getAxis() == Direction.Axis.X ? 1 : 0;
|
| 177 | } else {
|
| 178 | frontAxisKey = front.getAxis() == Direction.Axis.Y ? 1 : 0;
|
| 179 | }
|
| 180 |
|
| 181 | int frontKey = frontAxisKey << 1 | front.getAxisDirection().ordinal();
|
| 182 | return ((up.ordinal() << 2) + frontKey << 1) + sideBias.ordinal();
|
| 183 | }
|
| 184 |
|
| 185 | public enum SideBias {
|
| 186 | LEFT("left"),
|
| 187 | RIGHT("right");
|
| 188 |
|
| 189 | private final String name;
|
| 190 |
|
| 191 | SideBias(final String name) {
|
| 192 | this.name = name;
|
| 193 | }
|
| 194 |
|
| 195 | public Orientation.SideBias getOpposite() {
|
| 196 | return this == LEFT ? RIGHT : LEFT;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public String toString() {
|
| 201 | return this.name;
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 |
|