package net.minecraft.world.damagesource; import net.minecraft.core.Holder; import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; import net.minecraft.tags.TagKey; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec3; import org.jspecify.annotations.Nullable; public class DamageSource { private final Holder type; @Nullable private final Entity causingEntity; @Nullable private final Entity directEntity; @Nullable private final Vec3 damageSourcePosition; public String toString() { return "DamageSource (" + this.type().msgId() + ")"; } public float getFoodExhaustion() { return this.type().exhaustion(); } public boolean isDirect() { return this.causingEntity == this.directEntity; } private DamageSource( final Holder type, @Nullable final Entity directEntity, @Nullable final Entity causingEntity, @Nullable final Vec3 damageSourcePosition ) { this.type = type; this.causingEntity = causingEntity; this.directEntity = directEntity; this.damageSourcePosition = damageSourcePosition; } public DamageSource(final Holder type, @Nullable final Entity directEntity, @Nullable final Entity causingEntity) { this(type, directEntity, causingEntity, null); } public DamageSource(final Holder type, final Vec3 damageSourcePosition) { this(type, null, null, damageSourcePosition); } public DamageSource(final Holder type, @Nullable final Entity causingEntity) { this(type, causingEntity, causingEntity); } public DamageSource(final Holder type) { this(type, null, null, null); } @Nullable public Entity getDirectEntity() { return this.directEntity; } @Nullable public Entity getEntity() { return this.causingEntity; } @Nullable public ItemStack getWeaponItem() { return this.directEntity != null ? this.directEntity.getWeaponItem() : null; } public Component getLocalizedDeathMessage(final LivingEntity victim) { String deathMsg = "death.attack." + this.type().msgId(); if (this.causingEntity == null && this.directEntity == null) { LivingEntity source = victim.getKillCredit(); String playerMsg = deathMsg + ".player"; return source != null ? Component.translatable(playerMsg, victim.getDisplayName(), source.getDisplayName()) : Component.translatable(deathMsg, victim.getDisplayName()); } else { Component name = this.causingEntity == null ? this.directEntity.getDisplayName() : this.causingEntity.getDisplayName(); ItemStack held = this.causingEntity instanceof LivingEntity livingEntity ? livingEntity.getMainHandItem() : ItemStack.EMPTY; return !held.isEmpty() && held.has(DataComponents.CUSTOM_NAME) ? Component.translatable(deathMsg + ".item", victim.getDisplayName(), name, held.getDisplayName()) : Component.translatable(deathMsg, victim.getDisplayName(), name); } } public String getMsgId() { return this.type().msgId(); } public boolean scalesWithDifficulty() { return switch (this.type().scaling()) { case NEVER -> false; case WHEN_CAUSED_BY_LIVING_NON_PLAYER -> this.causingEntity instanceof LivingEntity && !(this.causingEntity instanceof Player); case ALWAYS -> true; }; } public boolean isCreativePlayer() { return this.getEntity() instanceof Player player && player.getAbilities().instabuild; } @Nullable public Vec3 getSourcePosition() { if (this.damageSourcePosition != null) { return this.damageSourcePosition; } else { return this.directEntity != null ? this.directEntity.position() : null; } } @Nullable public Vec3 sourcePositionRaw() { return this.damageSourcePosition; } public boolean is(final TagKey tag) { return this.type.is(tag); } public boolean is(final ResourceKey typeKey) { return this.type.is(typeKey); } public DamageType type() { return this.type.value(); } public Holder typeHolder() { return this.type; } }