diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplosionYield.java b/src/main/java/ch/njol/skript/expressions/ExprExplosionYield.java new file mode 100644 index 00000000000..1a04bc362b0 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprExplosionYield.java @@ -0,0 +1,128 @@ +/** + * 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.expressions; + +import org.bukkit.event.Event; +import org.bukkit.event.entity.ExplosionPrimeEvent; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Events; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; + +@Name("Explosion Yield") +@Description({"The yield of the explosion in an explosion prime event. This is how big the explosion is.", + " When changing the yield, values less than 0 will be ignored.", + " Read this wiki page for more information"}) +@Examples({"on explosion prime:", + "\tset the yield of the explosion to 10"}) +@Events("explosion prime") +@Since("INSERT VERSION") +public class ExprExplosionYield extends SimpleExpression { + + static { + Skript.registerExpression(ExprExplosionYield.class, Number.class, ExpressionType.SIMPLE, + "[the] explosion (yield|radius|size)", + "[the] (yield|radius|size) of [the] explosion" + ); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(ExplosionPrimeEvent.class)) { + Skript.error("The explosion radius is only usable in explosion prime events", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @Override + @Nullable + protected Number[] get(Event e) { + return new Number[]{((ExplosionPrimeEvent) e).getRadius()}; + } + + @Override + @Nullable + public Class[] acceptChange(final ChangeMode mode) { + if (mode == ChangeMode.REMOVE_ALL || mode == ChangeMode.RESET) + return null; + return CollectionUtils.array(Number.class); + } + + @Override + public void change(final Event event, final @Nullable Object[] delta, final ChangeMode mode) { + float f = delta == null ? 0 : ((Number) delta[0]).floatValue(); + if (f < 0) // Negative values will throw an error. + return; + ExplosionPrimeEvent e = (ExplosionPrimeEvent) event; + switch (mode) { + case SET: + e.setRadius(f); + break; + case ADD: + float add = e.getRadius() + f; + if (add < 0) + return; + e.setRadius(add); + break; + case REMOVE: + float subtract = e.getRadius() - f; + if (subtract < 0) + return; + e.setRadius(subtract); + break; + case DELETE: + e.setRadius(0); + break; + case REMOVE_ALL: + case RESET: + assert false; + } + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "the yield of the explosion"; + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java new file mode 100644 index 00000000000..e854a8f5019 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java @@ -0,0 +1,144 @@ +/** + * 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.expressions; + +import org.bukkit.entity.Creeper; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Explosive; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.util.coll.CollectionUtils; + +@Name("Explosive Yield") +@Description({"The yield of an explosive (creeper, primed tnt, fireball, etc.). This is how big of an explosion is caused by the entity.", + "Read this wiki page for more information"}) +@Examples({"on spawn of a creeper:", + "\tset the explosive yield of the event-entity to 10"}) +@RequiredPlugins("Minecraft 1.12 or newer for creepers") +@Since("INSERT VERSION") +public class ExprExplosiveYield extends SimplePropertyExpression { + + static { + register(ExprExplosiveYield.class, Number.class, "explosive (yield|radius|size)", "entities"); + } + + private final static boolean CREEPER_USABLE = Skript.methodExists(Creeper.class, "getExplosionRadius"); + + @Override + public Number convert(Entity e) { + if (e instanceof Explosive) + return ((Explosive) e).getYield(); + if (CREEPER_USABLE && e instanceof Creeper) + return ((Creeper) e).getExplosionRadius(); + return 0; + } + + @Override + @Nullable + public Class[] acceptChange(final ChangeMode mode) { + if (mode == ChangeMode.REMOVE_ALL || mode == ChangeMode.RESET) + return null; + return CollectionUtils.array(Number.class); + } + + @Override + public void change(final Event event, final @Nullable Object[] delta, final ChangeMode mode) { + Number change = delta != null ? (Number) delta[0] : 0; + for (Entity entity : getExpr().getArray(event)) { + if (entity instanceof Explosive) { + Explosive e = (Explosive) entity; + float f = change.floatValue(); + if (f < 0) // Negative values will throw an error. + return; + switch (mode) { + case SET: + e.setYield(f); + break; + case ADD: + float add = e.getYield() + f; + if (add < 0) + return; + e.setYield(add); + break; + case REMOVE: + float subtract = e.getYield() - f; + if (subtract < 0) + return; + e.setYield(subtract); + break; + case DELETE: + e.setYield(0); + break; + case REMOVE_ALL: + case RESET: + assert false; + } + } else if (CREEPER_USABLE && entity instanceof Creeper) { + Creeper c = (Creeper) entity; + int i = change.intValue(); + if (i < 0) // Negative values will throw an error. + return; + switch (mode) { + case SET: + c.setExplosionRadius(i); + break; + case ADD: + int add = c.getExplosionRadius() + i; + if (add < 0) + return; + c.setExplosionRadius(add); + break; + case REMOVE: + int subtract = c.getExplosionRadius() - i; + if (subtract < 0) + return; + c.setExplosionRadius(subtract); + break; + case DELETE: + c.setExplosionRadius(0); + break; + case REMOVE_ALL: + case RESET: + assert false; + } + } + } + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + protected String getPropertyName() { + return "explosive yield"; + } + +} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk new file mode 100644 index 00000000000..1797bb2fd4b --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk @@ -0,0 +1,13 @@ +test "explosive yield": + spawn a primed tnt at spawn of world "world" + assert explosive yield of last spawned tnt is 4 with "explosive yield of tnt is 4 by default" + set explosive yield of last spawned tnt to 10 + assert explosive yield of last spawned tnt is 10 with "an explosive's explosive yield should be 10 if it is set to 10" + delete last spawned primed tnt + +test "creeper explosive yield" when minecraft version is "1.12.2": + spawn a creeper at spawn of world "world" + assert explosive yield of last spawned creeper is 3 with "explosive yield of a creeper is 3 by default" + set explosive yield of last spawned creeper to 10 + assert explosive yield of last spawned creeper is 10 with "a creeper's explosive yield should be 10 if it is set to 10" + delete last spawned creeper