Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ghast Support #7597

Open
wants to merge 3 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsCharging.java
Original file line number Diff line number Diff line change
@@ -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<LivingEntity> {

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";
}

}
157 changes: 68 additions & 89 deletions src/main/java/ch/njol/skript/expressions/ExprExplosiveYield.java
Original file line number Diff line number Diff line change
@@ -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 <a href='https://minecraft.wiki/w/Explosion'>this wiki page</a> 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 <a href='https://minecraft.wiki/w/Explosion'>this wiki page</a> 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<Entity, Number> {

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<Integer> getter, Consumer<Integer> 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<Number> getReturnType() {
return Number.class;
Expand Down
13 changes: 13 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprExplosiveYield.sk
Original file line number Diff line number Diff line change
Expand Up @@ -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}