SPIGOT-7300, #829: Add new DamageSource API providing enhanced information about entity damage

By: Doc <nachito94@msn.com>
Also-by: 2008Choco <hawkeboyz2@hotmail.com>
This commit is contained in:
Bukkit/Spigot
2024-02-11 09:54:21 +11:00
parent e46e33f5e2
commit f9381f1dc4
13 changed files with 467 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ package org.bukkit.event.entity;
import com.google.common.base.Function;
import java.util.Map;
import org.bukkit.block.Block;
import org.bukkit.damage.DamageSource;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -13,13 +14,13 @@ import org.jetbrains.annotations.Nullable;
public class EntityDamageByBlockEvent extends EntityDamageEvent {
private final Block damager;
public EntityDamageByBlockEvent(@Nullable final Block damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, final double damage) {
super(damagee, cause, damage);
public EntityDamageByBlockEvent(@Nullable final Block damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, final double damage) {
super(damagee, cause, damageSource, damage);
this.damager = damager;
}
public EntityDamageByBlockEvent(@Nullable final Block damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
super(damagee, cause, modifiers, modifierFunctions);
public EntityDamageByBlockEvent(@Nullable final Block damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
super(damagee, cause, damageSource, modifiers, modifierFunctions);
this.damager = damager;
}

View File

@@ -2,6 +2,7 @@ package org.bukkit.event.entity;
import com.google.common.base.Function;
import java.util.Map;
import org.bukkit.damage.DamageSource;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
@@ -11,13 +12,13 @@ import org.jetbrains.annotations.NotNull;
public class EntityDamageByEntityEvent extends EntityDamageEvent {
private final Entity damager;
public EntityDamageByEntityEvent(@NotNull final Entity damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, final double damage) {
super(damagee, cause, damage);
public EntityDamageByEntityEvent(@NotNull final Entity damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, final double damage) {
super(damagee, cause, damageSource, damage);
this.damager = damager;
}
public EntityDamageByEntityEvent(@NotNull final Entity damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
super(damagee, cause, modifiers, modifierFunctions);
public EntityDamageByEntityEvent(@NotNull final Entity damager, @NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
super(damagee, cause, damageSource, modifiers, modifierFunctions);
this.damager = damager;
}

View File

@@ -9,6 +9,7 @@ import java.util.Map;
import java.util.Objects;
import org.bukkit.Material;
import org.bukkit.WorldBorder;
import org.bukkit.damage.DamageSource;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
@@ -27,12 +28,13 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
private final Map<DamageModifier, Double> originals;
private boolean cancelled;
private final DamageCause cause;
private final DamageSource damageSource;
public EntityDamageEvent(@NotNull final Entity damagee, @NotNull final DamageCause cause, final double damage) {
this(damagee, cause, new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, damage)), new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, ZERO)));
public EntityDamageEvent(@NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, final double damage) {
this(damagee, cause, damageSource, new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, damage)), new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, ZERO)));
}
public EntityDamageEvent(@NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
public EntityDamageEvent(@NotNull final Entity damagee, @NotNull final DamageCause cause, @NotNull final DamageSource damageSource, @NotNull final Map<DamageModifier, Double> modifiers, @NotNull final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
super(damagee);
Preconditions.checkArgument(modifiers.containsKey(DamageModifier.BASE), "BASE DamageModifier missing");
Preconditions.checkArgument(!modifiers.containsKey(null), "Cannot have null DamageModifier");
@@ -43,6 +45,7 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
this.cause = cause;
this.modifiers = modifiers;
this.modifierFunctions = modifierFunctions;
this.damageSource = damageSource;
}
@Override
@@ -64,14 +67,9 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
* @throws IllegalArgumentException if type is null
*/
public double getOriginalDamage(@NotNull DamageModifier type) throws IllegalArgumentException {
Preconditions.checkArgument(type != null, "Cannot have null DamageModifier");
final Double damage = originals.get(type);
if (damage != null) {
return damage;
}
if (type == null) {
throw new IllegalArgumentException("Cannot have null DamageModifier");
}
return 0;
return (damage != null) ? damage : 0;
}
/**
@@ -86,8 +84,9 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
* @see #getFinalDamage()
*/
public void setDamage(@NotNull DamageModifier type, double damage) throws IllegalArgumentException, UnsupportedOperationException {
Preconditions.checkArgument(type != null, "Cannot have null DamageModifier");
if (!modifiers.containsKey(type)) {
throw type == null ? new IllegalArgumentException("Cannot have null DamageModifier") : new UnsupportedOperationException(type + " is not applicable to " + getEntity());
throw new UnsupportedOperationException(type + " is not applicable to " + getEntity());
}
modifiers.put(type, damage);
}
@@ -185,14 +184,32 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
/**
* Gets the cause of the damage.
* <p>
* While a DamageCause may indicate a specific Bukkit-assigned cause of damage,
* {@link #getDamageSource()} may expose additional types of damage such as custom
* damage types provided by data packs, as well as any direct or indirect entities,
* locations, or other contributing factors to the damage being inflicted. The
* alternative is generally preferred, but DamageCauses provided to this event
* should largely encompass most common use cases for developers if a simple cause
* is required.
*
* @return A DamageCause value detailing the cause of the damage.
* @return a DamageCause value detailing the cause of the damage.
*/
@NotNull
public DamageCause getCause() {
return cause;
}
/**
* Get the source of damage.
*
* @return a DamageSource detailing the source of the damage.
*/
@NotNull
public DamageSource getDamageSource() {
return damageSource;
}
@NotNull
@Override
public HandlerList getHandlers() {
@@ -210,7 +227,7 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
* @deprecated This API is responsible for a large number of implementation
* problems and is in general unsustainable to maintain. It is likely to be
* removed very soon in a subsequent release. Please see
* https://www.spigotmc.org/threads/194446/ for more information.
* <a href="https://www.spigotmc.org/threads/194446/">this thread</a> for more information.
*/
@Deprecated
public enum DamageModifier {