-
-
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.
Open
EffOperations #7764
Changes from 6 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
171 changes: 171 additions & 0 deletions
171
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,171 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.classes.ClassInfo; | ||
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.lang.Variable; | ||
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.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). Cannot use literals.") | ||
@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 | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multiply 1 by 2 | ||
""") | ||
@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[][]{ | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{"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; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
operator = patterns.getInfo(matchedPattern); | ||
base = exprs[0]; | ||
if (!(base instanceof Variable<?>)) { | ||
Skript.error("Cannot operate on a non variable object."); | ||
return false; | ||
} | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
operative = LiteralUtils.defendExpression(exprs[1]); | ||
node = getParser().getNode(); | ||
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; | ||
ClassInfo<?> operativeClassInfo = Classes.getSuperClassInfo(operativeObject.getClass()); | ||
Class<?> operativeClass = operativeClassInfo.getC(); | ||
Operation<Object, Object, Object> operation = null; | ||
Function<?, Object> function = null; | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (base.isSingle()) { | ||
// If the variable provided is single, then we can do some checks | ||
// to ensure an operation is available; if not, we can produce a proper error. | ||
Object baseObject = base.getSingle(event); | ||
if (baseObject == null) | ||
return; | ||
ClassInfo<?> baseClassInfo = Classes.getSuperClassInfo(baseObject.getClass()); | ||
Class<?> baseClass = baseClassInfo.getC(); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!baseClass.equals(operativeClass)) { | ||
operation = getOperation(baseClass, operativeClass); | ||
if (operation == null) { | ||
error(Utils.A(baseClassInfo.getCodeName()) + "cannot be " + getOperatorString() | ||
+ " with " + Utils.a(operativeClassInfo.getCodeName())); | ||
return; | ||
} | ||
} else { | ||
operation = getOperation(operativeClass); | ||
if (operation == null) { | ||
error(Utils.A(operativeClassInfo.getCodeName()) + " cannot be " + getOperatorString() + "."); | ||
return; | ||
} | ||
} | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Operation<Object, Object, Object> finalOperation = operation; | ||
function = object -> finalOperation.calculate(object, operativeObject); | ||
} else { | ||
// In the case of a list variable, we should probably ignore throwing multiple errors for each object | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// that is not applicable for an operation | ||
function = object -> { | ||
Operation<Object, Object, Object> thisOperation = getOperation(object.getClass(), operativeClass); | ||
if (thisOperation != null) | ||
return thisOperation.calculate(object, operativeObject); | ||
return object; | ||
}; | ||
} | ||
//noinspection unchecked,rawtypes | ||
base.changeInPlace(event, (Function) function); | ||
} | ||
|
||
private @Nullable Operation<Object, Object, Object> getOperation(Class<?> type) { | ||
//noinspection unchecked | ||
return (Operation<Object, Object, Object>) Arithmetics.getConvertedOperation(operator, type, type, type); | ||
} | ||
|
||
private @Nullable Operation<Object, Object, Object> getOperation(Class<?> leftClass, Class<?> rightClass) { | ||
//noinspection unchecked | ||
return (Operation<Object, Object, Object>) Arithmetics.getConvertedOperation(operator, leftClass, rightClass, leftClass); | ||
} | ||
|
||
@Override | ||
public Node getNode() { | ||
return node; | ||
} | ||
|
||
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
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,68 @@ | ||
test "operations numbers": | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
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" | ||
divide {_num} by 5 | ||
assert {_num} is 3 with "Failed division for single number" | ||
raise {_num} to the power of 2 | ||
assert {_num} is 9 with "Failed exponentiation for single 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" | ||
divide {_nums::*} by 5 | ||
assert {_nums::*} is (4, 6, 8 and 10) with "Failed division for number list" | ||
raise {_nums::*} to the power of 2 | ||
assert {_nums::*} is (16, 36, 64 and 100) with "Failed exponentiation for number list" | ||
|
||
test "operations vectors by 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 vector" | ||
divide {_vector} by vector(2,3,4) | ||
assert {_vector} is vector(2,2,2) 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 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 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 vector" | ||
|
||
test "operations vectors by 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 number" | ||
divide {_vector} by 2 | ||
assert {_vector} is vector(5,5,5) with "Failed division for single vector by 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 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 number" | ||
|
||
test "operations timespans by numbers": | ||
set {_timespan} to 1 hour | ||
multiply {_timespan} by 4 | ||
assert {_timespan} is 4 hours with "Failed multiplication for single timespan by number" | ||
divide {_timespan} by 2 | ||
assert {_timespan} is 2 hours with "Failed division for single timespan by 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 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 number" | ||
|
||
test "operations error": | ||
parse: | ||
multiply 1 by 2 | ||
assert last parse logs is set with "Operations should only work for variables - multiplication" | ||
|
||
parse: | ||
divide 20 by 4 | ||
assert last parse logs is set with "Operations should only work for variables - division" | ||
|
||
parse: | ||
raise 3 to the powwer of 100 | ||
assert last parse logs is set with "Operations should only work for variables - exponentiation" |
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.