Interface DamageEntityEvent
-
- All Superinterfaces:
Cancellable,Event
- All Known Implementing Classes:
AbstractDamageEntityEvent
public interface DamageEntityEvent extends Event, Cancellable
Represents the base event for when anEntityis being "attacked". The uniqueness of this event is that allDamageSources can deal varying amounts of damage with varying modifiers based on various reasons. Due to this ambiguous variety of information that is possible to provide, theDamageEntityEventcan be summarized as so:An
ArrowEntity, that was shot by aSkeleton, with an enchantedItemTypes.BOWItemStack, when theWorldDifficultywas set toDifficulties.HARD, will deal possibly "5" damage to anyEntityit hits.The issue with representing this type of "logic flow" is that a particular amount of damage from a
DamageSource, even if specified to a particularDamageType, has no static finalized amount of damage to deal to a particularEntity. To properly represent this, aDamageSourcehas various "states" such as:DamageSource.isAbsolute(), orDamageSource.isBypassingArmor(). Quite simply, theDamageSourcewill always be the "first" element within aCausethat can be retrieved fromEvent.cause().Next, any additional "aides" in attacking the
Entitywill be included in order of "priority of relation" to "attacking" the entity. In short, if anotherEntityis considered a "team member" to the attackingEntity, the "team member" may be a second element within theCause. The same can be said if anArrowEntitywas shot from aDispenserthat was triggered by aPlayerflipping a switch.Continuing with the notion of "modifiers" to damage, the "base" damage is modified or added onto after various unknown methods are called or processed on the damage. Optimally, these modifiers can be traced to a particular object, be it an
ItemStack,Difficulty, or simply an an attribute. The interesting part is that these "modifiers" do not just define a static value to add to the "base" damage, they are usually a loose form of aDamageFunctionthat are applied to the "base" damage. Given thatCausehas a unique capability of storing any and everyObjectwilling to be passed into it, we can easily represent these "sources" of "modifiers" in aCause. Now, knowning the "source" will not provide enough information, so aDamageModifierTypeis provided with aDamageModifierto paint the fullest picture of "explaining" theDamageModifieras to why it is present, and why it is "modifying" the "base" damage. Finally, we can associate aDamageModifierwith aDamageFunctionthat is passed the current "damage" intoDoubleUnaryOperator.applyAsDouble(double), being added to the current "damage". After allDamageModifierDamageFunctions are "applied", the overall "damage" is now the final damage to actually throw aDamageEntityEvent.Note that due to the mechanics of the game,
DamageModifiers are always ordered in the order of which they apply their modifier onto the "base" damage. The implementation forfinalDamage()can be exemplified like so:double damage = this.baseDamage;<br /> for (Map.Entry<DamageModifier, Function<? super Double, Double>> entry : this.modifierFunctions.entrySet()) { damage += checkNotNull(entry.getValue().apply(damage)); } return damage;After which, the "final" damage is simply the summation of the "base" damage and all "modified damage" for each
DamageModifierprovided in this event.Coming forward, it is possible to further customize not only the "base" damage, but override pre-existing
DamageModifierDamageFunctions by callingsetDamage(DamageModifier, DoubleUnaryOperator)at which point the end result may be undefined. However, if a customDamageModifierthat aims to alter the "final" damage based on some custom circumstances, callingsetDamage(DamageModifier, DoubleUnaryOperator)on a newDamageModifierinstance, easily created from theDamageModifier.Builder, the provided pairing will be added at the "end" of the list for "modifying" the "base" damage.Note that this event is intended for processing incoming damage to an
Entityprior to anyDamageModifiers associated with theentity(). TheDamageEntityEventis used to process the variousDamageModifiers of which originate or are associated with the targetedEntity.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddDamageModifierBefore(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function, java.util.Set<DamageModifierType> before)Adds the providedDamageModifierandDoubleUnaryOperatorto the list of modifiers, such that theSetcontainingDamageModifierTypes provided inbeforewill appear after the provided damage modifier.voidaddModifierAfter(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function, java.util.Set<DamageModifierType> after)Adds the providedDamageModifierandDoubleUnaryOperatorto the list of modifiers, such that the modifier will appear in order after any current modifiers whose type are included in the providedSetofDamageModifierTypes.doublebaseDamage()Gets the "base" damage to deal to the targetedEntity.doubledamage(DamageModifier damageModifier)Gets the damage for the providedDamageModifier.Entityentity()Gets theEntity.doublefinalDamage()Gets the final damage that will be passed into the proceedingDamageEntityEvent.booleanisModifierApplicable(DamageModifier damageModifier)Checks whether the providedDamageModifieris applicable to the current availableDamageModifiers.java.util.List<DamageFunction>modifiers()doubleoriginalDamage()Gets the original "raw" amount of damage to deal to the targetedEntity.java.util.Map<DamageModifier,java.lang.Double>originalDamages()Gets anImmutableMapof all originalDamageModifiers and their associated "modified" damage.doubleoriginalFinalDamage()Gets the original "final" amount of damage after all originalDamageModifiers are applied tooriginalDamage().java.util.List<DamageFunction>originalFunctions()Gets the originalListofDamageModifiertoDamageFunctionthat was originally passed into the event.doubleoriginalModifierDamage(DamageModifier damageModifier)Gets the original damage for the providedDamageModifier.voidsetBaseDamage(double baseDamage)Sets the "base" damage to deal to the targetedEntity.voidsetDamage(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function)Sets the providedDamageFunctionto be used for the givenDamageModifier.booleanwillCauseDeath()Returns whether or not this event will cause the entity to die if the event is not cancelled.-
Methods inherited from interface org.spongepowered.api.event.Cancellable
isCancelled, setCancelled
-
-
-
-
Method Detail
-
originalDamage
double originalDamage()
Gets the original "raw" amount of damage to deal to the targetedEntity.- Returns:
- The original "raw" damage
-
originalFinalDamage
double originalFinalDamage()
Gets the original "final" amount of damage after all originalDamageModifiers are applied tooriginalDamage(). The "final" damage is considered the amount of health being lost by theEntity, if health is tracked.- Returns:
- The final amount of damage to originally deal
-
originalDamages
java.util.Map<DamageModifier,java.lang.Double> originalDamages()
Gets anImmutableMapof all originalDamageModifiers and their associated "modified" damage. Note that ordering is not retained.- Returns:
- An immutable map of the original modified damages
-
originalModifierDamage
double originalModifierDamage(DamageModifier damageModifier)
Gets the original damage for the providedDamageModifier. If the providedDamageModifierwas not included inoriginalDamages(), anIllegalArgumentExceptionis thrown.- Parameters:
damageModifier- The original damage modifier- Returns:
- The original damage change
-
originalFunctions
java.util.List<DamageFunction> originalFunctions()
Gets the originalListofDamageModifiertoDamageFunctionthat was originally passed into the event.- Returns:
- The list of damage modifier functions
-
baseDamage
double baseDamage()
Gets the "base" damage to deal to the targetedEntity. The "base" damage is the original value before passing along the chain ofDamageFunctions for all knownDamageModifiers.- Returns:
- The base damage
-
setBaseDamage
void setBaseDamage(double baseDamage)
Sets the "base" damage to deal to the targetedEntity. The "base" damage is the original value before passing along the chain ofDamageFunctions for all knownDamageModifiers.- Parameters:
baseDamage- The base damage
-
finalDamage
double finalDamage()
Gets the final damage that will be passed into the proceedingDamageEntityEvent. The final damage is the end result of thebaseDamage()being applied inDoubleUnaryOperator.applyAsDouble(double)available from all theTuples ofDamageModifiertoDamageFunctioninoriginalFunctions().- Returns:
- The final damage to deal
-
isModifierApplicable
boolean isModifierApplicable(DamageModifier damageModifier)
Checks whether the providedDamageModifieris applicable to the current availableDamageModifiers.- Parameters:
damageModifier- The damage modifier to check- Returns:
- True if the damage modifier is relevant to this event
-
damage
double damage(DamageModifier damageModifier)
Gets the damage for the providedDamageModifier. Providing thatisModifierApplicable(DamageModifier)returnstrue, the cached "damage" for theDamageModifieris returned.- Parameters:
damageModifier- The damage modifier to get the damage for- Returns:
- The modifier
-
setDamage
void setDamage(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function)
Sets the providedDamageFunctionto be used for the givenDamageModifier. If theDamageModifieris already included inmodifiers(), theDoubleUnaryOperatorreplaces the existing function. If there is noTuplefor theDamageModifier, a new one is created and added to the end of the list ofDoubleUnaryOperators to be applied to thebaseDamage().If needing to create a custom
DamageModifieris required, usage of theDamageModifier.Builderis recommended.- Parameters:
damageModifier- The damage modifierfunction- The function to map to the modifier
-
addDamageModifierBefore
void addDamageModifierBefore(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function, java.util.Set<DamageModifierType> before)
Adds the providedDamageModifierandDoubleUnaryOperatorto the list of modifiers, such that theSetcontainingDamageModifierTypes provided inbeforewill appear after the provided damage modifier.- Parameters:
damageModifier- The damage modifier to addfunction- The associated functionbefore- The set containing the modifier types to come after the provided modifier
-
addModifierAfter
void addModifierAfter(DamageModifier damageModifier, java.util.function.DoubleUnaryOperator function, java.util.Set<DamageModifierType> after)
Adds the providedDamageModifierandDoubleUnaryOperatorto the list of modifiers, such that the modifier will appear in order after any current modifiers whose type are included in the providedSetofDamageModifierTypes.- Parameters:
damageModifier- The damage modifier to addfunction- The associated functionafter- The set of modifier types to come before the new modifier
-
modifiers
java.util.List<DamageFunction> modifiers()
Gets a list of simpleTuples ofDamageModifierkeyed to their representativeDamageFunctions. AllDamageModifiers are applicable to the entity based on theDamageSourceand any possible invulnerabilities due to theDamageSource.- Returns:
- A list of damage modifiers to functions
-
willCauseDeath
boolean willCauseDeath()
Returns whether or not this event will cause the entity to die if the event is not cancelled. Only supported for living entities, returns false ifentity()is not a living entity.- Returns:
- Whether the entity will die
-
-