-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
Open
Absolutionism
wants to merge
16
commits into
SkriptLang:dev/feature
Choose a base branch
from
Absolutionism:dev/EffOperations
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+452
−3
Open
EffOperations #7764
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3c3f5bc
Initial Commit
Absolutionism 8ff74dd
Merge remote-tracking branch 'upstream/dev/feature' into dev/EffOpera…
Absolutionism 33829f3
Rewrite
Absolutionism ac8dd10
Update Docs+Test
Absolutionism 7372b00
Merge branch 'dev/feature' into dev/EffOperations
Absolutionism 834996f
Update Arithmetics.java
Absolutionism 1728c88
Partial Changes
Absolutionism a18a491
Additional Partial Changes
Absolutionism 858d6ea
Update EffOperations.java
Absolutionism b4b7dea
Minor Update
Absolutionism 2815399
Update EffOperations.java
Absolutionism 723168e
Merge branch 'dev/feature' into dev/EffOperations
Absolutionism aa724b7
Sovdes Version
Absolutionism fddda4b
Update Test
Absolutionism f502e5c
Additional Changes
Absolutionism 76cfa18
Merge branch 'dev/feature' into dev/EffOperations
Absolutionism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
209 changes: 209 additions & 0 deletions
209
src/main/java/ch/njol/skript/effects/EffOperations.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.classes.Changer.ChangeMode; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Example; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Effect; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.SyntaxStringBuilder; | ||
import ch.njol.skript.registrations.Classes; | ||
import ch.njol.skript.util.LiteralUtils; | ||
import ch.njol.skript.util.Patterns; | ||
import ch.njol.skript.util.Utils; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.arithmetic.Arithmetics; | ||
import org.skriptlang.skript.lang.arithmetic.Operation; | ||
import org.skriptlang.skript.lang.arithmetic.OperationInfo; | ||
import org.skriptlang.skript.lang.arithmetic.Operator; | ||
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; | ||
|
||
import java.util.function.Function; | ||
|
||
@Name("Operations") | ||
@Description("Perform multiplication, division, or exponentiation operations on variable objects " | ||
+ "(i.e. numbers, vectors, timespans, and other objects from addons). " | ||
+ "Literals cannot be used on the left-hand side.") | ||
@Example(""" | ||
set {_num} to 1 | ||
multiply {_num} by 10 | ||
divide {_num} by 5 | ||
raise {_num} to the power of 2 | ||
""") | ||
@Example(""" | ||
set {_nums::*} to 15, 21 and 30 | ||
divide {_nums::*} by 3 | ||
multiply {_nums::*} by 5 | ||
raise {_nums::*} to the power of 3 | ||
""") | ||
@Example(""" | ||
set {_vector} to vector(1,1,1) | ||
multiply {_vector} by vector(4,8,16) | ||
divide {_vector} by 2 | ||
""") | ||
@Example(""" | ||
set {_timespan} to 1 hour | ||
multiply {_timespan} by 3 | ||
""") | ||
@Example(""" | ||
# Will error due to literal | ||
multiply 1 by 2 | ||
divide 10 by {_num} | ||
""") | ||
@Since("INSERT VERSION") | ||
public class EffOperations extends Effect implements SyntaxRuntimeErrorProducer { | ||
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. Why not EffArithmetic? |
||
|
||
private static final Patterns<Operator> PATTERNS = new Patterns<>(new Object[][]{ | ||
{"multiply %~objects% by %object%", Operator.MULTIPLICATION}, | ||
{"divide %~objects% by %object%", Operator.DIVISION}, | ||
{"raise %~objects% to [the] (power|exponent) [of] %object%", Operator.EXPONENTIATION} | ||
}); | ||
|
||
static { | ||
Skript.registerEffect(EffOperations.class, PATTERNS.getPatterns()); | ||
} | ||
|
||
private Operator operator; | ||
private Expression<?> base; | ||
private Expression<?> operative; | ||
private Node node; | ||
private Operation<Object, Object, Object> operation = null; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
operator = PATTERNS.getInfo(matchedPattern); | ||
base = exprs[0]; | ||
Class<?>[] baseAccepted = base.acceptChange(ChangeMode.SET); | ||
if (baseAccepted == null) { | ||
Skript.error("This expression cannot be operated on."); | ||
return false; | ||
} | ||
operative = LiteralUtils.defendExpression(exprs[1]); | ||
node = getParser().getNode(); | ||
Class<?> operativeType = operative.getReturnType(); | ||
Class<?> baseType = base.getReturnType(); | ||
// Ensure 'baseType' is referencing a non-array class | ||
if (baseType.isArray()) | ||
baseType = baseType.getComponentType(); | ||
if (operativeType.equals(Object.class) && baseType.equals(Object.class)) { | ||
// Both are object, so we should do the checks within '#execute' | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return LiteralUtils.canInitSafely(operative); | ||
} else if (operativeType.equals(Object.class) || baseType.equals(Object.class)) { | ||
// One is 'Object', so we can atleast see if the other has any operations registered | ||
Class<?>[] returnTypes = null; | ||
if (baseType.equals(Object.class)) { | ||
returnTypes = Arithmetics.getOperations(operator).stream() | ||
.filter(info -> info.getRight().isAssignableFrom(operativeType)) | ||
.map(OperationInfo::getReturnType) | ||
.toArray(Class[]::new); | ||
if (returnTypes.length == 0) { | ||
Skript.error(operator.getName() + " cannot be performed with " | ||
+ Utils.a(getClassInfoCodeName(operativeType))); | ||
return false; | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
returnTypes = Arithmetics.getOperations(operator, baseType).stream() | ||
.map(OperationInfo::getReturnType) | ||
.toArray(Class[]::new); | ||
if (returnTypes.length == 0) { | ||
Skript.error("This expression can not be " + getOperatorString() + "."); | ||
return false; | ||
} | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
// We've deduced that the return types from both 'base' and 'operative' are guaranteed not to be 'Object.class' | ||
// We can check to see if an operation exists, if not, parse error | ||
operation = getOperation(baseType, operativeType); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (operation == null) { | ||
Skript.error("This expression cannot be " + getOperatorString() + " by " | ||
+ Utils.a(getClassInfoCodeName(operativeType)) + "."); | ||
return false; | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
return LiteralUtils.canInitSafely(operative); | ||
} | ||
|
||
@Override | ||
protected void execute(Event event) { | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object operativeObject = operative.getSingle(event); | ||
if (operativeObject == null) | ||
return; | ||
// Ensure 'operativeType' is not 'Object.class' by using '#getReturnType' or the class of the object | ||
Class<?> operativeType = operative.getReturnType().equals(Object.class) ? operativeObject.getClass() : operative.getReturnType(); | ||
Operation<Object, Object, Object> operation = this.operation; | ||
Function<?, Object> changerFunction = null; | ||
Class<?> baseType = base.getReturnType(); | ||
// Convert array classes to singular, allowing for easier checks | ||
if (baseType.isArray()) | ||
baseType = baseType.getComponentType(); | ||
// If 'base' is single or the '#getReturnType' returns a non 'Object.class' we can use the same operation | ||
if (base.isSingle() || !baseType.equals(Object.class)) { | ||
if (operation == null) { | ||
Object baseObject = base.getSingle(event); | ||
if (baseObject == null) | ||
return; | ||
// If we reached here through 'base.isSingle()', need to ensure the class is not 'Object.class' | ||
baseType = baseType.equals(Object.class) ? baseObject.getClass() : baseType; | ||
operation = getOperation(baseType, operativeType); | ||
if (operation == null) { | ||
error(Utils.A(getClassInfoCodeName(baseType)) + " cannot be " + getOperatorString() | ||
+ " with " + Utils.a(getClassInfoCodeName(operativeType)) + "."); | ||
return; | ||
} | ||
} | ||
Operation<Object, Object, Object> finalOperation = operation; | ||
changerFunction = object -> finalOperation.calculate(object, operativeObject); | ||
} else { | ||
changerFunction = object -> { | ||
Operation<Object, Object, Object> thisOperation = getOperation(object.getClass(), operativeType); | ||
if (thisOperation != null) | ||
return thisOperation.calculate(object, operativeObject); | ||
return object; | ||
}; | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
//noinspection unchecked,rawtypes | ||
base.changeInPlace(event, (Function) changerFunction); | ||
} | ||
|
||
private @Nullable Operation<Object, Object, Object> getOperation(Class<?> leftClass, Class<?> rightClass) { | ||
//noinspection unchecked | ||
return (Operation<Object, Object, Object>) Arithmetics.getOperation(operator, leftClass, rightClass, leftClass); | ||
} | ||
|
||
@Override | ||
public Node getNode() { | ||
return node; | ||
} | ||
|
||
private String getClassInfoCodeName(Class<?> clazz) { | ||
return Classes.getSuperClassInfo(clazz).getCodeName(); | ||
} | ||
|
||
private String getOperatorString() { | ||
return switch (operator) { | ||
case MULTIPLICATION -> "multiplied"; | ||
case DIVISION -> "divided"; | ||
case EXPONENTIATION -> "exponentiated"; | ||
default -> ""; | ||
}; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); | ||
switch (operator) { | ||
case MULTIPLICATION -> builder.append("multiply", base, "by"); | ||
case DIVISION -> builder.append("divide", base, "by"); | ||
case EXPONENTIATION -> builder.append("raise", base, "to the power of"); | ||
} | ||
builder.append(operative); | ||
return builder.toString(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/test/skript/tests/syntaxes/effects/EffOperations.sk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
test "operations effect numbers by literal numbers": | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
set {_num} to 1 | ||
multiply {_num} by 15 | ||
assert {_num} is 15 with "Failed multiplication for single number by literal number" | ||
divide {_num} by 5 | ||
assert {_num} is 3 with "Failed division for single number by literal number" | ||
raise {_num} to the power of 2 | ||
assert {_num} is 9 with "Failed exponentiation for single number by literal number" | ||
|
||
set {_nums::*} to 2, 3, 4 and 5 | ||
multiply {_nums::*} by 10 | ||
assert {_nums::*} is (20, 30, 40 and 50) with "Failed multiplication for number list by literal number" | ||
divide {_nums::*} by 5 | ||
assert {_nums::*} is (4, 6, 8 and 10) with "Failed division for number list by literal number" | ||
raise {_nums::*} to the power of 2 | ||
assert {_nums::*} is (16, 36, 64 and 100) with "Failed exponentiation for number list by literal number" | ||
|
||
test "operations effect numbers by variable numbers": | ||
set {_num} to 1 | ||
set {_multiply} to 10 | ||
set {_divide} to 5 | ||
set {_raise} to 2 | ||
multiply {_num} by {_multiply} | ||
assert {_num} is 10 with "Failed multiplication for single number by variable number" | ||
divide {_num} by {_divide} | ||
assert {_num} is 2 with "Failed division for single number by variable number" | ||
raise {_num} to the power of {_raise} | ||
assert {_num} is 4 with "Failed exponentiation for single number by variable number" | ||
|
||
set {_nums::*} to 2, 3, 4 and 5 | ||
multiply {_nums::*} by {_multiply} | ||
assert {_nums::*} is (20, 30, 40 and 50) with "Failed multiplication for number list by variable number" | ||
divide {_nums::*} by {_divide} | ||
assert {_nums::*} is (4, 6, 8 and 10) with "Failed division for number list by variable number" | ||
raise {_nums::*} to the power of {_raise} | ||
assert {_nums::*} is (16, 36, 64 and 100) with "Failed exponentiation for number list by variable number" | ||
|
||
test "operations effect vectors by function vectors": | ||
set {_vector} to vector(1,1,1) | ||
multiply {_vector} by vector(4,6,8) | ||
assert {_vector} is vector(4,6,8) with "Failed multiplication for single vector by function vector" | ||
divide {_vector} by vector(2,3,4) | ||
assert {_vector} is vector(2,2,2) with "Failed division for single vector by function vector" | ||
|
||
set {_vectors::*} to vector(2,2,2), vector(3,3,3) and vector(4,4,4) | ||
multiply {_vectors::*} by vector(2,5,10) | ||
assert {_vectors::*} is (vector(4,10,20), vector(6,15,30) and vector(8,20,40)) with "Failed multiplication for vector list by function vector" | ||
divide {_vectors::*} by vector(1,5,5) | ||
assert {_vectors::*} is (vector(4,2,4), vector(6,3,6) and vector(8,4,8)) with "Failed division for vector list by function vector" | ||
|
||
test "operations effect vectors by variable vectors": | ||
set {_vector} to vector(1,1,1) | ||
set {_multiply} to vector(10,20,30) | ||
set {_divide} to vector(5,5,5) | ||
multiply {_vector} by {_multiply} | ||
assert {_vector} is vector(10,20,30) with "Failed multiplication for single vector by vector" | ||
divide {_vector} by {_divide} | ||
assert {_vector} is vector(2,4,6) with "Failed division for single vector by vector" | ||
|
||
set {_vectors::*} to vector(2,2,2), vector(3,3,3) and vector(4,4,4) | ||
multiply {_vectors::*} by {_multiply} | ||
assert {_vectors::*} is (vector(20,40,60), vector(30,60,90) and vector(40,80,120)) with "Failed multiplication for vector list by variable vector" | ||
divide {_vectors::*} by {_divide} | ||
assert {_vectors::*} is (vector(4,8,12), vector(6,12,18) and vector(8,16,24)) with "Failed division for vector list by variable vector" | ||
|
||
test "operations effect vectors by literal numbers": | ||
set {_vector} to vector(1,1,1) | ||
multiply {_vector} by 10 | ||
assert {_vector} is vector(10,10,10) with "Failed multiplication for single vector by literal number" | ||
divide {_vector} by 2 | ||
assert {_vector} is vector(5,5,5) with "Failed division for single vector by literal number" | ||
|
||
set {_vectors::*} to vector(2,2,2), vector(3,3,3) and vector(4,4,4) | ||
multiply {_vectors::*} by 4 | ||
assert {_vectors::*} is (vector(8,8,8), vector(12,12,12) and vector(16,16,16)) with "Failed multiplication for vector list by literal number" | ||
divide {_vectors::*} by 2 | ||
assert {_vectors::*} is (vector(4,4,4), vector(6,6,6) and vector(8,8,8)) with "Failed division for vector list by literal number" | ||
|
||
test "operations effect vectors by variable numbers": | ||
set {_vector} to vector(1,1,1) | ||
set {_multiply} to 15 | ||
set {_divide} to 3 | ||
multiply {_vector} by {_multiply} | ||
assert {_vector} is vector(15,15,15) with "Failed multiplication for single vector by variable number" | ||
divide {_vector} by {_divide} | ||
assert {_vector} is vector(5,5,5) with "Failed division for single vector by variable number" | ||
|
||
set {_vectors::*} to vector(2,2,2), vector(3,3,3) and vector(4,4,4) | ||
multiply {_vectors::*} by {_multiply} | ||
assert {_vectors::*} is (vector(30,30,30), vector(45,45,45) and vector(60,60,60)) with "Failed multiplication for vector list by variable number" | ||
divide {_vectors::*} by {_divide} | ||
assert {_vectors::*} is (vector(10,10,10), vector(15,15,15) and vector(20,20,20)) with "Failed division for vector list by variable number" | ||
|
||
test "operations effect timespans by literal numbers": | ||
set {_timespan} to 1 hour | ||
multiply {_timespan} by 4 | ||
assert {_timespan} is 4 hours with "Failed multiplication for single timespan by literal number" | ||
divide {_timespan} by 2 | ||
assert {_timespan} is 2 hours with "Failed division for single timespan by literal number" | ||
|
||
set {_timespans::*} to (1 minute), (5 minutes) and (10 minutes) | ||
multiply {_timespans::*} by 5 | ||
assert {_timespans::*} is ((5 minutes), (25 minutes) and (50 minutes)) with "Failed multiplication for timespan list by literal number" | ||
divide {_timespans::*} by 2 | ||
assert {_timespans::*} is ((2 minutes and 30 seconds), (12 minutes and 30 seconds) and (25 minutes)) with "Failed division for timespan list by literal number" | ||
|
||
test "operations effect timespans by variable numbers": | ||
set {_timespan} to 1 hour | ||
set {_multiply} to 6 | ||
set {_divide} to 2 | ||
multiply {_timespan} by {_multiply} | ||
assert {_timespan} is 6 hours with "Failed multiplication for single timespan by variable number" | ||
divide {_timespan} by {_divide} | ||
assert {_timespan} is 3 hours with "Failed division for single timespan by variable number" | ||
|
||
set {_timespans::*} to (1 minute), (5 minutes) and (10 minutes) | ||
multiply {_timespans::*} by {_multiply} | ||
assert {_timespans::*} is ((6 minutes), (30 minutes) and (60 minutes)) with "Failed multiplication for timespan list by variable number" | ||
divide {_timespans::*} by {_divide} | ||
assert {_timespans::*} is ((3 minutes), (15 minutes) and (30 minutes)) with "Failed division for timespan list by variable number" | ||
|
||
test "operations effect literals error": | ||
parse: | ||
multiply 1 by 2 | ||
assert last parse logs is set with "Operations should not work for left side literals - multiplication" | ||
|
||
parse: | ||
divide 20 by 4 | ||
assert last parse logs is set with "Operations should not work for left side literals - division" | ||
|
||
parse: | ||
raise 3 to the power of 100 | ||
assert last parse logs is set with "Operations should not work for left side literals - exponentiation" | ||
|
||
test "operations effect expression errors": | ||
parse: | ||
multiply (the hunger level of (random element out of all players)) by 1 hour | ||
assert last parse logs contains "This expression cannot be multiplied by a timespan." with "Hunger level should not be able to be multiplied by timespan" | ||
|
||
parse: | ||
divide all worlds by 2 | ||
assert last parse logs contains "This expression cannot be operated on." with "ExprWorlds should not have a 'SET' ChangeMode" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.