Skip to content

Commit

Permalink
Update ExprXYZComponent for changeInPlace
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Dec 3, 2024
1 parent 8bfb096 commit 7c4569b
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/main/java/ch/njol/skript/expressions/ExprXYZComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.classes.Changer.ChangerUtils;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand Down Expand Up @@ -89,9 +90,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
if ((mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET)) {
boolean acceptsChange;
if (IS_RUNNING_1194) {
acceptsChange = Changer.ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class, Quaternionf.class);
acceptsChange = ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class, Quaternionf.class);
} else {
acceptsChange = Changer.ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class);
acceptsChange = ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class);
}
if (acceptsChange)
return CollectionUtils.array(Number.class);
Expand All @@ -102,35 +103,20 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
assert delta != null; // reset/delete not supported
Object[] objects = getExpr().getArray(event);
double value = ((Number) delta[0]).doubleValue();
final double value = ((Number) delta[0]).doubleValue();

boolean hasVectors = false;
boolean hasQuaternions = false;
boolean hasInvalidInput = false;
// for covering the edge cases such as an expression that returns a Vector but can only be set to a Quaternions
boolean acceptsVectors = ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class);
boolean acceptsQuaternions = ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Quaternionf.class);

for (Object object : objects) {
if (object instanceof Vector vector) {
getExpr().changeInPlace(event, object -> {
if (acceptsVectors && object instanceof Vector vector) {
changeVector(vector, axis, value, mode);
hasVectors = true;
} else if (object instanceof Quaternionf quaternion) {
} else if (acceptsQuaternions && object instanceof Quaternionf quaternion) {
changeQuaternion(quaternion, axis, (float) value, mode);
hasQuaternions = true;
} else {
hasInvalidInput = true;
}
}

// don't SET the expression if there were invalid inputs
if (hasInvalidInput)
return;

// covers the edge case where an expression can be set to Vector but returns Quaternions, or similar.
if (hasVectors && !Changer.ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class))
return;
if (hasQuaternions && !Changer.ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Quaternionf.class))
return;
getExpr().change(event, objects, ChangeMode.SET);
return object;
});
}

/**
Expand Down

0 comments on commit 7c4569b

Please sign in to comment.