Class DamageContainer
java.lang.Object
net.neoforged.neoforge.common.damagesource.DamageContainer
DamageContainer encapsulates aspects of the entity damage sequence so that
relevant context related to damage dealt is accessible throughout the entire
sequence.
Note: certain values will be defaults until the stage in the sequence when they are set.
The Damage Sequence
Entity.hurt(net.minecraft.world.damagesource.DamageSource, float)
is invoked on the recipient from the source of the attack.-
invalid reference
Entity#isInvulnerableTo
EntityInvulnerabilityCheckEvent
. - After determining the entity is vulnerable, the
DamageContainer
in instantiated for the entity. LivingIncomingDamageEvent
is fired.LivingShieldBlockEvent
fires and the result determines if shield effects apply.LivingEntity.actuallyHurt(net.minecraft.server.level.ServerLevel, net.minecraft.world.damagesource.DamageSource, float)
is called.- armor, magic, and mob_effect reductions are captured in the DamageContainer.
LivingDamageEvent.Pre
is fired.- absorption reductions are captured in the DamageContainer.
- if the damage is not zero, entity health is modified and recorded and
LivingDamageEvent.Post
is fired.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate float
private int
private float
private final float
private final EnumMap
<DamageContainer.Reduction, List<IReductionFunction>> private final EnumMap
<DamageContainer.Reduction, Float> private float
private final net.minecraft.world.damagesource.DamageSource
-
Constructor Summary
ConstructorsConstructorDescriptionDamageContainer
(net.minecraft.world.damagesource.DamageSource source, float originalDamage) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addModifier
(DamageContainer.Reduction type, IReductionFunction reductionFunction) Adds a callback modifier to the vanilla damage reductions.float
Returns the damage blocked during theLivingShieldBlockEvent
.float
Returns the current amount expected to be applied to the entity or used in subsequent damage calculations.float
Returns the value passed intoEntity.hurt(DamageSource, float)
before any modifications are made.int
Returns the number of ticks this entity will be invulnerable after damage is applied.float
This provides a post-reduction value for the reduction and modifiers.float
Returns the durability applied to the applicable shield afterLivingShieldBlockEvent
returned a successful block.net.minecraft.world.damagesource.DamageSource
Returns the damage source for this damage sequence.private float
modifyReduction
(DamageContainer.Reduction type, float reduction) void
void
setNewDamage
(float damage) This sets the current damage value for the entity at the stage of the damage sequence in which it is set.void
setPostAttackInvulnerabilityTicks
(int ticks) Explicitly sets the invulnerability ticks after the damage has been applied.void
setReduction
(DamageContainer.Reduction reduction, float amount)
-
Field Details
-
reductionFunctions
-
originalDamage
private final float originalDamage -
source
private final net.minecraft.world.damagesource.DamageSource source -
newDamage
private float newDamage -
reductions
-
blockedDamage
private float blockedDamage -
shieldDamage
private float shieldDamage -
invulnerabilityTicksAfterAttack
private int invulnerabilityTicksAfterAttack
-
-
Constructor Details
-
DamageContainer
public DamageContainer(net.minecraft.world.damagesource.DamageSource source, float originalDamage)
-
-
Method Details
-
getOriginalDamage
public float getOriginalDamage()Returns the value passed intoEntity.hurt(DamageSource, float)
before any modifications are made.- Returns:
- the value passed into
Entity.hurt(DamageSource, float)
before any modifications are made
-
getSource
public net.minecraft.world.damagesource.DamageSource getSource()Returns the damage source for this damage sequence.- Returns:
- the damage source for this damage sequence
-
setNewDamage
public void setNewDamage(float damage) This sets the current damage value for the entity at the stage of the damage sequence in which it is set. Subsequent steps in the damage sequence will use and modify this value accordingly. If this is called in the final step of the sequence, this value will be applied against the entity's health.- Parameters:
damage
- the amount to harm this entity at the end of the damage sequence
-
getNewDamage
public float getNewDamage()Returns the current amount expected to be applied to the entity or used in subsequent damage calculations.- Returns:
- the current amount expected to be applied to the entity or used in subsequent damage calculations
-
addModifier
Adds a callback modifier to the vanilla damage reductions. Each function will be performed in sequence on the vanilla value at the time theDamageContainer.Reduction
type is set by vanilla.- only the
EntityPreDamageEvent
happens early enough in the sequence for this method to have any effect. - if the vanilla reduction is not triggered, the reduction function will not execute.
- Parameters:
type
- The reduction type your function will apply toreductionFunction
- takes the current reduction from vanilla and any preceding functions and returns a new value for the reduction. These are always executed in insertion order. if sequence matters, useEventPriority
to order your function.
- only the
-
getBlockedDamage
public float getBlockedDamage()Returns the damage blocked during theLivingShieldBlockEvent
.- Returns:
- the damage blocked during the
LivingShieldBlockEvent
-
getShieldDamage
public float getShieldDamage()Returns the durability applied to the applicable shield afterLivingShieldBlockEvent
returned a successful block.- Returns:
- the durability applied to the applicable shield after
LivingShieldBlockEvent
returned a successful block
-
setPostAttackInvulnerabilityTicks
public void setPostAttackInvulnerabilityTicks(int ticks) Explicitly sets the invulnerability ticks after the damage has been applied.- Parameters:
ticks
- Ticks of invulnerability after this damage sequence
-
getPostAttackInvulnerabilityTicks
public int getPostAttackInvulnerabilityTicks()Returns the number of ticks this entity will be invulnerable after damage is applied.- Returns:
- the number of ticks this entity will be invulnerable after damage is applied
-
getReduction
This provides a post-reduction value for the reduction and modifiers. This will always return zero beforeLivingDamageEvent.Pre
and will consume all modifiers prior to the event.- Parameters:
type
- the specific source type of the damage reduction- Returns:
- The amount of damage reduced by armor after vanilla armor reductions and added modifiers
-
setBlockedDamage
-
setReduction
-
modifyReduction
-