Skip to content

Commit d74f6bc

Browse files
authored
Support for Kill damageable entities (#13667)
1 parent dd203a2 commit d74f6bc

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

paper-api/src/main/java/org/bukkit/entity/Damageable.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import org.bukkit.attribute.Attribute;
44
import org.bukkit.damage.DamageSource;
5-
import org.jetbrains.annotations.NotNull;
6-
import org.jetbrains.annotations.Nullable;
5+
import org.bukkit.damage.DamageType;
6+
import org.bukkit.event.entity.EntityRegainHealthEvent;
7+
import org.jspecify.annotations.NullMarked;
8+
import org.jspecify.annotations.Nullable;
79

810
/**
911
* Represents an {@link Entity} that has health and can take damage.
1012
*/
13+
@NullMarked
1114
public interface Damageable extends Entity {
1215
/**
1316
* Deals the given amount of damage to this entity.
@@ -32,7 +35,24 @@ public interface Damageable extends Entity {
3235
* @param amount amount of damage to deal
3336
* @param damageSource source to which the damage should be attributed
3437
*/
35-
void damage(double amount, @NotNull DamageSource damageSource);
38+
void damage(double amount, DamageSource damageSource);
39+
40+
/**
41+
* Sets the entity's health to 0 and kill the entity with a generic DamageSource.
42+
*
43+
* @throws IllegalStateException if is used in world generation
44+
*/
45+
default void kill() {
46+
this.kill(DamageSource.builder(DamageType.GENERIC_KILL).build());
47+
}
48+
49+
/**
50+
* Sets the entity's health to 0 and kill the entity with the specified DamageSource.
51+
*
52+
* @param damageSource the DamageSource to use for the kill
53+
* @throws IllegalStateException if is used in world generation
54+
*/
55+
void kill(DamageSource damageSource);
3656

3757
/**
3858
* Gets the entity's health from 0 to {@link #getMaxHealth()}, where 0 is dead.
@@ -57,7 +77,7 @@ public interface Damageable extends Entity {
5777
* @param amount heal amount
5878
*/
5979
default void heal(final double amount) {
60-
this.heal(amount, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.CUSTOM);
80+
this.heal(amount, EntityRegainHealthEvent.RegainReason.CUSTOM);
6181
}
6282

6383
/**
@@ -66,7 +86,7 @@ default void heal(final double amount) {
6686
* @param amount heal amount
6787
* @param reason heal reason
6888
*/
69-
void heal(double amount, @NotNull org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason reason);
89+
void heal(double amount, EntityRegainHealthEvent.RegainReason reason);
7090

7191
/**
7292
* Gets the entity's absorption amount.

paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftEnderDragonPart.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public void damage(double amount, Entity source) {
3131
this.getParent().damage(amount, source);
3232
}
3333

34+
@Override
35+
public void kill(DamageSource damageSource) {
36+
this.getParent().kill(damageSource);
37+
}
38+
3439
@Override
3540
public double getHealth() {
3641
return this.getParent().getHealth();

paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.bukkit.entity.WitherSkull;
9292
import org.bukkit.entity.memory.MemoryKey;
9393
import org.bukkit.event.entity.EntityPotionEffectEvent;
94+
import org.bukkit.event.entity.EntityRegainHealthEvent;
9495
import org.bukkit.inventory.EntityEquipment;
9596
import org.bukkit.inventory.ItemStack;
9697
import org.bukkit.potion.PotionEffect;
@@ -146,12 +147,10 @@ public void setHealth(double health) {
146147
}
147148
}
148149

149-
// Paper start - entity heal API
150150
@Override
151-
public void heal(final double amount, final org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason reason) {
151+
public void heal(final double amount, final EntityRegainHealthEvent.RegainReason reason) {
152152
this.getHandle().heal((float) amount, reason);
153153
}
154-
// Paper end - entity heal API
155154

156155
@Override
157156
public double getAbsorptionAmount() {
@@ -401,6 +400,15 @@ public void setBeeStingersInBody(int count) {
401400
this.getHandle().setStingerCount(count);
402401
}
403402

403+
@Override
404+
public void kill(org.bukkit.damage.DamageSource damageSource) {
405+
Preconditions.checkState(!this.getHandle().generation, "Cannot kill entity during world generation");
406+
Preconditions.checkArgument(damageSource != null, "damageSource cannot be null");
407+
408+
this.getHandle().setHealth(0);
409+
this.getHandle().die(((CraftDamageSource) damageSource).getHandle());
410+
}
411+
404412
@Override
405413
public void damage(double amount) {
406414
this.damage(amount, this.getHandle().damageSources().generic());

0 commit comments

Comments
 (0)