| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.EnumSet;
|
| 5 | import java.util.HashSet;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 |
|
| 10 | public enum Relative {
|
| 11 | X(0),
|
| 12 | Y(1),
|
| 13 | Z(2),
|
| 14 | Y_ROT(3),
|
| 15 | X_ROT(4),
|
| 16 | DELTA_X(5),
|
| 17 | DELTA_Y(6),
|
| 18 | DELTA_Z(7),
|
| 19 | ROTATE_DELTA(8);
|
| 20 |
|
| 21 | public static final Set<Relative> ALL = Set.of(values());
|
| 22 | public static final Set<Relative> ROTATION = Set.of(X_ROT, Y_ROT);
|
| 23 | public static final Set<Relative> DELTA = Set.of(DELTA_X, DELTA_Y, DELTA_Z, ROTATE_DELTA);
|
| 24 | public static final StreamCodec<ByteBuf, Set<Relative>> SET_STREAM_CODEC = ByteBufCodecs.INT.map(Relative::unpack, Relative::pack);
|
| 25 | private final int bit;
|
| 26 |
|
| 27 | @SafeVarargs
|
| 28 | public static Set<Relative> union(final Set<Relative>... sets) {
|
| 29 | HashSet<Relative> set = new HashSet<>();
|
| 30 |
|
| 31 | for (Set<Relative> s : sets) {
|
| 32 | set.addAll(s);
|
| 33 | }
|
| 34 |
|
| 35 | return set;
|
| 36 | }
|
| 37 |
|
| 38 | public static Set<Relative> rotation(final boolean relativeYRot, final boolean relativeXRot) {
|
| 39 | Set<Relative> relatives = EnumSet.noneOf(Relative.class);
|
| 40 | if (relativeYRot) {
|
| 41 | relatives.add(Y_ROT);
|
| 42 | }
|
| 43 |
|
| 44 | if (relativeXRot) {
|
| 45 | relatives.add(X_ROT);
|
| 46 | }
|
| 47 |
|
| 48 | return relatives;
|
| 49 | }
|
| 50 |
|
| 51 | public static Set<Relative> position(final boolean relativeX, final boolean relativeY, final boolean relativeZ) {
|
| 52 | Set<Relative> relatives = EnumSet.noneOf(Relative.class);
|
| 53 | if (relativeX) {
|
| 54 | relatives.add(X);
|
| 55 | }
|
| 56 |
|
| 57 | if (relativeY) {
|
| 58 | relatives.add(Y);
|
| 59 | }
|
| 60 |
|
| 61 | if (relativeZ) {
|
| 62 | relatives.add(Z);
|
| 63 | }
|
| 64 |
|
| 65 | return relatives;
|
| 66 | }
|
| 67 |
|
| 68 | public static Set<Relative> direction(final boolean relativeX, final boolean relativeY, final boolean relativeZ) {
|
| 69 | Set<Relative> relatives = EnumSet.noneOf(Relative.class);
|
| 70 | if (relativeX) {
|
| 71 | relatives.add(DELTA_X);
|
| 72 | }
|
| 73 |
|
| 74 | if (relativeY) {
|
| 75 | relatives.add(DELTA_Y);
|
| 76 | }
|
| 77 |
|
| 78 | if (relativeZ) {
|
| 79 | relatives.add(DELTA_Z);
|
| 80 | }
|
| 81 |
|
| 82 | return relatives;
|
| 83 | }
|
| 84 |
|
| 85 | Relative(final int bit) {
|
| 86 | this.bit = bit;
|
| 87 | }
|
| 88 |
|
| 89 | private int getMask() {
|
| 90 | return 1 << this.bit;
|
| 91 | }
|
| 92 |
|
| 93 | private boolean isSet(final int value) {
|
| 94 | return (value & this.getMask()) == this.getMask();
|
| 95 | }
|
| 96 |
|
| 97 | public static Set<Relative> unpack(final int value) {
|
| 98 | Set<Relative> result = EnumSet.noneOf(Relative.class);
|
| 99 |
|
| 100 | for (Relative argument : values()) {
|
| 101 | if (argument.isSet(value)) {
|
| 102 | result.add(argument);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | return result;
|
| 107 | }
|
| 108 |
|
| 109 | public static int pack(final Set<Relative> set) {
|
| 110 | int result = 0;
|
| 111 |
|
| 112 | for (Relative argument : set) {
|
| 113 | result |= argument.getMask();
|
| 114 | }
|
| 115 |
|
| 116 | return result;
|
| 117 | }
|
| 118 | }
|
| 119 |
|