diff --git a/src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java b/src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java new file mode 100644 index 00000000000..25766156787 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java @@ -0,0 +1,36 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.conditions.base.PropertyCondition; +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 org.bukkit.entity.Ghast; +import org.bukkit.entity.LivingEntity; + +@Name("Is Charging Fireball") +@Description("Check whether a ghast is charging a fireball.") +@Examples({ + "if last spawned ghast is charging fireball:", + "\tkill last spawned ghast" +}) +@Since("INSERT VERSION") +public class CondIsChargingFireball extends PropertyCondition { + + static { + register(CondIsChargingFireball.class, "charging [a] fireball", "livingentities"); + } + + @Override + public boolean check(LivingEntity entity) { + if (entity instanceof Ghast ghast) + return ghast.isCharging(); + return false; + } + + @Override + protected String getPropertyName() { + return "charging fireball"; + } + +} diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java index 72f70126894..74355ca9557 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java +++ b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java @@ -1,122 +1,101 @@ package ch.njol.skript.expressions; +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.*; +import ch.njol.skript.expressions.base.SimplePropertyExpression; +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.Explosive; +import org.bukkit.entity.Ghast; import org.bukkit.event.Event; import org.jetbrains.annotations.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; +import java.util.function.Consumer; +import java.util.function.Supplier; @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("2.5") +@Description({ + "The yield of an explosive (creeper, ghast, primed tnt, fireball, etc.). This is how big of an explosion is caused by the entity.", + "Read this wiki page for more information.", + "The yield of ghasts can only be set to between 0 and 127." +}) +@Examples({ + "on spawn of a creeper:", + "\tset the explosive yield of the event-entity to 10" +}) +@RequiredPlugins("Paper (ghasts)") +@Since("2.5, INSERT VERSION (ghasts)") public class ExprExplosiveYield extends SimplePropertyExpression { + private static final boolean SUPPORTS_GHASTS = Skript.methodExists(Ghast.class, "getExplosionPower"); + static { - register(ExprExplosiveYield.class, Number.class, "explosive (yield|radius|size)", "entities"); + register(ExprExplosiveYield.class, Number.class, "explosive (yield|radius|size|power)", "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; + public @Nullable Number convert(Entity entity) { + if (entity instanceof Explosive explosive) { + return explosive.getYield(); + } else if (entity instanceof Creeper creeper) { + return creeper.getExplosionRadius(); + } else if (SUPPORTS_GHASTS && entity instanceof Ghast ghast) { + return ghast.getExplosionPower(); + } + return null; } @Override - @Nullable - public Class[] acceptChange(final ChangeMode mode) { - switch (mode) { - case SET: - case ADD: - case REMOVE: - case DELETE: - return CollectionUtils.array(Number.class); - default: - return null; - } + public Class @Nullable [] acceptChange(ChangeMode mode) { + return switch (mode) { + case SET, DELETE, ADD, REMOVE -> CollectionUtils.array(Number.class); + default -> null; + }; } @Override - public void change(final Event event, final @Nullable Object[] delta, final ChangeMode mode) { - Number change = delta != null ? (Number) delta[0] : 0; + public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { + Number number = delta != null ? (Number) delta[0] : 0; + float floatValue = Math2.fit(0, number.floatValue(), Float.MAX_VALUE); + int intValue = Math2.fit(0, number.intValue(), Integer.MAX_VALUE); + 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; + if (entity instanceof Explosive explosive) { 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; - default: - 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; + case SET, DELETE -> explosive.setYield(floatValue); + case ADD -> { + float current = explosive.getYield(); + float newValue = Math2.fit(0, current + floatValue, Float.MAX_VALUE); + explosive.setYield(newValue); + } + case REMOVE -> { + float current = explosive.getYield(); + float newValue = Math2.fit(0, current - floatValue, Float.MAX_VALUE); + explosive.setYield(newValue); + } } + } else if (entity instanceof Creeper creeper) { + changeExplosionInteger(mode, intValue, creeper::getExplosionRadius, creeper::setExplosionRadius, Integer.MAX_VALUE); + } else if (SUPPORTS_GHASTS && entity instanceof Ghast ghast) { + changeExplosionInteger(mode, intValue, ghast::getExplosionPower, ghast::setExplosionPower, 127); } } } + private void changeExplosionInteger(ChangeMode mode, int value, Supplier getter, Consumer setter, int max) { + setter.accept(Math2.fit(0, + switch (mode) { + case SET, DELETE -> value; + case ADD -> getter.get() + value; + case REMOVE -> getter.get() - value; + default -> throw new IllegalArgumentException("Unexpected mode: " + mode); + }, + max)); + } + @Override public Class getReturnType() { return Number.class; diff --git a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk index 7dcb67b6a2d..e395ca38d69 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk @@ -11,3 +11,22 @@ test "creeper explosive yield": 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 + +test "ghast explosive power": + spawn a ghast at test-location: + set {_entity} to entity + set the explosive yield of {_entity} to 10 + assert the explosive yield of {_entity} is 10 with "Explosive yield of ghast was not set to 10" + remove 20 from the explosive yield of {_entity} + assert the explosive yield of {_entity} is 0 with "Explosive yield of ghast should be capped at 0 minimum" + add 100 to the explosive yield of {_entity} + assert the explosive yield of {_entity} is 100 with "Explosive yield of ghast should have had 100 added" + clear the explosive yield of {_entity} + assert the explosive yield of {_entity} is 0 with "Explosive yield of ghast was not cleared" + set the explosive yield of {_entity} to nan value + assert the explosive yield of {_entity} is 0 with "Explosive yield of ghast should have no effect of nan value" + set the explosive yield of {_entity} to infinity value + assert the explosive yield of {_entity} is 127 with "Explosive yield of ghast should be capped at 127 (max)" + set the explosive yield of {_entity} to -100000 + assert the explosive yield of {_entity} is 0 with "Explosive yield of ghast should be capped at 0 (min)" + clear entity within {_entity}