diff --git a/src/main/java/ch/njol/skript/effects/EffDetonate.java b/src/main/java/ch/njol/skript/effects/EffDetonate.java new file mode 100644 index 00000000000..30594262c7c --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffDetonate.java @@ -0,0 +1,63 @@ +package ch.njol.skript.effects; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.WindCharge; +import org.bukkit.entity.minecart.ExplosiveMinecart; +import org.bukkit.entity.Firework; +import org.bukkit.entity.Creeper; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; +import ch.njol.skript.Skript; +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.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; + +@Name("Detonate Entities") +@Description("Immediately detonates an entity. Accepted entities are fireworks, TNT minecarts, primed TNT, wind charges and creepers.") +@Examples("detonate last launched firework") +@Since("INSERT VERSION") +public class EffDetonate extends Effect { + + private static final boolean HAS_WINDCHARGE = Skript.classExists("org.bukkit.entity.WindCharge"); + + static { + Skript.registerEffect(EffDetonate.class, "detonate %entities%"); + } + + private Expression entities; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + entities = (Expression) exprs[0]; + return true; + } + + @Override + protected void execute(Event event) { + for (Entity entity : entities.getArray(event)) { + if (entity instanceof Firework firework) { + firework.detonate(); + } else if (HAS_WINDCHARGE && entity instanceof WindCharge windCharge) { + windCharge.explode(); + } else if (entity instanceof ExplosiveMinecart explosiveMinecart) { + explosiveMinecart.explode(); + } else if (entity instanceof Creeper creeper) { + creeper.explode(); + } else if (entity instanceof TNTPrimed tntPrimed) { + tntPrimed.setFuseTicks(0); + } + } + } + + public String toString(@Nullable Event event, boolean debug) { + return "detonate " + entities.toString(event, debug); + } + +} diff --git a/src/test/skript/tests/syntaxes/effects/EffDetonate.sk b/src/test/skript/tests/syntaxes/effects/EffDetonate.sk new file mode 100644 index 00000000000..aa88ac973b3 --- /dev/null +++ b/src/test/skript/tests/syntaxes/effects/EffDetonate.sk @@ -0,0 +1,21 @@ +test "detonate entity effect": + spawn a creeper at event-location + assert last spawned entity is valid with "creepers should not detonate when they are first spawned" + detonate last spawned entity + assert last spawned entity isn't valid with "creepers should instantly detonate when spawned" + +test "detonate explosive minecart" when running minecraft "1.19": + spawn a minecart with tnt at event-location + assert last spawned entity is valid with "minecarts with tnt should not detonate when they are first spawned" + detonate last spawned entity + assert last spawned entity isn't valid with "minecarts with tnt should instantly detonate when they are first spawned" + +test "detonate wind charge" when running minecraft "1.21": + spawn a wind charge at event-location + assert last spawned entity is valid with "wind charges should not detonate when they are first spawned" + detonate last spawned entity + assert last spawned entity isn't valid with "wind charges should instantly detonate when spawned" + + + +# The fireworks test cannot be added as fireworks are despawned a tick later and tests are instant