Skip to content

Commit

Permalink
Improve kill effect (#7148)
Browse files Browse the repository at this point in the history
* 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 <admin@moderocky.com>
Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 31, 2024
1 parent 8ab0fad commit bf32598
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions src/main/java/ch/njol/skript/effects/EffKill.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Entity> entities;

@SuppressWarnings({"unchecked", "null"})

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
entities = (Expression<Entity>) exprs[0];
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
//noinspection unchecked
this.entities = (Expression<Entity>) 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);
}

}

0 comments on commit bf32598

Please sign in to comment.