-
-
Notifications
You must be signed in to change notification settings - Fork 404
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 1 commit
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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
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.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(""" | ||
set {_target} to player's target entity | ||
send "%{_target}% has lived for %time lived of {_target}%" | ||
|
||
spawn zombie at player: | ||
set {_boss} to zombie | ||
# start off with 1 tick lived instead of 0 to avoid custom effects running off spawn | ||
add 1 tick to {_boss}'s time lived | ||
while {_boss} is alive: | ||
wait 1 tick | ||
# update display name every tick with hp | ||
set display name of {_boss} to "Boss %{_boss}'s health%/%{_boss}'s max health%" | ||
# heal every second | ||
if mod(ticks of {_boss}'s time lived, 20) is 0: | ||
add 1 to {_boss}'s health | ||
# push forward every 2 seconds | ||
if mod(ticks of {_boss}'s time lived, 40) is 0: | ||
push {_boss} forward at speed 1 | ||
# give strength every 10 seconds | ||
if mod(ticks of {_boss}'s time lived, 200) is 0: | ||
add potion effect of strength for 3 seconds to {_boss}'s potion effects | ||
""") | ||
@Since("INSERT VERSION") | ||
public class ExprTimeLived extends SimplePropertyExpression<Entity, Timespan> { | ||
|
||
static { | ||
register(ExprTimeLived.class, Timespan.class, "time (alive|lived)", "entities"); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Timespan convert(Entity entity) { | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new Timespan(TimePeriod.TICK, entity.getTicksLived()); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Class<?>[] acceptChange(ChangeMode mode) { | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (mode == ChangeMode.REMOVE_ALL || mode == ChangeMode.DELETE) | ||
return null; | ||
return CollectionUtils.array(Timespan.class); | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) { | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Entity entity = getExpr().getSingle(event); | ||
if (entity == null) return; | ||
|
||
int newTicks = 1; | ||
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. It might be better to store this and |
||
if (mode != ChangeMode.RESET && delta != null && delta.length > 0 && delta[0] instanceof Timespan timespan) { | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newTicks = (int) timespan.get(Timespan.TimePeriod.TICK); | ||
} | ||
|
||
int currentTicks = entity.getTicksLived(); | ||
int valueToSet = switch (mode) { | ||
case ADD -> currentTicks + newTicks; | ||
case REMOVE -> currentTicks - newTicks; | ||
case SET, RESET -> newTicks; | ||
default -> currentTicks; | ||
}; | ||
|
||
entity.setTicksLived(Math.max(1, valueToSet)); | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public Class<? extends Timespan> getReturnType() { | ||
return Timespan.class; | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "time lived"; | ||
} | ||
} | ||
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
MrScopes marked this conversation as resolved.
Show resolved
Hide resolved
|
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.