From 4812d20aec9457e9c852da952ea098bf55cae192 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:22:32 -0500 Subject: [PATCH 1/2] Initial Commit --- .../skript/conditions/CondIsCharging.java | 36 ++++ .../expressions/ExprExplosiveYield.java | 157 ++++++++---------- .../expressions/ExprExplosiveYield.sk | 13 ++ 3 files changed, 117 insertions(+), 89 deletions(-) create mode 100644 src/main/java/ch/njol/skript/conditions/CondIsCharging.java diff --git a/src/main/java/ch/njol/skript/conditions/CondIsCharging.java b/src/main/java/ch/njol/skript/conditions/CondIsCharging.java new file mode 100644 index 00000000000..49a1a761842 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsCharging.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:", + "\tkill last spawned ghast" +}) +@Since("INSERT VERSION") +public class CondIsCharging extends PropertyCondition { + + static { + register(CondIsCharging.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..dda1734aab5 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" +}) +@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; - 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; + if (entity instanceof Explosive explosive) { 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); + } else if (SUPPORTS_GHASTS && entity instanceof Ghast ghast) { + changeExplosionInteger(mode, intValue, ghast::getExplosionPower, ghast::setExplosionPower); } } } + private void changeExplosionInteger(ChangeMode mode, int value, Supplier getter, Consumer setter) { + 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); + }, + Integer.MAX_VALUE)); + } + @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..12c71baab5d 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk @@ -11,3 +11,16 @@ 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" + clear entity within {_entity} From c99268c7acc0adc32234ac89a491de4eeb52b5b6 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Sun, 2 Mar 2025 12:29:18 -0500 Subject: [PATCH 2/2] Requested Changes --- ...ndIsCharging.java => CondIsChargingFireball.java} | 6 +++--- .../njol/skript/expressions/ExprExplosiveYield.java | 12 ++++++------ .../tests/syntaxes/expressions/ExprExplosiveYield.sk | 6 ++++++ 3 files changed, 15 insertions(+), 9 deletions(-) rename src/main/java/ch/njol/skript/conditions/{CondIsCharging.java => CondIsChargingFireball.java} (77%) diff --git a/src/main/java/ch/njol/skript/conditions/CondIsCharging.java b/src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java similarity index 77% rename from src/main/java/ch/njol/skript/conditions/CondIsCharging.java rename to src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java index 49a1a761842..25766156787 100644 --- a/src/main/java/ch/njol/skript/conditions/CondIsCharging.java +++ b/src/main/java/ch/njol/skript/conditions/CondIsChargingFireball.java @@ -11,14 +11,14 @@ @Name("Is Charging Fireball") @Description("Check whether a ghast is charging a fireball.") @Examples({ - "if last spawned ghast is charging:", + "if last spawned ghast is charging fireball:", "\tkill last spawned ghast" }) @Since("INSERT VERSION") -public class CondIsCharging extends PropertyCondition { +public class CondIsChargingFireball extends PropertyCondition { static { - register(CondIsCharging.class, "charging [[a] fireball]", "livingentities"); + register(CondIsChargingFireball.class, "charging [a] fireball", "livingentities"); } @Override diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java index dda1734aab5..74355ca9557 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java +++ b/src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java @@ -19,7 +19,8 @@ @Name("Explosive Yield") @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" + "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:", @@ -35,7 +36,6 @@ public class ExprExplosiveYield extends SimplePropertyExpression register(ExprExplosiveYield.class, Number.class, "explosive (yield|radius|size|power)", "entities"); } - @Override public @Nullable Number convert(Entity entity) { if (entity instanceof Explosive explosive) { @@ -78,14 +78,14 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { } } } else if (entity instanceof Creeper creeper) { - changeExplosionInteger(mode, intValue, creeper::getExplosionRadius, creeper::setExplosionRadius); + 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); + changeExplosionInteger(mode, intValue, ghast::getExplosionPower, ghast::setExplosionPower, 127); } } } - private void changeExplosionInteger(ChangeMode mode, int value, Supplier getter, Consumer setter) { + private void changeExplosionInteger(ChangeMode mode, int value, Supplier getter, Consumer setter, int max) { setter.accept(Math2.fit(0, switch (mode) { case SET, DELETE -> value; @@ -93,7 +93,7 @@ private void changeExplosionInteger(ChangeMode mode, int value, Supplier getter.get() - value; default -> throw new IllegalArgumentException("Unexpected mode: " + mode); }, - Integer.MAX_VALUE)); + max)); } @Override diff --git a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk index 12c71baab5d..e395ca38d69 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk @@ -23,4 +23,10 @@ test "ghast explosive power": 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}