Class MagicDamageSource

java.lang.Object
net.minecraft.world.damagesource.DamageSource
com.binaris.wizardry.api.content.util.MagicDamageSource

public class MagicDamageSource extends net.minecraft.world.damagesource.DamageSource
Damage source factory for the mod's magic damage types. Including immunity saving and lookup.

This class extends DamageSource to provide convenience factory methods for creating magic damage sources (both direct and indirect). It also keeps a simple static mapping of entity classes to DamageType resource keys that represent immunities; this mapping is consulted by isEntityImmune(net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType>, net.minecraft.world.entity.Entity).

Typical usage:

  • Constructor Summary

    Constructors
    Constructor
    Description
    MagicDamageSource(net.minecraft.core.Holder<net.minecraft.world.damagesource.DamageType> damageTypeHolder, @Nullable net.minecraft.world.entity.Entity directEntity, @Nullable net.minecraft.world.entity.Entity causingEntity)
    Construct a new magic damage source wrapper.
  • Method Summary

    Modifier and Type
    Method
    Description
    static net.minecraft.world.damagesource.DamageSource
    causeDirectMagicDamage(net.minecraft.world.entity.Entity caster, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
    Create a direct magic damage DamageSource where caster is the direct source and there is no indirect/causing entity.
    static net.minecraft.world.damagesource.DamageSource
    causeIndirectMagicDamage(net.minecraft.world.entity.Entity magic, net.minecraft.world.entity.Entity caster, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
    Create an indirect magic damage DamageSource where magic is the direct entity (e.g.
    static boolean
    causeMagicDamage(net.minecraft.world.entity.Entity caster, net.minecraft.world.entity.Entity target, float damage, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
    A convenience method for applying magic damage to a target entity.
    static boolean
    isEntityImmune(net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type, net.minecraft.world.entity.Entity entity)
    Checks if the given entity is immune to the specified damage type.
    static void
    setEntityImmunities(Class<? extends net.minecraft.world.entity.Entity> entityType, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType>... immunities)
    Sets the immunities for the specified entity type.

    Methods inherited from class net.minecraft.world.damagesource.DamageSource

    getDirectEntity, getEntity, getFoodExhaustion, getLocalizedDeathMessage, getMsgId, getSourcePosition, is, is, isCreativePlayer, isIndirect, scalesWithDifficulty, sourcePositionRaw, toString, type, typeHolder

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MagicDamageSource

      public MagicDamageSource(net.minecraft.core.Holder<net.minecraft.world.damagesource.DamageType> damageTypeHolder, @Nullable @Nullable net.minecraft.world.entity.Entity directEntity, @Nullable @Nullable net.minecraft.world.entity.Entity causingEntity)
      Construct a new magic damage source wrapper.
      Parameters:
      damageTypeHolder - Holder for the DamageType describing this damage.
      directEntity - The direct entity that dealt the damage (could be a projectile or other entity), or null.
      causingEntity - The indirect/owner entity that ultimately caused the damage (could be null).
  • Method Details

    • setEntityImmunities

      @SafeVarargs public static void setEntityImmunities(Class<? extends net.minecraft.world.entity.Entity> entityType, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType>... immunities)
      Sets the immunities for the specified entity type.
      Parameters:
      entityType - The class of the entity type.
      immunities - The damage types to which the entity type is immune.
    • isEntityImmune

      public static boolean isEntityImmune(net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type, net.minecraft.world.entity.Entity entity)
      Checks if the given entity is immune to the specified damage type.
      Parameters:
      type - The damage type to check immunity against (a resource key from EBDamageSources).
      entity - The entity to check for immunity.
      Returns:
      true if the entity is immune to the specified damage type, false otherwise.
    • causeMagicDamage

      public static boolean causeMagicDamage(net.minecraft.world.entity.Entity caster, net.minecraft.world.entity.Entity target, float damage, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
      A convenience method for applying magic damage to a target entity.

      If the caster entity has an owner (either implements OwnableEntity or TraceableEntity and returns a non-null owner), the damage is treated as indirect: the magic entity is the direct source and the owner is the indirect/causing entity. Otherwise damage is treated as direct and the caster is used as the direct source with no indirect entity.

      Parameters:
      caster - the entity applying the damage (normally a projectile or summoned entity)
      target - the entity taking the damage
      damage - the amount of damage to apply
      type - the type of magic damage to apply (a ResourceKey from DamageType)
      Returns:
      whether the target entity was damaged (result of Entity.hurt(DamageSource, float))
    • causeDirectMagicDamage

      public static net.minecraft.world.damagesource.DamageSource causeDirectMagicDamage(net.minecraft.world.entity.Entity caster, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
      Create a direct magic damage DamageSource where caster is the direct source and there is no indirect/causing entity.
      Parameters:
      caster - The entity directly dealing the damage (often the shooter or caster).
      type - The damage type resource key (from EBDamageSources).
      Returns:
      A configured DamageSource instance representing direct magic damage.
    • causeIndirectMagicDamage

      public static net.minecraft.world.damagesource.DamageSource causeIndirectMagicDamage(net.minecraft.world.entity.Entity magic, net.minecraft.world.entity.Entity caster, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type)
      Create an indirect magic damage DamageSource where magic is the direct entity (e.g. a projectile) and caster is the indirect/owner entity who should be credited.
      Parameters:
      magic - The direct magic entity (projectile/minion).
      caster - The owner/indirect entity to credit for the damage.
      type - The damage type resource key.
      Returns:
      A configured DamageSource instance representing indirect magic damage.