| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import io.netty.buffer.ByteBuf;
|
| 7 | import java.util.stream.IntStream;
|
| 8 | import javax.annotation.concurrent.Immutable;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import org.joml.Vector3i;
|
| 14 |
|
| 15 | @Immutable
|
| 16 | public class Vec3i implements Comparable<Vec3i> {
|
| 17 | public static final Codec<Vec3i> CODEC = Codec.INT_STREAM
|
| 18 | .comapFlatMap(input -> Util.fixedSize(input, 3).map(ints -> new Vec3i(ints[0], ints[1], ints[2])), pos -> IntStream.of(pos.getX(), pos.getY(), pos.getZ()));
|
| 19 | public static final StreamCodec<ByteBuf, Vec3i> STREAM_CODEC = StreamCodec.composite(
|
| 20 | ByteBufCodecs.VAR_INT, Vec3i::getX, ByteBufCodecs.VAR_INT, Vec3i::getY, ByteBufCodecs.VAR_INT, Vec3i::getZ, Vec3i::new
|
| 21 | );
|
| 22 | public static final Vec3i ZERO = new Vec3i(0, 0, 0);
|
| 23 | private int x;
|
| 24 | private int y;
|
| 25 | private int z;
|
| 26 |
|
| 27 | public static Codec<Vec3i> offsetCodec(final int maxOffsetPerAxis) {
|
| 28 | return CODEC.validate(
|
| 29 | value -> Math.abs(value.getX()) <= maxOffsetPerAxis && Math.abs(value.getY()) <= maxOffsetPerAxis && Math.abs(value.getZ()) <= maxOffsetPerAxis
|
| 30 | ? DataResult.success(value)
|
| 31 | : DataResult.error(() -> "Position out of range, expected at most " + maxOffsetPerAxis + ": " + value)
|
| 32 | );
|
| 33 | }
|
| 34 |
|
| 35 | public Vec3i(final int x, final int y, final int z) {
|
| 36 | this.x = x;
|
| 37 | this.y = y;
|
| 38 | this.z = z;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean equals(final Object o) {
|
| 43 | if (this == o) {
|
| 44 | return true;
|
| 45 | } else {
|
| 46 | return !(o instanceof Vec3i vec3i) ? false : this.getX() == vec3i.getX() && this.getY() == vec3i.getY() && this.getZ() == vec3i.getZ();
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public int hashCode() {
|
| 52 | return (this.getY() + this.getZ() * 31) * 31 + this.getX();
|
| 53 | }
|
| 54 |
|
| 55 | public int compareTo(final Vec3i pos) {
|
| 56 | if (this.getY() == pos.getY()) {
|
| 57 | return this.getZ() == pos.getZ() ? this.getX() - pos.getX() : this.getZ() - pos.getZ();
|
| 58 | } else {
|
| 59 | return this.getY() - pos.getY();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | public int getX() {
|
| 64 | return this.x;
|
| 65 | }
|
| 66 |
|
| 67 | public int getY() {
|
| 68 | return this.y;
|
| 69 | }
|
| 70 |
|
| 71 | public int getZ() {
|
| 72 | return this.z;
|
| 73 | }
|
| 74 |
|
| 75 | protected Vec3i setX(final int x) {
|
| 76 | this.x = x;
|
| 77 | return this;
|
| 78 | }
|
| 79 |
|
| 80 | protected Vec3i setY(final int y) {
|
| 81 | this.y = y;
|
| 82 | return this;
|
| 83 | }
|
| 84 |
|
| 85 | protected Vec3i setZ(final int z) {
|
| 86 | this.z = z;
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | public Vec3i offset(final int x, final int y, final int z) {
|
| 91 | return x == 0 && y == 0 && z == 0 ? this : new Vec3i(this.getX() + x, this.getY() + y, this.getZ() + z);
|
| 92 | }
|
| 93 |
|
| 94 | public Vec3i offset(final Vec3i vec) {
|
| 95 | return this.offset(vec.getX(), vec.getY(), vec.getZ());
|
| 96 | }
|
| 97 |
|
| 98 | public Vec3i subtract(final Vec3i vec) {
|
| 99 | return this.offset(-vec.getX(), -vec.getY(), -vec.getZ());
|
| 100 | }
|
| 101 |
|
| 102 | public Vec3i multiply(final int scale) {
|
| 103 | if (scale == 1) {
|
| 104 | return this;
|
| 105 | } else {
|
| 106 | return scale == 0 ? ZERO : new Vec3i(this.getX() * scale, this.getY() * scale, this.getZ() * scale);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | public Vec3i multiply(final int xScale, final int yScale, final int zScale) {
|
| 111 | return new Vec3i(this.getX() * xScale, this.getY() * yScale, this.getZ() * zScale);
|
| 112 | }
|
| 113 |
|
| 114 | public Vec3i above() {
|
| 115 | return this.above(1);
|
| 116 | }
|
| 117 |
|
| 118 | public Vec3i above(final int steps) {
|
| 119 | return this.relative(Direction.UP, steps);
|
| 120 | }
|
| 121 |
|
| 122 | public Vec3i below() {
|
| 123 | return this.below(1);
|
| 124 | }
|
| 125 |
|
| 126 | public Vec3i below(final int steps) {
|
| 127 | return this.relative(Direction.DOWN, steps);
|
| 128 | }
|
| 129 |
|
| 130 | public Vec3i north() {
|
| 131 | return this.north(1);
|
| 132 | }
|
| 133 |
|
| 134 | public Vec3i north(final int steps) {
|
| 135 | return this.relative(Direction.NORTH, steps);
|
| 136 | }
|
| 137 |
|
| 138 | public Vec3i south() {
|
| 139 | return this.south(1);
|
| 140 | }
|
| 141 |
|
| 142 | public Vec3i south(final int steps) {
|
| 143 | return this.relative(Direction.SOUTH, steps);
|
| 144 | }
|
| 145 |
|
| 146 | public Vec3i west() {
|
| 147 | return this.west(1);
|
| 148 | }
|
| 149 |
|
| 150 | public Vec3i west(final int steps) {
|
| 151 | return this.relative(Direction.WEST, steps);
|
| 152 | }
|
| 153 |
|
| 154 | public Vec3i east() {
|
| 155 | return this.east(1);
|
| 156 | }
|
| 157 |
|
| 158 | public Vec3i east(final int steps) {
|
| 159 | return this.relative(Direction.EAST, steps);
|
| 160 | }
|
| 161 |
|
| 162 | public Vec3i relative(final Direction direction) {
|
| 163 | return this.relative(direction, 1);
|
| 164 | }
|
| 165 |
|
| 166 | public Vec3i relative(final Direction direction, final int steps) {
|
| 167 | return steps == 0
|
| 168 | ? this
|
| 169 | : new Vec3i(this.getX() + direction.getStepX() * steps, this.getY() + direction.getStepY() * steps, this.getZ() + direction.getStepZ() * steps);
|
| 170 | }
|
| 171 |
|
| 172 | public Vec3i relative(final Direction.Axis axis, final int steps) {
|
| 173 | if (steps == 0) {
|
| 174 | return this;
|
| 175 | }
|
| 176 |
|
| 177 | int xStep = axis == Direction.Axis.X ? steps : 0;
|
| 178 | int yStep = axis == Direction.Axis.Y ? steps : 0;
|
| 179 | int zStep = axis == Direction.Axis.Z ? steps : 0;
|
| 180 | return new Vec3i(this.getX() + xStep, this.getY() + yStep, this.getZ() + zStep);
|
| 181 | }
|
| 182 |
|
| 183 | public Vec3i cross(final Vec3i upVector) {
|
| 184 | return new Vec3i(
|
| 185 | this.getY() * upVector.getZ() - this.getZ() * upVector.getY(),
|
| 186 | this.getZ() * upVector.getX() - this.getX() * upVector.getZ(),
|
| 187 | this.getX() * upVector.getY() - this.getY() * upVector.getX()
|
| 188 | );
|
| 189 | }
|
| 190 |
|
| 191 | public boolean closerThan(final Vec3i pos, final double distance) {
|
| 192 | return this.distSqr(pos) < Mth.square(distance);
|
| 193 | }
|
| 194 |
|
| 195 | public boolean closerToCenterThan(final Position pos, final double distance) {
|
| 196 | return this.distToCenterSqr(pos) < Mth.square(distance);
|
| 197 | }
|
| 198 |
|
| 199 | public double distSqr(final Vec3i pos) {
|
| 200 | return this.distToLowCornerSqr(pos.getX(), pos.getY(), pos.getZ());
|
| 201 | }
|
| 202 |
|
| 203 | public double distToCenterSqr(final Position pos) {
|
| 204 | return this.distToCenterSqr(pos.x(), pos.y(), pos.z());
|
| 205 | }
|
| 206 |
|
| 207 | public double distToCenterSqr(final double x, final double y, final double z) {
|
| 208 | double dx = this.getX() + 0.5 - x;
|
| 209 | double dy = this.getY() + 0.5 - y;
|
| 210 | double dz = this.getZ() + 0.5 - z;
|
| 211 | return dx * dx + dy * dy + dz * dz;
|
| 212 | }
|
| 213 |
|
| 214 | public double distToLowCornerSqr(final double x, final double y, final double z) {
|
| 215 | double dx = this.getX() - x;
|
| 216 | double dy = this.getY() - y;
|
| 217 | double dz = this.getZ() - z;
|
| 218 | return dx * dx + dy * dy + dz * dz;
|
| 219 | }
|
| 220 |
|
| 221 | public int distManhattan(final Vec3i pos) {
|
| 222 | float xd = Math.abs(pos.getX() - this.getX());
|
| 223 | float yd = Math.abs(pos.getY() - this.getY());
|
| 224 | float zd = Math.abs(pos.getZ() - this.getZ());
|
| 225 | return (int)(xd + yd + zd);
|
| 226 | }
|
| 227 |
|
| 228 | public int distChessboard(final Vec3i pos) {
|
| 229 | int xd = Math.abs(this.getX() - pos.getX());
|
| 230 | int yd = Math.abs(this.getY() - pos.getY());
|
| 231 | int zd = Math.abs(this.getZ() - pos.getZ());
|
| 232 | return Math.max(Math.max(xd, yd), zd);
|
| 233 | }
|
| 234 |
|
| 235 | public boolean differsHorizontally(final Vec3i pos) {
|
| 236 | return this.x != pos.x || this.z != pos.z;
|
| 237 | }
|
| 238 |
|
| 239 | public int get(final Direction.Axis axis) {
|
| 240 | return axis.choose(this.x, this.y, this.z);
|
| 241 | }
|
| 242 |
|
| 243 | public Vector3i toMutable() {
|
| 244 | return new Vector3i(this.x, this.y, this.z);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public String toString() {
|
| 249 | return MoreObjects.toStringHelper(this).add("x", this.getX()).add("y", this.getY()).add("z", this.getZ()).toString();
|
| 250 | }
|
| 251 |
|
| 252 | public String toShortString() {
|
| 253 | return this.getX() + ", " + this.getY() + ", " + this.getZ();
|
| 254 | }
|
| 255 | }
|
| 256 |
|