-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2907 from APickledWalrus/APickledWalrus-time-exprs
Time Since Expression
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/ch/njol/skript/expressions/ExprTimeSince.java
This file contains 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,77 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* | ||
* Copyright 2011-2017 Peter Güttinger and contributors | ||
*/ | ||
package ch.njol.skript.expressions; | ||
|
||
import org.bukkit.event.Event; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.util.Date; | ||
import ch.njol.skript.util.Timespan; | ||
|
||
@Name("Time Since") | ||
@Description("The time that has passed since a date. If the given date is in the future, a value will not be returned.") | ||
@Examples("send \"%time since 5 minecraft days ago% has passed since 5 minecraft days ago!\" to player") | ||
@Since("INSERT VERSION") | ||
public class ExprTimeSince extends SimplePropertyExpression<Date, Timespan> { | ||
|
||
static { | ||
Skript.registerExpression(ExprTimeSince.class, Timespan.class, ExpressionType.PROPERTY, "[the] time since %dates%"); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Timespan convert(Date date) { | ||
Date now = Date.now(); | ||
|
||
/* | ||
* This condition returns whether the date the player is using is | ||
* before the current date, the same as the current date, or after the current date. | ||
* A value less than 0 indicates that the new date is BEFORE the current date. | ||
* A value of 0 indicates that the new date is the SAME as the current date. | ||
* A value greater than 0 indicates that the new date is AFTER the current date. | ||
*/ | ||
if (date.compareTo(now) < 1) | ||
return date.difference(now); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Class<? extends Timespan> getReturnType() { | ||
return Timespan.class; | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "time since"; | ||
} | ||
|
||
@Override | ||
public String toString(final @Nullable Event e, final boolean debug) { | ||
return "the time since " + getExpr().toString(e, debug); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/test/skript/tests/syntaxes/expressions/ExprTimeSince.sk
This file contains 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,11 @@ | ||
test "time since": | ||
|
||
set {_time} to time since 1 hour ago | ||
assert {_time} is 1 hour with "time since 1 hour ago failed | value: %{_time}%" | ||
|
||
set {_time} to time since 1 day ago | ||
assert {_time} is 1 day with "time since 1 day ago failed | value: %{_time}%" | ||
|
||
set {_time} to time since 1 day later | ||
# {_time} won't update since "1 day later" returns null | ||
assert {_time} is 1 day with "time since 1 day later failed | value: %{_time}%" |