Skip to content

Commit

Permalink
Merge pull request #5952 from sovdeeth/RotateVectorToStringFix
Browse files Browse the repository at this point in the history
Clean up vector classes and fix a few bugs.
  • Loading branch information
sovdeeth authored Sep 29, 2023
2 parents d1f73b8 + 729b0ec commit 613a067
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
import ch.njol.util.Kleenean;
import ch.njol.util.VectorMath;

/**
* @author bi0qaw
*/
@Name("Vectors - Rotate Around Vector")
@Description("Rotates a vector around another vector")
@Examples({"rotate {_v} around vector 1, 0, 0 by 90"})
@Description("Rotates one or more vectors around another vector")
@Examples("rotate {_v} around vector 1, 0, 0 by 90")
@Since("2.2-dev28")
public class EffVectorRotateAroundAnother extends Effect {

Expand All @@ -47,34 +44,34 @@ public class EffVectorRotateAroundAnother extends Effect {
}

@SuppressWarnings("null")
private Expression<Vector> first, second;
private Expression<Vector> vectors, axis;

@SuppressWarnings("null")
private Expression<Number> degree;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean kleenean, ParseResult parseResult) {
first = (Expression<Vector>) exprs[0];
second = (Expression<Vector>) exprs[1];
vectors = (Expression<Vector>) exprs[0];
axis = (Expression<Vector>) exprs[1];
degree = (Expression<Number>) exprs[2];
return true;
}

@SuppressWarnings("null")
@Override
protected void execute(Event e) {
Vector v2 = second.getSingle(e);
Number d = degree.getSingle(e);
if (v2 == null || d == null)
protected void execute(Event event) {
Vector axis = this.axis.getSingle(event);
Number angle = degree.getSingle(event);
if (axis == null || angle == null)
return;
for (Vector v1 : first.getArray(e))
VectorMath.rot(v1, v2, d.doubleValue());
for (Vector vector : vectors.getArray(event))
VectorMath.rot(vector, axis, angle.doubleValue());
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "rotate " + first.toString(e, debug) + " around " + second.toString(e, debug) + " by " + degree + "degrees";
public String toString(@Nullable Event event, boolean debug) {
return "rotate " + vectors.toString(event, debug) + " around " + axis.toString(event, debug) + " by " + degree.toString(event, debug) + "degrees";
}

}
41 changes: 20 additions & 21 deletions src/main/java/ch/njol/skript/effects/EffVectorRotateXYZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@
import ch.njol.util.Kleenean;
import ch.njol.util.VectorMath;

/**
* @author bi0qaw
*/
@Name("Vectors - Rotate around XYZ")
@Description("Rotates a vector around x, y, or z axis by some degrees")
@Examples({"rotate {_v} around x-axis by 90",
"rotate {_v} around y-axis by 90",
"rotate {_v} around z-axis by 90 degrees"})
@Description("Rotates one or more vectors around the x, y, or z axis by some amount of degrees")
@Examples({
"rotate {_v} around x-axis by 90",
"rotate {_v} around y-axis by 90",
"rotate {_v} around z-axis by 90 degrees"
})
@Since("2.2-dev28")
public class EffVectorRotateXYZ extends Effect {

static {
Skript.registerEffect(EffVectorRotateXYZ.class, "rotate %vectors% around (1¦x|2¦y|3¦z)(-| )axis by %number% [degrees]");
Skript.registerEffect(EffVectorRotateXYZ.class, "rotate %vectors% around (0¦x|1¦y|2¦z)(-| )axis by %number% [degrees]");
}

private final static Character[] axes = new Character[] {'x', 'y', 'z'};
Expand All @@ -68,28 +67,28 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is

@Override
@SuppressWarnings("null")
protected void execute(Event e) {
Number d = degree.getSingle(e);
if (d == null)
protected void execute(Event event) {
Number angle = degree.getSingle(event);
if (angle == null)
return;
switch (axis) {
case 0:
for (Vector vector : vectors.getArray(event))
VectorMath.rotX(vector, angle.doubleValue());
break;
case 1:
for (Vector v : vectors.getArray(e))
VectorMath.rotX(v, d.doubleValue());
for (Vector vector : vectors.getArray(event))
VectorMath.rotY(vector, angle.doubleValue());
break;
case 2:
for (Vector v : vectors.getArray(e))
VectorMath.rotY(v, d.doubleValue());
break;
case 3:
for (Vector v : vectors.getArray(e))
VectorMath.rotZ(v, d.doubleValue());
for (Vector vector : vectors.getArray(event))
VectorMath.rotZ(vector, angle.doubleValue());
}
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "rotate " + vectors.toString(e, debug) + " around " + axes[axis] + "-axis" + " by " + degree + "degrees";
public String toString(@Nullable Event event, boolean debug) {
return "rotate " + vectors.toString(event, debug) + " around " + axes[axis] + "-axis" + " by " + degree.toString(event, debug) + "degrees";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@
import ch.njol.util.VectorMath;
import ch.njol.util.coll.CollectionUtils;

/**
* @author bi0qaw
*/
@Name("Vectors - Angle Between")
@Description("Gets the angle between two vectors.")
@Examples({"send \"%the angle between vector 1, 0, 0 and vector 0, 1, 1%\""})
@Examples("send \"%the angle between vector 1, 0, 0 and vector 0, 1, 1%\"")
@Since("2.2-dev28")
public class ExprVectorAngleBetween extends SimpleExpression<Number> {

Expand All @@ -62,12 +59,12 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
@SuppressWarnings("null")
protected Number[] get(Event e) {
Vector v1 = first.getSingle(e);
Vector v2 = second.getSingle(e);
if (v1 == null || v2 == null)
protected Number[] get(Event event) {
Vector first = this.first.getSingle(event);
Vector second = this.second.getSingle(event);
if (first == null || second == null)
return null;
return CollectionUtils.array(v1.angle(v2) * (float) VectorMath.RAD_TO_DEG);
return CollectionUtils.array(first.angle(second) * (float) VectorMath.RAD_TO_DEG);
}

@Override
Expand All @@ -81,8 +78,8 @@ public Class<? extends Number> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "the angle between " + first.toString(e, debug) + " and " + second.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return "the angle between " + first.toString(event, debug) + " and " + second.toString(event, debug);
}

}
58 changes: 25 additions & 33 deletions src/main/java/ch/njol/skript/expressions/ExprVectorArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,41 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

/**
* @author bi0qaw
*/
@Name("Vectors - Arithmetic")
@Description("Arithmetic expressions for vectors.")
@Examples({"set {_v} to vector 1, 2, 3 // 5",
"set {_v} to {_v} ++ {_v}",
"set {_v} to {_v} ++ 5",
"set {_v} to {_v} -- {_v}",
"set {_v} to {_v} -- 5",
"set {_v} to {_v} ** {_v}",
"set {_v} to {_v} ** 5",
"set {_v} to {_v} // {_v}",
"set {_v} to {_v} // 5"})
@Since("2.2-dev28")
@Examples({
"set {_v} to vector 1, 2, 3 // vector 5, 5, 5",
"set {_v} to {_v} ++ {_v}",
"set {_v} to {_v} -- {_v}",
"set {_v} to {_v} ** {_v}",
"set {_v} to {_v} // {_v}"
})
@Since("2.2-desecond8")
public class ExprVectorArithmetic extends SimpleExpression<Vector> {

private enum Operator {
PLUS("++") {
@Override
public Vector calculate(final Vector v1, final Vector v2) {
return v1.clone().add(v2);
public Vector calculate(final Vector first, final Vector second) {
return first.clone().add(second);
}
},
MINUS("--") {
@Override
public Vector calculate(final Vector v1, final Vector v2) {
return v1.clone().subtract(v2);
public Vector calculate(final Vector first, final Vector second) {
return first.clone().subtract(second);
}
},
MULT("**") {
@Override
public Vector calculate(final Vector v1, final Vector v2) {
return v1.clone().multiply(v2);
public Vector calculate(final Vector first, final Vector second) {
return first.clone().multiply(second);
}
},
DIV("//") {
@Override
public Vector calculate(final Vector v1, final Vector v2) {
return v1.clone().divide(v2);
public Vector calculate(final Vector first, final Vector second) {
return first.clone().divide(second);
}
};

Expand All @@ -84,7 +79,7 @@ public Vector calculate(final Vector v1, final Vector v2) {
this.sign = sign;
}

public abstract Vector calculate(Vector v1, Vector v2);
public abstract Vector calculate(Vector first, Vector second);

@Override
public String toString() {
Expand All @@ -107,25 +102,22 @@ public String toString() {
private Expression<Vector> first, second;

@SuppressWarnings("null")
private Operator op;
private Operator operator;

@Override
@SuppressWarnings({"unchecked", "null"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
first = (Expression<Vector>) exprs[0];
second = (Expression<Vector>) exprs[1];
op = patterns.getInfo(matchedPattern);
operator = patterns.getInfo(matchedPattern);
return true;
}

@Override
protected Vector[] get(Event e) {
Vector v1 = first.getSingle(e), v2 = second.getSingle(e);
if (v1 == null)
v1 = new Vector();
if (v2 == null)
v2 = new Vector();
return CollectionUtils.array(op.calculate(v1, v2));
protected Vector[] get(Event event) {
Vector first = this.first.getOptionalSingle(event).orElse(new Vector());
Vector second = this.second.getOptionalSingle(event).orElse(new Vector());
return CollectionUtils.array(operator.calculate(first, second));
}

@Override
Expand All @@ -139,8 +131,8 @@ public Class<? extends Vector> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return first.toString(e, debug) + " " + op + " " + second.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return first.toString(event, debug) + " " + operator + " " + second.toString(event, debug);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

/**
* @author bi0qaw
*/
@Name("Vectors - Vector Between Locations")
@Description("Creates a vector between two locations.")
@Examples({"set {_v} to vector between {_loc1} and {_loc2}"})
@Examples("set {_v} to vector between {_loc1} and {_loc2}")
@Since("2.2-dev28")
public class ExprVectorBetweenLocations extends SimpleExpression<Vector> {

Expand All @@ -62,12 +59,12 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
@SuppressWarnings("null")
protected Vector[] get(Event e) {
Location l1 = from.getSingle(e);
Location l2 = to.getSingle(e);
if (l1 == null || l2 == null)
protected Vector[] get(Event event) {
Location from = this.from.getSingle(event);
Location to = this.to.getSingle(event);
if (from == null || to == null)
return null;
return CollectionUtils.array(new Vector(l2.getX() - l1.getX(), l2.getY() - l1.getY(), l2.getZ() - l1.getZ()));
return CollectionUtils.array(new Vector(to.getX() - from.getX(), to.getY() - from.getY(), to.getZ() - from.getZ()));
}

@Override
Expand All @@ -80,8 +77,8 @@ public Class<? extends Vector> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "vector from " + from.toString(e, debug) + " to " + to.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return "vector from " + from.toString(event, debug) + " to " + to.toString(event, debug);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

/**
* @author bi0qaw
*/
@Name("Vectors - Cross Product")
@Description("Gets the cross product between two vectors.")
@Examples({"send \"%vector 1, 0, 0 cross vector 0, 1, 0%\""})
@Examples("send \"%vector 1, 0, 0 cross vector 0, 1, 0%\"")
@Since("2.2-dev28")
public class ExprVectorCrossProduct extends SimpleExpression<Vector> {

Expand All @@ -60,12 +57,12 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
@SuppressWarnings("null")
protected Vector[] get(Event e) {
Vector v1 = first.getSingle(e);
Vector v2 = second.getSingle(e);
if (v1 == null || v2 == null)
protected Vector[] get(Event event) {
Vector first = this.first.getSingle(event);
Vector second = this.second.getSingle(event);
if (first == null || second == null)
return null;
return CollectionUtils.array(v1.clone().crossProduct(v2));
return CollectionUtils.array(first.clone().crossProduct(second));
}

@Override
Expand All @@ -79,8 +76,8 @@ public Class<? extends Vector> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return first.toString(e, debug) + " cross " + second.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return first.toString(event, debug) + " cross " + second.toString(event, debug);
}

}
Loading

0 comments on commit 613a067

Please sign in to comment.