DamageSource .java

net.minecraft.world.damagesource.DamageSource (class, 132 lines): members: getLocalizedDeathMessage, damageSourcePosition, scalesWithDifficulty, getMsgId.

net.minecraft.world.damagesourceclass132 linescommon4.1 KB

API Surface (11)

String toString()
float getFoodExhaustion()
boolean isDirect()
Component getLocalizedDeathMessage(final LivingEntity victim)
String getMsgId()
boolean scalesWithDifficulty()
boolean isCreativePlayer()
boolean is(final TagKey<DamageType> tag)
boolean is(final ResourceKey<DamageType> typeKey)
DamageType type()
Holder<DamageType> typeHolder()

Fields & Constants (4)

type
causingEntity
directEntity
damageSourcePosition

String literals (4)

"DamageSource ("
"death.attack."
".player"
".item"

Full Source

DamageSource.java132 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.damagesource;
2
3
import net.minecraft.core.Holder;
4
import net.minecraft.core.component.DataComponents;
5
import net.minecraft.network.chat.Component;
6
import net.minecraft.resources.ResourceKey;
7
import net.minecraft.tags.TagKey;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.item.ItemStack;
12
import net.minecraft.world.phys.Vec3;
13
import org.jspecify.annotations.Nullable;
14
15
public class DamageSource {
16
	private final Holder<DamageType> type;
17
	private final @Nullable Entity causingEntity;
18
	private final @Nullable Entity directEntity;
19
	private final @Nullable Vec3 damageSourcePosition;
20
21
	@Override
22
	public String toString() {
23
		return "DamageSource (" + this.type().msgId() + ")";
24
	}
25
26
	public float getFoodExhaustion() {
27
		return this.type().exhaustion();
28
	}
29
30
	public boolean isDirect() {
31
		return this.causingEntity == this.directEntity;
32
	}
33
34
	private DamageSource(
35
		final Holder<DamageType> type, final @Nullable Entity directEntity, final @Nullable Entity causingEntity, final @Nullable Vec3 damageSourcePosition
36
	) {
37
		this.type = type;
38
		this.causingEntity = causingEntity;
39
		this.directEntity = directEntity;
40
		this.damageSourcePosition = damageSourcePosition;
41
	}
42
43
	public DamageSource(final Holder<DamageType> type, final @Nullable Entity directEntity, final @Nullable Entity causingEntity) {
44
		this(type, directEntity, causingEntity, null);
45
	}
46
47
	public DamageSource(final Holder<DamageType> type, final Vec3 damageSourcePosition) {
48
		this(type, null, null, damageSourcePosition);
49
	}
50
51
	public DamageSource(final Holder<DamageType> type, final @Nullable Entity causingEntity) {
52
		this(type, causingEntity, causingEntity);
53
	}
54
55
	public DamageSource(final Holder<DamageType> type) {
56
		this(type, null, null, null);
57
	}
58
59
	public @Nullable Entity getDirectEntity() {
60
		return this.directEntity;
61
	}
62
63
	public @Nullable Entity getEntity() {
64
		return this.causingEntity;
65
	}
66
67
	public @Nullable ItemStack getWeaponItem() {
68
		return this.directEntity != null ? this.directEntity.getWeaponItem() : null;
69
	}
70
71
	public Component getLocalizedDeathMessage(final LivingEntity victim) {
72
		String deathMsg = "death.attack." + this.type().msgId();
73
		if (this.causingEntity == null && this.directEntity == null) {
74
			LivingEntity source = victim.getKillCredit();
75
			String playerMsg = deathMsg + ".player";
76
			return source != null
77
				? Component.translatable(playerMsg, victim.getDisplayName(), source.getDisplayName())
78
				: Component.translatable(deathMsg, victim.getDisplayName());
79
		} else {
80
			Component name = this.causingEntity == null ? this.directEntity.getDisplayName() : this.causingEntity.getDisplayName();
81
			ItemStack held = this.causingEntity instanceof LivingEntity livingEntity ? livingEntity.getMainHandItem() : ItemStack.EMPTY;
82
			return !held.isEmpty() && held.has(DataComponents.CUSTOM_NAME)
83
				? Component.translatable(deathMsg + ".item", victim.getDisplayName(), name, held.getDisplayName())
84
				: Component.translatable(deathMsg, victim.getDisplayName(), name);
85
		}
86
	}
87
88
	public String getMsgId() {
89
		return this.type().msgId();
90
	}
91
92
	public boolean scalesWithDifficulty() {
93
		return switch (this.type().scaling()) {
94
			case NEVER -> false;
95
			case WHEN_CAUSED_BY_LIVING_NON_PLAYER -> this.causingEntity instanceof LivingEntity && !(this.causingEntity instanceof Player);
96
			case ALWAYS -> true;
97
		};
98
	}
99
100
	public boolean isCreativePlayer() {
101
		return this.getEntity() instanceof Player player && player.getAbilities().instabuild;
102
	}
103
104
	public @Nullable Vec3 getSourcePosition() {
105
		if (this.damageSourcePosition != null) {
106
			return this.damageSourcePosition;
107
		} else {
108
			return this.directEntity != null ? this.directEntity.position() : null;
109
		}
110
	}
111
112
	public @Nullable Vec3 sourcePositionRaw() {
113
		return this.damageSourcePosition;
114
	}
115
116
	public boolean is(final TagKey<DamageType> tag) {
117
		return this.type.is(tag);
118
	}
119
120
	public boolean is(final ResourceKey<DamageType> typeKey) {
121
		return this.type.is(typeKey);
122
	}
123
124
	public DamageType type() {
125
		return this.type.value();
126
	}
127
128
	public Holder<DamageType> typeHolder() {
129
		return this.type;
130
	}
131
}
132

Decompiled Minecraft 26.3 source of DamageSource (net/minecraft/world/damagesource/DamageSource.java) with Mojang official mappings, stored verbatim on the site.