Class FinalDamageEvent
- All Implemented Interfaces:
org.bukkit.event.Cancellable
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:
setDamage(String, double)changes one type, and with it the total;setTotalDamage(double)scales every type by the same factor, so that the types keep their share of the hit.
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 withregisterProvider(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 ofFinalDamageEvent 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 staticgetHandlerList()method, and it calls the event on the one list thatgetHandlers()returns. With nogetHandlerList()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)andsetDamage(String, double), all three.getTotalDamage()andsetTotalDamage(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()andsetCancelled(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
FieldsModifier and TypeFieldDescriptionstatic final StringThe type thatsetTotalDamage(double)puts the damage in when the hit has no damage types at all. -
Constructor Summary
ConstructorsConstructorDescriptionFinalDamageEvent(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 TypeMethodDescriptionorg.bukkit.entity.LivingEntityorg.bukkit.event.entity.EntityDamageEvent.DamageCausegetCause()doubleorg.bukkit.event.entity.EntityDamageEventThe Bukkit event the provider is handling.The final damage of the hit by type id, in the provider's order.static org.bukkit.event.HandlerListThe handler list Bukkit registers listeners of this event, and of every subclass, with.final org.bukkit.event.HandlerListFinal, so that every subclass shares this class's handler list.org.bukkit.entity.ProjectileThe amounts the dealer asked for, as opposed to the final amounts ofgetDamages().doubleorg.bukkit.entity.LivingEntitystatic booleanbooleanstatic voidregisterProvider(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 voidregisterProvider(org.bukkit.plugin.Plugin provider, Predicate<? super org.bukkit.event.entity.EntityDamageEvent> fires) Announces a plugin that fires this event.voidsetCancelled(boolean cancel) Cancels the hit, or lets it through again.voidSets the final damage of one type.voidsetTotalDamage(double total) Sets the final damage of the hit by scaling every type by the same factor, so that each keeps its share.static voidunregisterProvider(org.bukkit.plugin.Plugin provider) Withdraws a provider.static booleanwillFire(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
-
Field Details
-
DEFAULT_TYPE
The type thatsetTotalDamage(double)puts the damage in when the hit has no damage types at all.- See Also:
-
-
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 hitattacker- 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 projectilecause- the cause of the hit. Usually that of the Bukkit event, or the one theDamageRequestasks for.damageEvent- the Bukkit event being handledrequest- the request that describes the hit (seeDamageContexts.claim(EntityDamageEvent)), or null if the hit was not announcedorigin- what deals the damage: the request's origin, or the provider's best guess when there is no requestweaponStatsMode- how the attacker's equipment took part: the request's mode, orWeaponStatsMode.ADDwhen there is no requestdamages- 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
- Returns:
- the request that describes the hit, or null if the hit was not announced through
DamageContexts
-
getOrigin
- Returns:
- what deals the damage: the request's origin, or the provider's best guess when there is no request
-
getWeaponStatsMode
- Returns:
- how the attacker's equipment took part in the hit: the request's mode, or
WeaponStatsMode.ADDwhen there is no request
-
getRequestedAmounts
The amounts the dealer asked for, as opposed to the final amounts ofgetDamages().- Returns:
- the request's amounts by type, or an empty map if there is no request
-
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)andsetDamage(String, double).- Returns:
- the damage by type. The map cannot be changed; use
setDamage(String, double).
-
getDamage
- Parameters:
type- a damage type id- Returns:
- the final damage of that type, or 0 if the hit has none of it
-
setDamage
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 idamount- 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:
isCancelledin interfaceorg.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:
setCancelledin interfaceorg.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:
getHandlersin classorg.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 fromonEnable, and callunregisterProvider(Plugin)fromonDisable. 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 eventfires- 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 asregisterProvider(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:
A provider that has been disabled without unregistering is not counted.@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())); }- Parameters:
event- the Bukkit damage event of the hit- Returns:
- whether an enabled provider says it fires this event for the hit
-