diff --git a/src/main/java/ch/njol/skript/conditions/CondIncendiary.java b/src/main/java/ch/njol/skript/conditions/CondIncendiary.java new file mode 100644 index 00000000000..c7f0b981413 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIncendiary.java @@ -0,0 +1,91 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.conditions; + +import ch.njol.skript.ScriptLoader; +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.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Explosive; +import org.bukkit.event.Event; +import org.bukkit.event.entity.ExplosionPrimeEvent; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Is Incendiary") +@Description("Checks if an entity will create fire when it explodes. This condition is also usable in an explosion prime event.") +@Examples({"on explosion prime:", + "\tif the explosion is fiery:", + "\t\tbroadcast \"A fiery explosive has been ignited!\""}) +@Since("INSERT VERSION") +public class CondIncendiary extends Condition { + + static { + Skript.registerCondition(CondIncendiary.class, + "%entities% ((is|are) incendiary|cause[s] a[n] (incendiary|fiery) explosion)", + "%entities% ((is not|are not|isn't|aren't) incendiary|(does not|do not|doesn't|don't) cause[s] a[n] (incendiary|fiery) explosion)", + "the [event(-| )]explosion (is|1¦(is not|isn't)) (incendiary|fiery)" + ); + } + + @SuppressWarnings("null") + private Expression entities; + + private boolean isEvent; + + @Override + @SuppressWarnings({"unchecked", "null"}) + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + isEvent = matchedPattern == 2; + if (isEvent && !ScriptLoader.isCurrentEvent(ExplosionPrimeEvent.class)) { + Skript.error("Checking if 'the explosion' is fiery is only possible in an explosion prime event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + if (!isEvent) + entities = (Expression) exprs[0]; + setNegated(matchedPattern == 1 || parseResult.mark == 1); + return true; + } + + @Override + public boolean check(Event e) { + if (isEvent) + return ((ExplosionPrimeEvent) e).getFire() ^ isNegated(); + return entities.check(e, entity -> entity instanceof Explosive && ((Explosive) entity).isIncendiary(), isNegated()); + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + if (isEvent) + return "the event-explosion " + (isNegated() == false ? "is" : "is not") + " incendiary"; + if (entities.isSingle()) + return entities.toString(e, debug) + (isNegated() == false ? " is" : " is not") + " incendiary"; + return entities.toString(e, debug) + (isNegated() == false ? " are" : " are not") + " incendiary"; + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffIncendiary.java b/src/main/java/ch/njol/skript/effects/EffIncendiary.java new file mode 100644 index 00000000000..9adc81e8409 --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffIncendiary.java @@ -0,0 +1,95 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.effects; + +import ch.njol.skript.ScriptLoader; +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.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Explosive; +import org.bukkit.event.Event; +import org.bukkit.event.entity.ExplosionPrimeEvent; +import org.eclipse.jdt.annotation.Nullable; + +@Name("Make Incendiary") +@Description("Sets if an entity's explosion will leave behind fire. This effect is also usable in an explosion prime event.") +@Examples({"on explosion prime:", + "\tmake the explosion fiery"}) +@Since("INSERT VERSION") +public class EffIncendiary extends Effect { + + static { + Skript.registerEffect(EffIncendiary.class, + "make %entities% [(1¦not)] incendiary", + "make %entities%'[s] explosion [(1¦not)] (incendiary|fiery)", + "make [the] [event(-| )]explosion [(1¦not)] (incendiary|fiery)" + ); + } + + @SuppressWarnings("null") + private Expression entities; + + private boolean causeFire; + + private boolean isEvent; + + @Override + @SuppressWarnings({"unchecked", "null"}) + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + isEvent = matchedPattern == 2; + if (isEvent && !ScriptLoader.isCurrentEvent(ExplosionPrimeEvent.class)) { + Skript.error("Making 'the explosion' fiery is only usable in an explosion prime event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + if (!isEvent) + entities = (Expression) exprs[0]; + causeFire = parseResult.mark != 1; + return true; + } + + @Override + protected void execute(Event e) { + if (isEvent) { + ((ExplosionPrimeEvent) e).setFire(causeFire); + } else { + for (Entity entity : entities.getArray(e)) { + if (entity instanceof Explosive) + ((Explosive) entity).setIsIncendiary(causeFire); + } + } + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + if (isEvent) + return "make the event-explosion " + (causeFire == true ? "" : "not") + " fiery"; + return "make " + entities.toString(e, debug) + (causeFire == true ? "" : " not") + " incendiary"; + } + +} diff --git a/src/test/skript/tests/syntaxes/effects/EffExplodeWithFire.sk b/src/test/skript/tests/syntaxes/effects/EffExplodeWithFire.sk new file mode 100644 index 00000000000..fce4c2d37eb --- /dev/null +++ b/src/test/skript/tests/syntaxes/effects/EffExplodeWithFire.sk @@ -0,0 +1,8 @@ +test "explode with fire effect/condition": + spawn a primed tnt at spawn of world "world" + assert last spawned primed tnt doesn't cause an incendiary explosion with "a primed tnt's explosion shouldn't cause fire by default" + make the last spawned primed tnt's explosion fiery + assert last spawned primed tnt causes an incendiary explosion with "a primed tnt's explosion should cause fire when set to" + make the last spawned primed tnt's explosion not fiery + assert last spawned primed tnt doesn't cause an incendiary explosion with "a primed tnt's explosion shouldn't cause fire when set not to" + delete last spawned tnt