-
-
Notifications
You must be signed in to change notification settings - Fork 403
EffOperations #7764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev/feature
Are you sure you want to change the base?
EffOperations #7764
Changes from 2 commits
3c3f5bc
8ff74dd
33829f3
ac8dd10
7372b00
834996f
1728c88
a18a491
858d6ea
b4b7dea
2815399
723168e
aa724b7
fddda4b
f502e5c
76cfa18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye | |
leftAccepts = left.acceptChange(ChangeMode.SET); | ||
// Ensure 'left' is changeable | ||
if (leftAccepts == null) { | ||
Skript.error("'" + left + "' cannot be set to anything and therefore cannot be " + getOperatorName() + "."); | ||
Skript.error("'" + left + "' cannot be set to anything and therefore cannot be " + getOperatorVerb() + "."); | ||
return false; | ||
} else if (leftAccepts.length == 0) { | ||
throw new IllegalStateException("An expression should never return an empty array for a ChangeMode of 'SET'"); | ||
|
@@ -107,41 +107,47 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye | |
Class<?> leftType = left.getReturnType(); | ||
Class<?> rightType = right.getReturnType(); | ||
|
||
if (leftType.isArray()) | ||
leftType = leftType.getComponentType(); | ||
|
||
if (leftType.equals(Object.class) && rightType.equals(Object.class)) { | ||
// 'left' and 'right' return 'Object.class' thus making operation checks non-applicable | ||
// However, we can check to make sure any of the registered operations return types are applicable | ||
// for 'left's acceptedClasses | ||
Class<?>[] allReturnTypes = Arithmetics.getAllReturnTypes(operator).toArray(Class[]::new); | ||
if (!ChangerUtils.acceptsChangeTypes(leftAccepts, allReturnTypes)) { | ||
Skript.error(left + " cannot be " + getOperatorName() + "."); | ||
Skript.error(left.toString(null, Skript.debug()) + " cannot be " + getOperatorVerb() + "."); | ||
return false; | ||
} | ||
return LiteralUtils.canInitSafely(right); | ||
} else if (leftType.equals(Object.class) || rightType.equals(Object.class)) { | ||
// Only one returns 'Object.class' | ||
Class<?>[] returnTypes; | ||
if (leftType.equals(Object.class)) { | ||
// 'left' returns 'Object.class', so we get all operations where 'right' is assignable to the right side | ||
// of the operations and store the return types | ||
returnTypes = Arithmetics.getOperations(operator).stream() | ||
.filter(info -> info.getRight().isAssignableFrom(rightType)) | ||
.map(OperationInfo::getReturnType) | ||
.toArray(Class[]::new); | ||
} else { | ||
// 'right' returns 'Object.class', so we get all operations where 'left' is assignable to the left side | ||
// of the operations and store the return types | ||
returnTypes = Arithmetics.getOperations(operator, leftType).stream() | ||
.map(OperationInfo::getReturnType) | ||
.toArray(Class[]::new); | ||
} | ||
|
||
// No operations found, meaning nothing can be done | ||
if (returnTypes.length == 0) { | ||
noOperationError(left, leftType, rightType); | ||
return false; | ||
} | ||
// Check if 'left' can be changed into at least one of the possible return types | ||
if (!ChangerUtils.acceptsChangeTypes(leftAccepts, returnTypes)) { | ||
genericParseError(left, rightType); | ||
return false; | ||
} | ||
} else { | ||
// Both 'left' and 'right' return an exact class type, so we check if the operation exists | ||
// Then if 'left' accepts the return type of the operation | ||
operationInfo = Arithmetics.lookupOperationInfo(operator, leftType, rightType, leftAccepts); | ||
if (operationInfo == null || !ChangerUtils.acceptsChangeTypes(leftAccepts, operationInfo.getReturnType())) { | ||
genericParseError(left, rightType); | ||
|
@@ -154,8 +160,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye | |
@Override | ||
protected void execute(Event event) { | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object rightObject = right.getSingle(event); | ||
if (rightObject == null) | ||
if (rightObject == null) { | ||
error("Cannot operate with a null object."); | ||
return; | ||
} | ||
if (left.isSingle() && left.getSingle(event) == null) { | ||
error("Cannot operate on a null object."); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be doing errors here imo. Precedent is for effects acting on nothing to simply do nothing. |
||
|
||
Class<?> rightType = rightObject.getClass(); | ||
|
||
|
@@ -198,7 +210,7 @@ private void printArithmeticError(Class<?> left, Class<?> right) { | |
} | ||
|
||
private void genericParseError(Expression<?> leftExpr, Class<?> rightType) { | ||
Skript.error("'" + leftExpr + "' cannot be " + getOperatorName() + " by " | ||
Skript.error("'" + leftExpr + "' cannot be " + getOperatorVerb() + " by " | ||
+ Classes.getSuperClassInfo(rightType).getName().withIndefiniteArticle() + "."); | ||
} | ||
|
||
|
@@ -211,7 +223,7 @@ private void noOperationError(Expression<?> leftExpr, Class<?> leftType, Class<? | |
} | ||
} | ||
|
||
private String getOperatorName() { | ||
private String getOperatorVerb() { | ||
return switch (operator) { | ||
case MULTIPLICATION -> "multiplied"; | ||
case DIVISION -> "divided"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,17 @@ test "operations effect expression errors": | |
parse: | ||
divide all worlds by 2 | ||
assert last parse logs contains "'worlds' cannot be set to anything and therefore cannot be divided." with "ExprWorlds should not have a 'SET' ChangeMode" | ||
|
||
test "operations effect invalid operation": # TODO: test for runtime errors | ||
set {_date} to now | ||
multiply {_date} by 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be asserting the value of {_date} afterwards |
||
|
||
set {_span} to 1 second | ||
divide {_date} by {_span} | ||
|
||
test "operations effect on expression": | ||
set {_player} to "Notch" parsed as offline player | ||
set time played of {_player} to 1 second | ||
multiply the time played of {_player} by 60 | ||
assert the time played of {_player} is 1 minute with "ExprTimePlayed was not changed with EffOperations" | ||
set the time played of {_player} to 0 seconds |
Uh oh!
There was an error while loading. Please reload this page.