Skip to content
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

Add Timespan/Timespan=Number arithmetic and TimePeriod.MILLISECOND #6749

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/java/ch/njol/skript/classes/data/DefaultOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.skript.util.Utils;
import ch.njol.util.Math2;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -85,9 +86,9 @@ public class DefaultOperations {
});

// Timespan - Timespan
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(Math2.addClamped(left.getMilliSeconds(), right.getMilliSeconds())));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getMilliSeconds() - right.getMilliSeconds())));
Arithmetics.registerOperation(Operator.ADDITION, Timespan.class, (left, right) -> new Timespan(Math2.addClamped(left.getAs(TimePeriod.MILLISECOND), right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerOperation(Operator.SUBTRACTION, Timespan.class, (left, right) -> new Timespan(Math.max(0, left.getAs(TimePeriod.MILLISECOND) - right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerDifference(Timespan.class, (left, right) -> new Timespan(Math.abs(left.getAs(TimePeriod.MILLISECOND) - right.getAs(TimePeriod.MILLISECOND))));
Arithmetics.registerDefaultValue(Timespan.class, Timespan::new);

// Timespan - Number
Expand All @@ -96,20 +97,24 @@ public class DefaultOperations {
long scalar = right.longValue();
if (scalar < 0)
return null;
return new Timespan(Math2.multiplyClamped(left.getMilliSeconds(), scalar));
return new Timespan(Math2.multiplyClamped(left.getAs(TimePeriod.MILLISECOND), scalar));
}, (left, right) -> {
long scalar = left.longValue();
if (scalar < 0)
return null;
return new Timespan(scalar * right.getMilliSeconds());
return new Timespan(scalar * right.getAs(TimePeriod.MILLISECOND));
});
Arithmetics.registerOperation(Operator.DIVISION, Timespan.class, Number.class, (left, right) -> {
long scalar = right.longValue();
if (scalar <= 0)
return null;
return new Timespan(left.getMilliSeconds() / scalar);
return new Timespan(left.getAs(TimePeriod.MILLISECOND) / scalar);
});

// Timespan / Timespan = Number
Arithmetics.registerOperation(Operator.DIVISION, Timespan.class, Timespan.class, Number.class,
(left, right) -> left.getAs(TimePeriod.MILLISECOND) / (double) right.getAs(TimePeriod.MILLISECOND));
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

// Date - Timespan
Arithmetics.registerOperation(Operator.ADDITION, Date.class, Timespan.class, Date::plus);
Arithmetics.registerOperation(Operator.SUBTRACTION, Date.class, Timespan.class, Date::minus);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/util/Timespan.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Timespan implements YggdrasilSerializable, Comparable<Timespan> { /

public enum TimePeriod {

MILLISECOND(1L),
TICK(50L),
SECOND(1000L),
MINUTE(SECOND.time * 60L),
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ tree types:

# -- Time --
time:
millisecond: millisecond¦s
tick: tick¦s
second: second¦s
minute: minute¦s
Expand Down
8 changes: 7 additions & 1 deletion src/test/skript/tests/syntaxes/expressions/ExprArithmetic.sk
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ test "timespan arithmetic":
assert (2 * {_t1}) is (2 seconds) with "2 * 1 second is not 2 seconds"
assert (2 / {_t1}) is not set with "number divided by timespan is set"

assert ({_t1} + 2) is not set with "timespan plus number is set"
assert ({_t1} + 2) is not set with "timespan plus number is set"

assert {_t1} / {_t2} is 0.5 with "timespan / timespan failed"
assert {_t1} / 1 tick is 20 with "timespan / timespan of different units failed"
assert 0 seconds / {_t2} is 0 with "0 timespan / timespan failed"
assert {_t1} / 0 seconds is infinity value with "timespan / 0 timespan failed"
assert isNaN(0 seconds / 0 ticks) is true with "0 timespan / 0 timespan failed", expected NaN value, got (0 seconds / 0 ticks)

test "date arithmetic":
set {_d1} to now
Expand Down