Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 25 additions & 5 deletions paper-api/src/main/java/org/bukkit/entity/Damageable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import org.bukkit.attribute.Attribute;
import org.bukkit.damage.DamageSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.bukkit.damage.DamageType;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Represents an {@link Entity} that has health and can take damage.
*/
@NullMarked
public interface Damageable extends Entity {
/**
* Deals the given amount of damage to this entity.
Expand All @@ -32,7 +35,24 @@ public interface Damageable extends Entity {
* @param amount amount of damage to deal
* @param damageSource source to which the damage should be attributed
*/
void damage(double amount, @NotNull DamageSource damageSource);
void damage(double amount, DamageSource damageSource);

/**
* Sets the entity's health to 0 and kill the entity with a generic DamageSource.
*
* @throws IllegalStateException if is used in world generation
*/
default void kill() {
this.kill(DamageSource.builder(DamageType.GENERIC_KILL).build());
}

/**
* Sets the entity's health to 0 and kill the entity with the specified DamageSource.
*
* @param damageSource the DamageSource to use for the kill
* @throws IllegalStateException if is used in world generation
*/
void kill(DamageSource damageSource);

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

/**
Expand All @@ -66,7 +86,7 @@ default void heal(final double amount) {
* @param amount heal amount
* @param reason heal reason
*/
void heal(double amount, @NotNull org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason reason);
void heal(double amount, EntityRegainHealthEvent.RegainReason reason);

/**
* Gets the entity's absorption amount.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void damage(double amount, Entity source) {
this.getParent().damage(amount, source);
}

@Override
public void kill(DamageSource damageSource) {
this.getParent().kill(damageSource);
}

@Override
public double getHealth() {
return this.getParent().getHealth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.bukkit.entity.WitherSkull;
import org.bukkit.entity.memory.MemoryKey;
import org.bukkit.event.entity.EntityPotionEffectEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
Expand Down Expand Up @@ -145,12 +146,10 @@ public void setHealth(double health) {
}
}

// Paper start - entity heal API
@Override
public void heal(final double amount, final org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason reason) {
public void heal(final double amount, final EntityRegainHealthEvent.RegainReason reason) {
this.getHandle().heal((float) amount, reason);
}
// Paper end - entity heal API

@Override
public double getAbsorptionAmount() {
Expand Down Expand Up @@ -400,6 +399,15 @@ public void setBeeStingersInBody(int count) {
this.getHandle().setStingerCount(count);
}

@Override
public void kill(org.bukkit.damage.DamageSource damageSource) {
Preconditions.checkState(!this.getHandle().generation, "Cannot kill entity during world generation");
Preconditions.checkArgument(damageSource != null, "damageSource cannot be null");

this.getHandle().setHealth(0);
this.getHandle().die(((CraftDamageSource) damageSource).getHandle());
}

@Override
public void damage(double amount) {
this.damage(amount, this.getHandle().damageSources().generic());
Expand Down
Loading