forked from SkriptLang/Skript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffKill.java
More file actions
75 lines (62 loc) · 2.3 KB
/
EffKill.java
File metadata and controls
75 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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.Example;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
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;
@Name("Kill")
@Description("Kills an entity.")
@Example("kill the player")
@Example("kill all creepers in the player's world")
@Example("kill all endermen, witches and bats")
@Since("1.0, 2.10 (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%");
}
private Expression<Entity> entities;
@Override
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(Event event) {
for (Entity entity : entities.getArray(event)) {
if (entity instanceof EnderDragonPart part)
entity = part.getParent();
if (entity instanceof Damageable damageable) {
if (SUPPORTS_DAMAGE_SOURCE) {
EntityDamageEvent.DamageCause cause = EntityDamageEvent.DamageCause.KILL;
HealthUtils.damage(damageable, Double.POSITIVE_INFINITY, 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(@Nullable Event event, boolean debug) {
return "kill " + entities.toString(event, debug);
}
}