From bf3259835d400b9be2063fa4c24ea9732b730263 Mon Sep 17 00:00:00 2001 From: EquipableMC <66171067+EquipableMC@users.noreply.github.com> Date: Tue, 31 Dec 2024 09:48:48 -0500 Subject: [PATCH] Improve kill effect (#7148) * ignores totem and cleans up EffKill * improve syntax + add example docs * requested changes! * requested changes * requested changes * oops * requested changes * remove ignoring totem & remove setting the player to survival then back to creative * oopsies * remove useless imports * Use damage where possible to trigger death event. * Update src/main/java/ch/njol/skript/effects/EffKill.java Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> --------- Co-authored-by: cheeezburga <47320303+cheeezburga@users.noreply.github.com> Co-authored-by: Moderocky Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> --- .../java/ch/njol/skript/effects/EffKill.java | 58 +++++++++---------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffKill.java b/src/main/java/ch/njol/skript/effects/EffKill.java index 0ecc6146756..0dd869f92fd 100644 --- a/src/main/java/ch/njol/skript/effects/EffKill.java +++ b/src/main/java/ch/njol/skript/effects/EffKill.java @@ -1,6 +1,7 @@ package ch.njol.skript.effects; import ch.njol.skript.Skript; +import ch.njol.skript.bukkitutil.DamageUtils; import ch.njol.skript.bukkitutil.HealthUtils; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; @@ -10,73 +11,66 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; -import org.bukkit.GameMode; import org.bukkit.entity.Damageable; import org.bukkit.entity.EnderDragonPart; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Event; +import org.bukkit.event.entity.EntityDamageEvent; import org.jetbrains.annotations.Nullable; -/** - * @author Peter Güttinger - */ @Name("Kill") @Description({"Kills an entity.", "Note: This effect does not set the entity's health to 0 (which causes issues), but damages the entity by 100 times its maximum health."}) @Examples({"kill the player", "kill all creepers in the player's world", "kill all endermen, witches and bats"}) -@Since("1.0") +@Since("1.0, INSERT VERSION (ignoring totem of undying)") public class EffKill extends Effect { + private static final boolean SUPPORTS_DAMAGE_SOURCE = Skript.classExists("org.bukkit.damage.DamageSource"); + static { Skript.registerEffect(EffKill.class, "kill %entities%"); } - - // Absolutely make sure it dies - public static final int DAMAGE_AMOUNT = Integer.MAX_VALUE; - - @SuppressWarnings("null") + private Expression entities; - - @SuppressWarnings({"unchecked", "null"}) + @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) { - entities = (Expression) exprs[0]; + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { + //noinspection unchecked + this.entities = (Expression) exprs[0]; return true; } @Override - protected void execute(final Event e) { - for (Entity entity : entities.getArray(e)) { - - if (entity instanceof EnderDragonPart) { - entity = ((EnderDragonPart) entity).getParent(); - } - - if (entity instanceof Damageable) { - final boolean creative = entity instanceof Player && ((Player) entity).getGameMode() == GameMode.CREATIVE; - if (creative) // Set player to survival before applying damage - ((Player) entity).setGameMode(GameMode.SURVIVAL); + protected void execute(Event event) { + for (Entity entity : entities.getArray(event)) { - HealthUtils.damage((Damageable) entity, HealthUtils.getMaxHealth((Damageable) entity) * 100); // just to make sure that it really dies >:) + if (entity instanceof EnderDragonPart part) + entity = part.getParent(); - if (creative) // Set creative player back to creative - ((Player) entity).setGameMode(GameMode.CREATIVE); + if (entity instanceof Damageable damageable) { + if (SUPPORTS_DAMAGE_SOURCE) { + EntityDamageEvent.DamageCause cause = EntityDamageEvent.DamageCause.KILL; + HealthUtils.damage(damageable, 100 + damageable.getHealth(), DamageUtils.getDamageSourceFromCause(cause)); + } else { + HealthUtils.setHealth(damageable, 0); + HealthUtils.damage(damageable, 1); + } } // if everything done so far has failed to kill this thing // We also don't want to remove a player as this would remove the player's data from the server. if (entity.isValid() && !(entity instanceof Player)) entity.remove(); - + } } - + @Override - public String toString(final @Nullable Event e, final boolean debug) { - return "kill" + entities.toString(e, debug); + public String toString(@Nullable Event event, boolean debug) { + return "kill " + entities.toString(event, debug); } }