Class FinalDamageEvent

java.lang.Object
org.bukkit.event.Event
studio.magemonkey.codex.api.damage.FinalDamageEvent
All Implemented Interfaces:
org.bukkit.event.Cancellable

public class FinalDamageEvent extends org.bukkit.event.Event implements org.bukkit.event.Cancellable
The final numbers of a hit, as a damage plugin has calculated them, just before it writes them to the Bukkit damage event.

A damage plugin (a provider, such as Divinity) fires this event from its own EntityDamageEvent handler, after it has worked out dodge, critical hits, blocking, defenses and the like, and before it sets the damage of the Bukkit event. Other plugins can then read and change the damage of the hit by type, without depending on the provider. A skill plugin such as Fabled, for example, runs its on-hit triggers and buffs here instead of changing the Bukkit event's damage itself.

Codex never fires this event. It only defines it, so that plugins which cannot see each other's classes can share it.

Damage by type

The damage is kept per damage type, keyed by the provider's type id (physical, fire and so on; what the ids are is up to the provider). The total is always the sum of the types: After the event, the provider deals getTotalDamage() (and may run its own after-hit effects, such as life steal, on the per-type amounts).

Cancelling

Cancelling this event cancels the hit: the provider cancels the Bukkit event and deals no damage. Codex only keeps the flag; acting on it is the provider's job.

Knowing whether the event will fire

A plugin that changes Bukkit damage itself when no provider is around needs to know whether this event will come for a hit, so that it does not apply its changes twice. Providers announce themselves with registerProvider(Plugin, Predicate); ask with willFire(EntityDamageEvent). See there.

Subclassing

A provider may fire this class as it is, or a subclass of its own that adds what only it knows. Listeners of FinalDamageEvent receive the subclass too, and listeners of the subclass keep working:
  • Do not declare getHandlerList() in a subclass. Bukkit registers a listener with the handler list of the nearest class, starting at the listened class and walking up, that declares a static getHandlerList() method, and it calls the event on the one list that getHandlers() returns. With no getHandlerList() of its own, every listener of the subclass lands in this class's list, and Bukkit hands each listener only the events that are instances of the class it listens to. getHandlers() is final for this reason.
  • A subclass that keeps the per-type damage in storage of its own passes an empty map to the constructor and overrides getDamages(), getDamage(String) and setDamage(String, double), all three. getTotalDamage() and setTotalDamage(double) are built on those three and stay correct without being overridden. A subclass may still override them (for speed, or to split the damage of a hit that has none in its own way), as long as the total stays the sum of the types.
  • isCancelled() and setCancelled(boolean) may be overridden in pairs, for example to share a flag the subclass already has.

This event is fired, read and changed on the server thread only. It uses nothing newer than the oldest Minecraft version Codex supports.

  • Nested Class Summary

    Nested classes/interfaces inherited from class org.bukkit.event.Event

    org.bukkit.event.Event.Result
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The type that setTotalDamage(double) puts the damage in when the hit has no damage types at all.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FinalDamageEvent(org.bukkit.entity.LivingEntity victim, org.bukkit.entity.LivingEntity attacker, org.bukkit.entity.Projectile projectile, org.bukkit.event.entity.EntityDamageEvent.DamageCause cause, org.bukkit.event.entity.EntityDamageEvent damageEvent, DamageRequest request, DamageOrigin origin, WeaponStatsMode weaponStatsMode, Map<String,Double> damages)
    Creates the event.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.bukkit.entity.LivingEntity
     
    org.bukkit.event.entity.EntityDamageEvent.DamageCause
     
    double
     
    org.bukkit.event.entity.EntityDamageEvent
    The Bukkit event the provider is handling.
    The final damage of the hit by type id, in the provider's order.
    static org.bukkit.event.HandlerList
    The handler list Bukkit registers listeners of this event, and of every subclass, with.
    final org.bukkit.event.HandlerList
    Final, so that every subclass shares this class's handler list.
     
    org.bukkit.entity.Projectile
     
     
    The amounts the dealer asked for, as opposed to the final amounts of getDamages().
    double
     
    org.bukkit.entity.LivingEntity
     
     
    static boolean
     
    boolean
     
    static void
    registerProvider(org.bukkit.plugin.Plugin provider)
    Announces a plugin that fires this event for every damage event it sees that is still going when its handler runs.
    static void
    registerProvider(org.bukkit.plugin.Plugin provider, Predicate<? super org.bukkit.event.entity.EntityDamageEvent> fires)
    Announces a plugin that fires this event.
    void
    setCancelled(boolean cancel)
    Cancels the hit, or lets it through again.
    void
    setDamage(String type, double amount)
    Sets the final damage of one type.
    void
    setTotalDamage(double total)
    Sets the final damage of the hit by scaling every type by the same factor, so that each keeps its share.
    static void
    unregisterProvider(org.bukkit.plugin.Plugin provider)
    Withdraws a provider.
    static boolean
    willFire(org.bukkit.event.entity.EntityDamageEvent event)
    Whether a provider will fire this event for a hit, should the hit not be cancelled before the provider's handler runs.

    Methods inherited from class org.bukkit.event.Event

    getEventName, isAsynchronous

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • FinalDamageEvent

      public FinalDamageEvent(@NotNull org.bukkit.entity.LivingEntity victim, @Nullable org.bukkit.entity.LivingEntity attacker, @Nullable org.bukkit.entity.Projectile projectile, @NotNull org.bukkit.event.entity.EntityDamageEvent.DamageCause cause, @NotNull org.bukkit.event.entity.EntityDamageEvent damageEvent, @Nullable DamageRequest request, @NotNull DamageOrigin origin, @NotNull WeaponStatsMode weaponStatsMode, @NotNull Map<String,Double> damages)
      Creates the event. Only a provider creates it, right before it calls it.
      Parameters:
      victim - the entity that is hit
      attacker - the entity the hit is attributed to: the shooter of a projectile, the caster of a skill. Null if there is none.
      projectile - the projectile that hit, or null if it was not a projectile
      cause - the cause of the hit. Usually that of the Bukkit event, or the one the DamageRequest asks for.
      damageEvent - the Bukkit event being handled
      request - the request that describes the hit (see DamageContexts.claim(EntityDamageEvent)), or null if the hit was not announced
      origin - what deals the damage: the request's origin, or the provider's best guess when there is no request
      weaponStatsMode - how the attacker's equipment took part: the request's mode, or WeaponStatsMode.ADD when there is no request
      damages - the final damage by type id, in the order the provider wants them listed. The map is copied. Amounts must be finite numbers and not negative. A subclass with storage of its own passes an empty map (see the class description).
  • Method Details

    • getVictim

      @NotNull public org.bukkit.entity.LivingEntity getVictim()
      Returns:
      the entity that is hit
    • getAttacker

      @Nullable public org.bukkit.entity.LivingEntity getAttacker()
      Returns:
      the entity the hit is attributed to (the shooter of a projectile, the caster of a skill), or null if there is none
    • getProjectile

      @Nullable public org.bukkit.entity.Projectile getProjectile()
      Returns:
      the projectile that hit, or null if the hit was not from a projectile
    • getCause

      @NotNull public org.bukkit.event.entity.EntityDamageEvent.DamageCause getCause()
      Returns:
      the cause of the hit
    • getDamageEvent

      @NotNull public org.bukkit.event.entity.EntityDamageEvent getDamageEvent()
      The Bukkit event the provider is handling. Read it, but leave its damage alone: the provider sets it from this event's total when this event is over. To stop the hit, cancel this event.
      Returns:
      the Bukkit damage event of the hit
    • getRequest

      @Nullable public DamageRequest getRequest()
      Returns:
      the request that describes the hit, or null if the hit was not announced through DamageContexts
    • getOrigin

      @NotNull public DamageOrigin getOrigin()
      Returns:
      what deals the damage: the request's origin, or the provider's best guess when there is no request
    • getWeaponStatsMode

      @NotNull public WeaponStatsMode getWeaponStatsMode()
      Returns:
      how the attacker's equipment took part in the hit: the request's mode, or WeaponStatsMode.ADD when there is no request
    • getRequestedAmounts

      @NotNull public Map<String,Double> getRequestedAmounts()
      The amounts the dealer asked for, as opposed to the final amounts of getDamages().
      Returns:
      the request's amounts by type, or an empty map if there is no request
    • getDamages

      @NotNull public Map<String,Double> getDamages()
      The final damage of the hit by type id, in the provider's order. Types with an amount of 0 may be listed.

      A subclass with storage of its own overrides this together with getDamage(String) and setDamage(String, double).

      Returns:
      the damage by type. The map cannot be changed; use setDamage(String, double).
    • getDamage

      public double getDamage(@NotNull String type)
      Parameters:
      type - a damage type id
      Returns:
      the final damage of that type, or 0 if the hit has none of it
    • setDamage

      public void setDamage(@NotNull String type, double amount)
      Sets the final damage of one type. The total changes by the difference. A type the hit did not have is added. Setting a type to 0 removes its damage but may keep it listed.

      Which ids a provider knows is up to it. A provider may ignore, or map, an id it does not know.

      Parameters:
      type - a damage type id
      amount - the amount, which must be a finite number and not negative
      Throws:
      IllegalArgumentException - if the amount is negative, infinite or not a number
    • getTotalDamage

      public double getTotalDamage()
      Returns:
      the final damage of the hit: the sum of getDamages()
    • setTotalDamage

      public void setTotalDamage(double total)
      Sets the final damage of the hit by scaling every type by the same factor, so that each keeps its share. Setting 0 sets every type to 0.

      A hit whose total is 0 has no shares to keep. Then the new total is split evenly between the listed types, or, if there are none, given to DEFAULT_TYPE.

      Parameters:
      total - the new total, which must be a finite number and not negative
      Throws:
      IllegalArgumentException - if the total is negative, infinite or not a number
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface org.bukkit.event.Cancellable
      Returns:
      whether the hit is cancelled. The provider then cancels the Bukkit event and deals no damage.
    • setCancelled

      public void setCancelled(boolean cancel)
      Cancels the hit, or lets it through again. A cancelled hit deals no damage: the provider cancels the Bukkit event and skips its after-hit effects.
      Specified by:
      setCancelled in interface org.bukkit.event.Cancellable
      Parameters:
      cancel - whether to cancel the hit
    • getHandlers

      @NotNull public final org.bukkit.event.HandlerList getHandlers()
      Final, so that every subclass shares this class's handler list. See the class description.
      Specified by:
      getHandlers in class org.bukkit.event.Event
    • getHandlerList

      @NotNull public static org.bukkit.event.HandlerList getHandlerList()
      The handler list Bukkit registers listeners of this event, and of every subclass, with.
      Returns:
      the handler list
    • registerProvider

      public static void registerProvider(@NotNull org.bukkit.plugin.Plugin provider, @NotNull Predicate<? super org.bukkit.event.entity.EntityDamageEvent> fires)
      Announces a plugin that fires this event. Call it from onEnable, and call unregisterProvider(Plugin) from onDisable. Registering again replaces the earlier predicate.

      The predicate answers, for a Bukkit damage event, whether the provider will fire this event for it, should the hit still be going (not cancelled) when the provider's handler runs. It is asked from other plugins' damage handlers, possibly before the provider's handler has run, so it must not change anything, must be cheap, and must not rely on what the provider works out during the hit. It should answer true for every hit the provider calculates; answering true for a hit the provider then leaves alone means the asking plugin skips it entirely.

      Use registerProvider(Plugin) if the provider fires the event for every hit.

      Parameters:
      provider - the plugin that fires the event
      fires - whether the provider fires the event for a given Bukkit damage event
    • registerProvider

      public static void registerProvider(@NotNull org.bukkit.plugin.Plugin provider)
      Announces a plugin that fires this event for every damage event it sees that is still going when its handler runs. Same as registerProvider(provider, event -> true).
      Parameters:
      provider - the plugin that fires the event
    • unregisterProvider

      public static void unregisterProvider(@NotNull org.bukkit.plugin.Plugin provider)
      Withdraws a provider. Doing so for a plugin that is not registered does nothing.
      Parameters:
      provider - the plugin that no longer fires the event
    • hasProvider

      public static boolean hasProvider()
      Returns:
      whether any enabled plugin has registered as a provider
    • willFire

      public static boolean willFire(@NotNull org.bukkit.event.entity.EntityDamageEvent event)
      Whether a provider will fire this event for a hit, should the hit not be cancelled before the provider's handler runs.

      This is what a plugin that otherwise changes the Bukkit event's damage itself asks, from its own damage handler, to decide between the two ways:

      @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
      public void onDamage(EntityDamageByEntityEvent event) {
          if (FinalDamageEvent.willFire(event)) return; // handled in onFinalDamage instead
          event.setDamage(applyBuffs(event.getDamage()));
      }
      
      @EventHandler(ignoreCancelled = true)
      public void onFinalDamage(FinalDamageEvent event) {
          event.setTotalDamage(applyBuffs(event.getTotalDamage()));
      }
      
      A provider that has been disabled without unregistering is not counted.
      Parameters:
      event - the Bukkit damage event of the hit
      Returns:
      whether an enabled provider says it fires this event for the hit