-
-
Notifications
You must be signed in to change notification settings - Fork 406
Add ExprTimeLived #8134
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
MrScopes
wants to merge
18
commits into
SkriptLang:dev/feature
Choose a base branch
from
MrScopes:exprtimelived
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
Add ExprTimeLived #8134
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ecce69d
Add ExprTimeLived
MrScopes 8cc1aed
Merge branch 'dev/feature' into exprtimelived
Absolutionism 80bd802
adjust annotation
MrScopes 6d114b5
adjust annotation
MrScopes 3a8866f
adjust annotation
MrScopes cc59c9c
space after method
MrScopes 006ddf1
Remove unnecessary conditions
MrScopes 737af81
Add casting back to condition
MrScopes 4d586fc
adjust acceptChange to only allow add/remove/set instead of everythin…
MrScopes c497e0e
allow multiple entities to be changed
MrScopes 5efb6a2
simpler examples
MrScopes 1c32c22
add reset to accepted changing methods
MrScopes 843e725
fix inconsistency in example
MrScopes 777e567
Remove unneeded quote blocks
MrScopes 3833bcb
blank last line
MrScopes 72ea3bc
Make sure it doesn't go above max integer limit
MrScopes 47db240
Math2 import
MrScopes 7f2f69c
remove start of interaction code moved to another branch
MrScopes 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
77 changes: 77 additions & 0 deletions
77
src/main/java/ch/njol/skript/expressions/ExprTimeLived.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,77 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.classes.Changer.ChangeMode; | ||
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.expressions.base.SimplePropertyExpression; | ||
import ch.njol.skript.util.Timespan; | ||
import ch.njol.skript.util.Timespan.TimePeriod; | ||
import ch.njol.util.Math2; | ||
import ch.njol.util.coll.CollectionUtils; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Name("Time Lived of Entity") | ||
@Description(""" | ||
Returns the total amount of time the entity has lived. | ||
Note: This does not reset when a player dies. | ||
""") | ||
@Example("clear all entities where [input's time lived > 1 hour]") | ||
@Example(""" | ||
on right click on entity: | ||
send "%entity% has lived for %time lived of entity%" to player | ||
""") | ||
@Since("INSERT VERSION") | ||
public class ExprTimeLived extends SimplePropertyExpression<Entity, Timespan> { | ||
|
||
static { | ||
register(ExprTimeLived.class, Timespan.class, "time (alive|lived)", "entities"); | ||
} | ||
|
||
@Override | ||
public @Nullable Timespan convert(Entity entity) { | ||
return new Timespan(TimePeriod.TICK, entity.getTicksLived()); | ||
} | ||
|
||
@Override | ||
public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
return switch (mode) { | ||
case ADD, REMOVE, SET, RESET -> CollectionUtils.array(Timespan.class); | ||
default -> null; | ||
}; | ||
} | ||
|
||
@Override | ||
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
int newTicks = 1; | ||
if (delta != null && delta[0] instanceof Timespan timespan) { | ||
newTicks = (int) timespan.get(Timespan.TimePeriod.TICK); | ||
} | ||
|
||
for (Entity entity : getExpr().getArray(event)) { | ||
int currentTicks = entity.getTicksLived(); | ||
int valueToSet = switch (mode) { | ||
case ADD -> currentTicks + newTicks; | ||
case REMOVE -> currentTicks - newTicks; | ||
case SET, RESET -> newTicks; | ||
default -> currentTicks; | ||
}; | ||
|
||
entity.setTicksLived(Math2.fit(1, valueToSet, Integer.MAX_VALUE)); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<? extends Timespan> getReturnType() { | ||
return Timespan.class; | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "time lived"; | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to store this and
valueToSet
as longs to account for overflows. That is, if the value is set to high for an int, clamp it down to the max int value.