| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.math.OctahedralGroup;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.IntFunction;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.util.ByIdMap;
|
| 12 | import net.minecraft.util.ExtraCodecs;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.util.StringRepresentable;
|
| 15 | import net.minecraft.util.Util;
|
| 16 |
|
| 17 | public enum Rotation implements StringRepresentable {
|
| 18 | NONE(0, "none", OctahedralGroup.IDENTITY),
|
| 19 | CLOCKWISE_90(1, "clockwise_90", OctahedralGroup.ROT_90_Y_NEG),
|
| 20 | CLOCKWISE_180(2, "180", OctahedralGroup.ROT_180_FACE_XZ),
|
| 21 | COUNTERCLOCKWISE_90(3, "counterclockwise_90", OctahedralGroup.ROT_90_Y_POS);
|
| 22 |
|
| 23 | public static final IntFunction<Rotation> BY_ID = ByIdMap.continuous(Rotation::getIndex, values(), ByIdMap.OutOfBoundsStrategy.WRAP);
|
| 24 | public static final Codec<Rotation> CODEC = StringRepresentable.fromEnum(Rotation::values);
|
| 25 | public static final StreamCodec<ByteBuf, Rotation> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Rotation::getIndex);
|
| 26 | @Deprecated
|
| 27 | public static final Codec<Rotation> LEGACY_CODEC = ExtraCodecs.legacyEnum(Rotation::valueOf);
|
| 28 | private final int index;
|
| 29 | private final String id;
|
| 30 | private final OctahedralGroup rotation;
|
| 31 |
|
| 32 | Rotation(final int index, final String id, final OctahedralGroup rotation) {
|
| 33 | this.index = index;
|
| 34 | this.id = id;
|
| 35 | this.rotation = rotation;
|
| 36 | }
|
| 37 |
|
| 38 | public Rotation getRotated(final Rotation rot) {
|
| 39 | return switch (rot) {
|
| 40 | case CLOCKWISE_90 -> {
|
| 41 | switch (this) {
|
| 42 | case NONE:
|
| 43 | yield CLOCKWISE_90;
|
| 44 | case CLOCKWISE_90:
|
| 45 | yield CLOCKWISE_180;
|
| 46 | case CLOCKWISE_180:
|
| 47 | yield COUNTERCLOCKWISE_90;
|
| 48 | case COUNTERCLOCKWISE_90:
|
| 49 | yield NONE;
|
| 50 | default:
|
| 51 | throw new MatchException(null, null);
|
| 52 | }
|
| 53 | }
|
| 54 | case CLOCKWISE_180 -> {
|
| 55 | switch (this) {
|
| 56 | case NONE:
|
| 57 | yield CLOCKWISE_180;
|
| 58 | case CLOCKWISE_90:
|
| 59 | yield COUNTERCLOCKWISE_90;
|
| 60 | case CLOCKWISE_180:
|
| 61 | yield NONE;
|
| 62 | case COUNTERCLOCKWISE_90:
|
| 63 | yield CLOCKWISE_90;
|
| 64 | default:
|
| 65 | throw new MatchException(null, null);
|
| 66 | }
|
| 67 | }
|
| 68 | case COUNTERCLOCKWISE_90 -> {
|
| 69 | switch (this) {
|
| 70 | case NONE:
|
| 71 | yield COUNTERCLOCKWISE_90;
|
| 72 | case CLOCKWISE_90:
|
| 73 | yield NONE;
|
| 74 | case CLOCKWISE_180:
|
| 75 | yield CLOCKWISE_90;
|
| 76 | case COUNTERCLOCKWISE_90:
|
| 77 | yield CLOCKWISE_180;
|
| 78 | default:
|
| 79 | throw new MatchException(null, null);
|
| 80 | }
|
| 81 | }
|
| 82 | default -> this;
|
| 83 | };
|
| 84 | }
|
| 85 |
|
| 86 | public OctahedralGroup rotation() {
|
| 87 | return this.rotation;
|
| 88 | }
|
| 89 |
|
| 90 | public Direction rotate(final Direction direction) {
|
| 91 | if (direction.getAxis() == Direction.Axis.Y) {
|
| 92 | return direction;
|
| 93 | }
|
| 94 |
|
| 95 | return switch (this) {
|
| 96 | case CLOCKWISE_90 -> direction.getClockWise();
|
| 97 | case CLOCKWISE_180 -> direction.getOpposite();
|
| 98 | case COUNTERCLOCKWISE_90 -> direction.getCounterClockWise();
|
| 99 | default -> direction;
|
| 100 | };
|
| 101 | }
|
| 102 |
|
| 103 | public int rotate(final int rotation, final int steps) {
|
| 104 | return switch (this) {
|
| 105 | case CLOCKWISE_90 -> (rotation + steps / 4) % steps;
|
| 106 | case CLOCKWISE_180 -> (rotation + steps / 2) % steps;
|
| 107 | case COUNTERCLOCKWISE_90 -> (rotation + steps * 3 / 4) % steps;
|
| 108 | default -> rotation;
|
| 109 | };
|
| 110 | }
|
| 111 |
|
| 112 | public static Rotation getRandom(final RandomSource random) {
|
| 113 | return Util.getRandom(values(), random);
|
| 114 | }
|
| 115 |
|
| 116 | public static List<Rotation> getShuffled(final RandomSource random) {
|
| 117 | return Util.shuffledCopy(values(), random);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public String getSerializedName() {
|
| 122 | return this.id;
|
| 123 | }
|
| 124 |
|
| 125 | private int getIndex() {
|
| 126 | return this.index;
|
| 127 | }
|
| 128 | }
|
| 129 |
|