Class DamageContainer

java.lang.Object
net.neoforged.neoforge.common.damagesource.DamageContainer

public class DamageContainer extends Object
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

  1. Entity.hurt(net.minecraft.world.damagesource.DamageSource, float) is invoked on the recipient from the source of the attack.
  2. invalid reference
    Entity#isInvulnerableTo
    is invoked and fires EntityInvulnerabilityCheckEvent.
  3. After determining the entity is vulnerable, the DamageContainer in instantiated for the entity.
  4. LivingIncomingDamageEvent is fired.
  5. LivingShieldBlockEvent fires and the result determines if shield effects apply.
  6. LivingEntity.actuallyHurt(net.minecraft.server.level.ServerLevel, net.minecraft.world.damagesource.DamageSource, float) is called.
  7. armor, magic, and mob_effect reductions are captured in the DamageContainer.
  8. LivingDamageEvent.Pre is fired.
  9. absorption reductions are captured in the DamageContainer.
  10. if the damage is not zero, entity health is modified and recorded and LivingDamageEvent.Post is fired.
  • Field Details

    • reductionFunctions

      private final EnumMap<DamageContainer.Reduction,List<IReductionFunction>> reductionFunctions
    • originalDamage

      private final float originalDamage
    • source

      private final net.minecraft.world.damagesource.DamageSource source
    • newDamage

      private float newDamage
    • reductions

      private final EnumMap<DamageContainer.Reduction,Float> 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 into Entity.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

      public void addModifier(DamageContainer.Reduction type, IReductionFunction reductionFunction)
      Adds a callback modifier to the vanilla damage reductions. Each function will be performed in sequence on the vanilla value at the time the DamageContainer.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 to
      reductionFunction - 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, use EventPriority to order your function.
    • getBlockedDamage

      public float getBlockedDamage()
      Returns the damage blocked during the LivingShieldBlockEvent.
      Returns:
      the damage blocked during the LivingShieldBlockEvent
    • getShieldDamage

      public float getShieldDamage()
      Returns the durability applied to the applicable shield after LivingShieldBlockEvent 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

      public float getReduction(DamageContainer.Reduction type)
      This provides a post-reduction value for the reduction and modifiers. This will always return zero before LivingDamageEvent.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

      @Internal public void setBlockedDamage(LivingShieldBlockEvent event)
    • setReduction

      @Internal public void setReduction(DamageContainer.Reduction reduction, float amount)
    • modifyReduction

      private float modifyReduction(DamageContainer.Reduction type, float reduction)