Skip to content

Commit

Permalink
Fiery Explosion Effect and Condition (#2779)
Browse files Browse the repository at this point in the history
* Fiery Explosion Stuff

* Fix Condition

* Merge conditions

* Make Requested Changes

* Fix test

* Pattern Changes & Refactoring

* Test and pattern fixes
  • Loading branch information
APickledWalrus authored Jul 20, 2020
1 parent 3a42aa4 commit 6df393e
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIncendiary.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*
* 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<Entity> 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<Entity>) 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";
}

}
95 changes: 95 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffIncendiary.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*
* 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<Entity> 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<Entity>) 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";
}

}
8 changes: 8 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffExplodeWithFire.sk
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6df393e

Please sign in to comment.