Class MagicDamageSource
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:
causeMagicDamage(Entity, Entity, float, ResourceKey)- applies damage using an appropriate direct/indirect source and making all the needed checks for you.causeDirectMagicDamage(Entity, ResourceKey)- build a direct damage source.causeIndirectMagicDamage(Entity, Entity, ResourceKey)- build an indirect damage source (projectile caused by an owner).
-
Constructor Summary
ConstructorsConstructorDescriptionMagicDamageSource(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 TypeMethodDescriptionstatic net.minecraft.world.damagesource.DamageSourcecauseDirectMagicDamage(net.minecraft.world.entity.Entity caster, net.minecraft.resources.ResourceKey<net.minecraft.world.damagesource.DamageType> type) Create a direct magic damageDamageSourcewherecasteris the direct source and there is no indirect/causing entity.static net.minecraft.world.damagesource.DamageSourcecauseIndirectMagicDamage(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 damageDamageSourcewheremagicis the direct entity (e.g.static booleancauseMagicDamage(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 booleanisEntityImmune(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 voidsetEntityImmunities(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
-
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 theDamageTypedescribing this damage.directEntity- The direct entity that dealt the damage (could be a projectile or other entity), ornull.causingEntity- The indirect/owner entity that ultimately caused the damage (could benull).
-
-
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 fromEBDamageSources).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
OwnableEntityorTraceableEntityand 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 damagedamage- the amount of damage to applytype- the type of magic damage to apply (aResourceKeyfromDamageType)- 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 damageDamageSourcewherecasteris 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 (fromEBDamageSources).- Returns:
- A configured
DamageSourceinstance 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 damageDamageSourcewheremagicis the direct entity (e.g. a projectile) andcasteris 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
DamageSourceinstance representing indirect magic damage.
-