Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

90 changes: 90 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprFuseDuration.java
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("""
Comment on lines +20 to +21
Copy link
Copy Markdown
Member

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 duration and have creepers support all, tnt support remaining. Thoughts? I'm not a fan of fuse duration meaning two different things and also just not implementing creeper's getFuseTicks

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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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());
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());
}

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";
}

}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.entity.SimpleEntityData;
import ch.njol.skript.expressions.ExprFuseDuration;
import org.bukkit.entity.AbstractNautilus;
import org.skriptlang.skript.addon.AddonModule;
import org.skriptlang.skript.addon.HierarchicalAddonModule;
Expand Down Expand Up @@ -38,7 +39,8 @@ protected void loadSelf(SkriptAddon addon) {
}

register(addon,
ExprDeathMessage::register
ExprDeathMessage::register,
ExprFuseDuration::register
);
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprFuseDuration.sk
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
delete entity
delete entity