Skip to content

Commit

Permalink
🚀 Add timespan details expression & Improvements (#4661)
Browse files Browse the repository at this point in the history
* 🚀 Add timespan details expression

* Changes

* Improvements

* Changes

* Add DD:HH:MM:SS.ms + improve toString

* Update src/main/java/ch/njol/skript/util/Timespan.java

Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com>

* Update src/main/java/ch/njol/skript/util/Timespan.java

Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com>

* Update src/main/java/ch/njol/skript/util/Timespan.java

Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com>

* Update src/main/java/ch/njol/skript/util/Timespan.java

Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com>

* Testing

* Improvements - Unfinished  code

* Update enums

* Fix building

* Test build

* Test 2

* Fix code

* Requested Changes & Improvements

* Requested Changes

* Pikachu's quality requested changes 🚀

* Apply suggestions

* typo

* Update src/main/java/ch/njol/skript/expressions/ExprTimespanDetails.java

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Add Timespan(TimePeriod, long) and deprecate fromTicks

* Add getAs(TimePeriod)

---------

Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com>
Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com>
Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>
Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
5 people authored Mar 19, 2024
1 parent ae1f465 commit c59ff5b
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 74 deletions.
73 changes: 73 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprTimespanDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

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.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Timespan.TimePeriod;
import ch.njol.util.Kleenean;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Locale;

@Name("Timespan Details")
@Description("Retrieve specific information of a <a href=\"/classes.html#timespan\">timespan</a> such as hours/minutes/etc.")
@Examples({
"set {_t} to difference between now and {Payouts::players::%uuid of player%::last-date}",
"send \"It has been %days of {_t}% day(s) since last payout.\""
})
@Since("INSERT VERSION")
public class ExprTimespanDetails extends SimplePropertyExpression<Timespan, Long> {

static {
register(ExprTimespanDetails.class, Long.class, "(:(tick|second|minute|hour|day|week|month|year))s", "timespans");
}

@SuppressWarnings("NotNullFieldNotInitialized")
private TimePeriod type;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
type = TimePeriod.valueOf(parseResult.tags.get(0).toUpperCase(Locale.ENGLISH));
return super.init(exprs, matchedPattern, isDelayed, parseResult);
}

@Override
@Nullable
public Long convert(Timespan time) {
return time.getMilliSeconds() / type.getTime();
}

@Override
public Class<? extends Long> getReturnType() {
return Long.class;
}

@Override
protected String getPropertyName() {
return type.name().toLowerCase(Locale.ENGLISH);
}

}
Loading

0 comments on commit c59ff5b

Please sign in to comment.