-
-
Notifications
You must be signed in to change notification settings - Fork 439
Add ExprFuseTicks #8552
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
base: dev/feature
Are you sure you want to change the base?
Add ExprFuseTicks #8552
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||||||||||
| 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.util.Math2; | ||||||||||||||||||||
| import ch.njol.util.coll.CollectionUtils; | ||||||||||||||||||||
| import org.bukkit.entity.Creeper; | ||||||||||||||||||||
| import org.bukkit.entity.Entity; | ||||||||||||||||||||
| import org.bukkit.entity.TNTPrimed; | ||||||||||||||||||||
| import org.bukkit.event.Event; | ||||||||||||||||||||
| import org.jetbrains.annotations.Nullable; | ||||||||||||||||||||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Name("Fuse Duration") | ||||||||||||||||||||
| @Description("The fuse duration of a creeper or primed TNT. For creepers, this is the total fuse duration before explosion. For primed TNT, this is the remaining time before explosion.") | ||||||||||||||||||||
| @Example(""" | ||||||||||||||||||||
| on spawn of a creeper: | ||||||||||||||||||||
| set the fuse duration of the event-entity to 5 seconds | ||||||||||||||||||||
| """) | ||||||||||||||||||||
| @Example(""" | ||||||||||||||||||||
| on spawn of primed tnt: | ||||||||||||||||||||
| set fuse duration of event-entity to 10 seconds | ||||||||||||||||||||
| """) | ||||||||||||||||||||
| @Since("INSERT VERSION") | ||||||||||||||||||||
| public class ExprFuseDuration extends SimplePropertyExpression<Entity, Timespan> { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public static void register(SyntaxRegistry registry) { | ||||||||||||||||||||
| registry.register( | ||||||||||||||||||||
| SyntaxRegistry.EXPRESSION, | ||||||||||||||||||||
| infoBuilder(ExprFuseDuration.class, Timespan.class, "fuse (duration|time)", "entities", false).build() | ||||||||||||||||||||
|
Member
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. missing .supplier() |
||||||||||||||||||||
| ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| public @Nullable Timespan convert(Entity entity) { | ||||||||||||||||||||
| if (entity instanceof Creeper creeper) | ||||||||||||||||||||
| return new Timespan(Timespan.TimePeriod.TICK, creeper.getMaxFuseTicks()); | ||||||||||||||||||||
| else if (entity instanceof TNTPrimed tnt) | ||||||||||||||||||||
| return new Timespan(Timespan.TimePeriod.TICK, tnt.getFuseTicks()); | ||||||||||||||||||||
|
Comment on lines
+41
to
+44
Contributor
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.
Suggested change
|
||||||||||||||||||||
| return null; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||||||||||||||||||||
| return switch (mode) { | ||||||||||||||||||||
| case SET, DELETE, ADD, REMOVE -> CollectionUtils.array(Timespan.class); | ||||||||||||||||||||
| default -> null; | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||||||||||||||||||||
| int ticks = delta != null && delta[0] instanceof Timespan ts | ||||||||||||||||||||
| ? (int) Math2.fit(0, ts.get(Timespan.TimePeriod.TICK), Integer.MAX_VALUE) | ||||||||||||||||||||
| : 0; | ||||||||||||||||||||
| for (Entity entity : getExpr().getArray(event)) { | ||||||||||||||||||||
| if (entity instanceof Creeper creeper) { | ||||||||||||||||||||
| creeper.setMaxFuseTicks(Math2.fit(0, switch (mode) { | ||||||||||||||||||||
| case SET, DELETE -> ticks; | ||||||||||||||||||||
| case ADD -> creeper.getMaxFuseTicks() + ticks; | ||||||||||||||||||||
| case REMOVE -> creeper.getMaxFuseTicks() - ticks; | ||||||||||||||||||||
| default -> throw new IllegalArgumentException("Unexpected mode: " + mode); | ||||||||||||||||||||
| }, Integer.MAX_VALUE)); | ||||||||||||||||||||
| } else if (entity instanceof TNTPrimed tnt) { | ||||||||||||||||||||
| tnt.setFuseTicks(Math2.fit(0, switch (mode) { | ||||||||||||||||||||
| case SET, DELETE -> ticks; | ||||||||||||||||||||
| case ADD -> tnt.getFuseTicks() + ticks; | ||||||||||||||||||||
| case REMOVE -> tnt.getFuseTicks() - ticks; | ||||||||||||||||||||
| default -> throw new IllegalArgumentException("Unexpected mode: " + mode); | ||||||||||||||||||||
| }, Integer.MAX_VALUE)); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| public Class<Timespan> getReturnType() { | ||||||||||||||||||||
| return Timespan.class; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| protected String getPropertyName() { | ||||||||||||||||||||
| return "fuse duration"; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
Member
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.
Suggested change
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||
| test "creeper fuse duration": | ||||||||
| spawn a creeper at test-location: | ||||||||
| assert fuse duration of entity is 1.5 seconds with "fuse duration of a creeper should be 1.5 seconds by default" | ||||||||
| set fuse duration of entity to 5 seconds | ||||||||
| assert fuse duration of entity is 5 seconds with "fuse duration of creeper should be 5 seconds after setting to 5 seconds" | ||||||||
| add 2.5 seconds to fuse duration of entity | ||||||||
| assert fuse duration of entity is 7.5 seconds with "fuse duration of creeper should be 7.5 seconds after adding 2.5 seconds" | ||||||||
| remove 10 seconds from fuse duration of entity | ||||||||
| assert fuse duration of entity is 0 seconds with "fuse duration of creeper should be capped at 0 minimum" | ||||||||
| delete fuse duration of entity | ||||||||
| assert fuse duration of entity is 0 seconds with "fuse duration of creeper should be 0 seconds after deletion" | ||||||||
| delete entity | ||||||||
|
|
||||||||
| test "primed tnt fuse duration": | ||||||||
| spawn a primed tnt at test-location: | ||||||||
| assert fuse duration of entity is 4 seconds with "fuse duration of primed tnt should be 4 seconds by default" | ||||||||
| set fuse duration of entity to 10 seconds | ||||||||
| assert fuse duration of entity is 10 seconds with "fuse duration of primed tnt should be 10 seconds after setting to 10 seconds" | ||||||||
| add 2.5 seconds to fuse duration of entity | ||||||||
| assert fuse duration of entity is 12.5 seconds with "fuse duration of primed tnt should be 12.5 seconds after adding 2.5 seconds" | ||||||||
| remove 15 seconds from fuse duration of entity | ||||||||
| assert fuse duration of entity is 0 seconds with "fuse duration of primed tnt should be capped at 0 minimum" | ||||||||
| delete fuse duration of entity | ||||||||
| assert fuse duration of entity is 0 seconds with "fuse duration of primed tnt should be 0 seconds after deletion" | ||||||||
| delete entity | ||||||||
|
Member
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.
Suggested change
|
||||||||
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.
So creepers have max and elapsed fuse ticks, with which you can calculate remaining.
Tnt just has remaining fuse ticks.
I think you should change this to
[:elapsed|:remaining|max] fuse durationand have creepers support all, tnt support remaining. Thoughts? I'm not a fan offuse durationmeaning two different things and also just not implementing creeper'sgetFuseTicks