Interface AttackEntityEvent
- All Superinterfaces:
Cancellable,Event
- All Known Implementing Classes:
AbstractAttackEntityEvent
Entity is being "attacked".
The uniqueness of this event is that all DamageSources 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,
the AttackEntityEvent can be summarized as so:
An ArrowEntity,
that was shot by a Skeleton,
with an enchanted ItemTypes.BOW ItemStack,
when the World Difficulty was set to
Difficulties.HARD,
will deal possibly "5" damage to any Entity it 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 particular DamageType, has no static finalized amount of damage
to deal to a particular Entity. To properly represent this,
a DamageSource has various "states" such as:
DamageSource.isAbsolute(), or DamageSource.isBypassingArmor().
Quite simply, the DamageSource will always be the "first" element
within a Cause that can be retrieved from Event.cause().
Next, any additional "aides" in attacking the Entity will
be included in order of "priority of relation" to "attacking" the entity. In
short, if another Entity is considered a "team member" to the
attacking Entity, the "team member" may be a second element within
the Cause. The same can be said if an ArrowEntity was shot from
a Dispenser that was triggered by a Player flipping 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 a Function that are applied to
the "base" damage. Given that Cause has a unique capability of
storing any and every Object willing to be passed into it, we
can easily represent these "sources" of "modifiers" in a Cause.
Now, knowing the "source" will not provide enough information, so a
DamageModifierType is provided with a DamageModifier to
paint the fullest picture of "explaining" the DamageModifier as to
why it is present, and why it is "modifying" the "base" damage. Finally,
we can associate a DamageModifier with a Function that is
passed the current "damage" into Function.apply(Object), being added
to the current "damage". After all DamageModifier Functions
are "applied", the overall "damage" is now the final damage to actually
throw a AttackEntityEvent.
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 for finalOutputDamage() 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 DamageModifier
provided in this event.
Coming forward, it is possible to further customize not only the "base"
damage, but override pre-existing DamageModifier Functions
by calling setOutputDamage(DamageModifier, DoubleUnaryOperator) at which point the
end result may be undefined. However, if a custom DamageModifier
that aims to alter the "final" damage based on some custom circumstances,
calling setOutputDamage(DamageModifier, DoubleUnaryOperator) on a new
DamageModifier instance, easily created from the
DamageModifier.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 Entity prior to any DamageModifiers associated with
the entity(). The AttackEntityEvent is used
to process the various DamageModifiers of which originate or are
associated with the targeted Entity.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ABSORPTIONand theCausecontains aPotionEffect.static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor "boots".static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor a "chestplate".static final StringFor use with aDamageModifierwhere the root cause is "created" by an object, usually theEntityorLivingentity.static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshot.static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.HARD_HATand theCausecontains anItemStackSnapshot, usually a helmet.static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor a "helmet".static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor "leggings".static final StringFor use with aDamageSourcewhere it is either aBlockDamageSourceorEntityDamageSourcesuch that it was last "notified" by the object represented in the cause.static final StringFor use with aDamageModifierwhere it's type is aDamageModifierTypes.HARD_HATand theCausecontains aPotionEffect.static final Stringor use with aDamageModifierwhere its type is aDamageModifierTypes.SHIELDand theCausecontains anItemStackSnapshot(in Vanilla, a shield).static final StringFor use with theDamageSourcethat is known as the "source" of the damage. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDamageModifierAfter(DamageModifier damageModifier, DoubleUnaryOperator function, Set<DamageModifierType> after) Adds the providedDamageModifierandFunctionto the list of modifiers, such that the modifier will appear in order after any current modifiers whose type are included in the providedSetofDamageModifierTypes.voidaddDamageModifierBefore(DamageModifier damageModifier, DoubleUnaryOperator function, Set<DamageModifierType> before) Adds the providedDamageModifierandFunctionto the list of modifiers, such that theSetcontainingDamageModifierTypes provided inbeforewill appear after the provided damage modifier.doubleGets the "base" damage to deal to the targetedEntity.entity()Gets theEntity.doubleGets the final damage that will be passed into the proceedingAttackEntityEvent.booleanisModifierApplicable(DamageModifier damageModifier) Checks whether the providedDamageModifieris applicable to the current availableDamageModifiers.floatGets the knock back modifier.doubleGets the original "raw" amount of damage to deal to the targetedEntity.Gets anImmutableMapof all originalDamageModifiers and their associated "modified" damage.doubleGets the original "final" amount of damage after all originalDamageModifiers are applied tooriginalDamage().doubleoriginalModifierDamage(DamageModifier damageModifier) Gets the original damage for the providedDamageModifier.doubleoutputDamage(DamageModifier damageModifier) Gets the damage for the providedDamageModifier.voidsetBaseOutputDamage(double baseDamage) Sets the "base" damage to deal to the targetedEntity.voidsetKnockbackModifier(float modifier) Sets the knock back modifier.voidsetOutputDamage(DamageModifier damageModifier, DoubleUnaryOperator function) Sets the providedFunctionto be used for the givenDamageModifier.Methods inherited from interface org.spongepowered.api.event.Cancellable
isCancelled, setCancelled
-
Field Details
-
SOURCE
For use with theDamageSourcethat is known as the "source" of the damage.- See Also:
-
HARD_HAT_ARMOR
For use with aDamageModifierwhere it's type is aDamageModifierTypes.HARD_HATand theCausecontains anItemStackSnapshot, usually a helmet.- See Also:
-
SHIELD
or use with aDamageModifierwhere its type is aDamageModifierTypes.SHIELDand theCausecontains anItemStackSnapshot(in Vanilla, a shield).- See Also:
-
GENERAL_ARMOR
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshot. Separate from hard hat but still considered as "armor" where it will absorb a certain amount of damage before dealing damage to the wearer.- See Also:
-
HELMET
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor a "helmet".- See Also:
-
CHESTPLATE
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor a "chestplate".- See Also:
-
LEGGINGS
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor "leggings".- See Also:
-
BOOTS
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ARMORand theCausecontains anItemStackSnapshotfor "boots".- See Also:
-
RESISTANCE
For use with aDamageModifierwhere it's type is aDamageModifierTypes.HARD_HATand theCausecontains aPotionEffect.- See Also:
-
ABSORPTION
For use with aDamageModifierwhere it's type is aDamageModifierTypes.ABSORPTIONand theCausecontains aPotionEffect.- See Also:
-
CREATOR
For use with aDamageModifierwhere the root cause is "created" by an object, usually theEntityorLivingentity.- See Also:
-
NOTIFIER
For use with aDamageSourcewhere it is either aBlockDamageSourceorEntityDamageSourcesuch that it was last "notified" by the object represented in the cause.Usually this is used where a
Playerinteracted with the nowDamageSourcesuch that they- See Also:
-
-
Method Details
-
entity
Entity entity()Gets theEntity.- Returns:
- The entity
-
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
Map<DamageModifier,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
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
List<DamageFunction> originalFunctions()- Returns:
- The list of damage modifier functions
-
baseOutputDamage
double baseOutputDamage()Gets the "base" damage to deal to the targetedEntity. The "base" damage is the original value before passing along the chain ofFunctions for all knownDamageModifiers.- Returns:
- The base damage
-
setBaseOutputDamage
void setBaseOutputDamage(double baseDamage) Sets the "base" damage to deal to the targetedEntity. The "base" damage is the original value before passing along the chain ofFunctions for all knownDamageModifiers.- Parameters:
baseDamage- The base damage
-
finalOutputDamage
double finalOutputDamage()Gets the final damage that will be passed into the proceedingAttackEntityEvent. The final damage is the end result of thebaseOutputDamage()being applied inFunction.apply(Object)available from all theTuples ofDamageModifiertoFunctioninoriginalFunctions().- Returns:
- The final damage to deal
-
isModifierApplicable
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
-
outputDamage
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
-
setOutputDamage
Sets the providedFunctionto be used for the givenDamageModifier. If theDamageModifieris already included inmodifiers(), theFunctionreplaces the existing function. If there is noTuplefor theDamageModifier, a new one is created and added to the end of the list ofFunctions to be applied to thebaseOutputDamage().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, DoubleUnaryOperator function, Set<DamageModifierType> before) Adds the providedDamageModifierandFunctionto 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
-
addDamageModifierAfter
void addDamageModifierAfter(DamageModifier damageModifier, DoubleUnaryOperator function, Set<DamageModifierType> after) Adds the providedDamageModifierandFunctionto 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
List<DamageFunction> modifiers()Gets a list of simpleTuples ofDamageModifierkeyed to their representativeFunctions. AllDamageModifiers are applicable to the entity based on theDamageSourceand any possible invulnerabilities due to theDamageSource.- Returns:
- A list of damage modifiers to functions
-
knockbackModifier
float knockbackModifier()Gets the knock back modifier. The modifier itself will apply to the momentum of the attacked entity.- Returns:
- The knock back modifier
-
setKnockbackModifier
void setKnockbackModifier(float modifier) Sets the knock back modifier. The modifier itself will apply to the momentum of the attacked entity.- Parameters:
modifier- The knock back modifier to set
-