Skip to content

Commit 7daab90

Browse files
committed
Refactor Vec3i
1 parent 779d5f6 commit 7daab90

File tree

1 file changed

+66
-80
lines changed
  • src/main/java/com/falsepattern/lib/compat

1 file changed

+66
-80
lines changed
Lines changed: 66 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,107 @@
11
package com.falsepattern.lib.compat;
22

33
import com.falsepattern.lib.StableAPI;
4+
import lombok.*;
45
import net.minecraft.util.MathHelper;
56

67
import javax.annotation.concurrent.Immutable;
7-
import java.util.Objects;
88

9+
/**
10+
* A functional equivalent to Vec3i present in Minecraft 1.12
11+
*/
12+
@Getter
913
@Immutable
14+
@EqualsAndHashCode
15+
@AllArgsConstructor
1016
@StableAPI(since = "0.6.0")
1117
public class Vec3i implements Comparable<Vec3i> {
1218
/**
13-
* An immutable vector with zero as all coordinates.
19+
* A static zero vector
1420
*/
1521
public static final Vec3i NULL_VECTOR = new Vec3i(0, 0, 0);
16-
/**
17-
* X coordinate
18-
*/
19-
private final int x;
20-
/**
21-
* Y coordinate
22-
*/
23-
private final int y;
24-
/**
25-
* Z coordinate
26-
*/
27-
private final int z;
2822

29-
public Vec3i(int xIn, int yIn, int zIn) {
30-
this.x = xIn;
31-
this.y = yIn;
32-
this.z = zIn;
33-
}
34-
35-
public Vec3i(double xIn, double yIn, double zIn) {
36-
this(MathHelper.floor_double(xIn), MathHelper.floor_double(yIn), MathHelper.floor_double(zIn));
37-
}
38-
39-
public int compareTo(Vec3i p_compareTo_1_) {
40-
if (this.getY() == p_compareTo_1_.getY()) {
41-
return this.getZ() == p_compareTo_1_.getZ() ? this.getX() - p_compareTo_1_.getX() : this.getZ() - p_compareTo_1_.getZ();
42-
} else {
43-
return this.getY() - p_compareTo_1_.getY();
44-
}
45-
}
23+
protected final int x;
24+
protected final int y;
25+
protected final int z;
4626

4727
/**
48-
* Gets the X coordinate.
28+
* Instantiates a new vector.
29+
*
30+
* @param x the x
31+
* @param y the y
32+
* @param z the z
4933
*/
50-
public int getX() {
51-
return this.x;
34+
public Vec3i(double x, double y, double z) {
35+
this(MathHelper.floor_double(x), MathHelper.floor_double(y), MathHelper.floor_double(z));
5236
}
5337

54-
/**
55-
* Gets the Y coordinate.
56-
*/
57-
public int getY() {
58-
return this.y;
38+
public int compareTo(@NonNull Vec3i vec) {
39+
return y == vec.getY() ? z == vec.getZ() ? x - vec.getX() : z - vec.getZ() : y - vec.getY();
5940
}
6041

6142
/**
62-
* Gets the Z coordinate.
43+
* Cross product between two vectors.
44+
*
45+
* @param vec the other vector
46+
* @return the new resulting vector
6347
*/
64-
public int getZ() {
65-
return this.z;
48+
public Vec3i crossProduct(@NonNull Vec3i vec) {
49+
return new Vec3i(
50+
y * vec.getZ() - z * vec.getY(),
51+
z * vec.getX() - x * vec.getZ(),
52+
x * vec.getY() - y * vec.getX()
53+
);
6654
}
6755

6856
/**
69-
* Calculate the cross product of this and the given Vector
57+
* Gets distance to the vector.
58+
*
59+
* @param x the other x
60+
* @param y the other y
61+
* @param z the other z
62+
* @return the distance
7063
*/
71-
public Vec3i crossProduct(Vec3i vec) {
72-
return new Vec3i(this.getY() * vec.getZ() - this.getZ() * vec.getY(), this.getZ() * vec.getX() - this.getX() * vec.getZ(), this.getX() * vec.getY() - this.getY() * vec.getX());
73-
}
74-
75-
public double getDistance(int xIn, int yIn, int zIn) {
76-
double d0 = (this.getX() - xIn);
77-
double d1 = (this.getY() - yIn);
78-
double d2 = (this.getZ() - zIn);
79-
return Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
64+
public double getDistance(int x, int y, int z) {
65+
return Math.sqrt(distanceSq(x, y, z));
8066
}
8167

8268
/**
83-
* Calculate squared distance to the given coordinates
69+
* Square distance between vectors.
70+
*
71+
* @param vec the other vector
72+
* @return the square distance
8473
*/
85-
public double distanceSq(double toX, double toY, double toZ) {
86-
double d0 = (double) this.getX() - toX;
87-
double d1 = (double) this.getY() - toY;
88-
double d2 = (double) this.getZ() - toZ;
89-
return d0 * d0 + d1 * d1 + d2 * d2;
74+
public double distanceSq(@NonNull Vec3i vec) {
75+
return distanceSq(vec.getX(), vec.getY(), vec.getZ());
9076
}
9177

9278
/**
93-
* Compute square of distance from point x, y, z to center of this Block
79+
* Square root distance to a point.
80+
*
81+
* @param x the other x
82+
* @param y the other y
83+
* @param z the other z
84+
* @return the square distance
9485
*/
95-
public double distanceSqToCenter(double xIn, double yIn, double zIn) {
96-
double d0 = (double) this.getX() + 0.5D - xIn;
97-
double d1 = (double) this.getY() + 0.5D - yIn;
98-
double d2 = (double) this.getZ() + 0.5D - zIn;
99-
return d0 * d0 + d1 * d1 + d2 * d2;
86+
public double distanceSq(int x, int y, int z) {
87+
val dX = this.x - x;
88+
val dY = this.y - y;
89+
val dZ = this.z - z;
90+
return dX * dX + dY * dY + dZ * dZ;
10091
}
10192

10293
/**
103-
* Calculate squared distance to the given Vector
94+
* Square distance between point to center of this Block.
95+
*
96+
* @param x the other x
97+
* @param y the other y
98+
* @param z the other z
99+
* @return the square distance to center
104100
*/
105-
public double distanceSq(Vec3i to) {
106-
return this.distanceSq(to.getX(), to.getY(), to.getZ());
107-
}
108-
109-
@Override
110-
public boolean equals(Object o) {
111-
if (this == o) return true;
112-
if (o == null || getClass() != o.getClass()) return false;
113-
Vec3i vec3i = (Vec3i) o;
114-
return getX() == vec3i.getX() && getY() == vec3i.getY() && getZ() == vec3i.getZ();
115-
}
116-
117-
@Override
118-
public int hashCode() {
119-
return Objects.hash(getX(), getY(), getZ());
101+
public double distanceSqToCenter(double x, double y, double z) {
102+
val dX = this.x + 0.5D - x;
103+
val dY = this.y + 0.5D - y;
104+
val dZ = this.z + 0.5D - z;
105+
return dX * dX + dY * dY + dZ * dZ;
120106
}
121107
}

0 commit comments

Comments
 (0)