HurtBySensor .java

net.minecraft.world.entity.ai.sensing.HurtBySensor (class): members: doTick, requires. imports: ImmutableSet, Brain, DamageSource, MemoryModuleType.

net.minecraft.world.entity.ai.sensingclass37 linescommonextends Sensor<LivingEntity>1.3 KB

API Surface (2)

Set<MemoryModuleType<?>> requires()
void doTick(final ServerLevel level, final LivingEntity body)

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

HurtBySensor.java37 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.sensing;
2
3
import com.google.common.collect.ImmutableSet;
4
import java.util.Set;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.world.damagesource.DamageSource;
7
import net.minecraft.world.entity.LivingEntity;
8
import net.minecraft.world.entity.ai.Brain;
9
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
10
11
public class HurtBySensor extends Sensor<LivingEntity> {
12
	@Override
13
	public Set<MemoryModuleType<?>> requires() {
14
		return ImmutableSet.of(MemoryModuleType.HURT_BY, MemoryModuleType.HURT_BY_ENTITY);
15
	}
16
17
	@Override
18
	protected void doTick(final ServerLevel level, final LivingEntity body) {
19
		Brain<?> brain = body.getBrain();
20
		DamageSource damageSource = body.getLastDamageSource();
21
		if (damageSource != null) {
22
			brain.setMemory(MemoryModuleType.HURT_BY, body.getLastDamageSource());
23
			if (damageSource.getEntity() instanceof LivingEntity livingEntity) {
24
				brain.setMemory(MemoryModuleType.HURT_BY_ENTITY, livingEntity);
25
			}
26
		} else {
27
			brain.eraseMemory(MemoryModuleType.HURT_BY);
28
		}
29
30
		brain.getMemory(MemoryModuleType.HURT_BY_ENTITY).ifPresent(hurtByEntity -> {
31
			if (!hurtByEntity.isAlive() || hurtByEntity.level() != level) {
32
				brain.eraseMemory(MemoryModuleType.HURT_BY_ENTITY);
33
			}
34
		});
35
	}
36
}
37

Decompiled Minecraft 26.3 source of HurtBySensor (net/minecraft/world/entity/ai/sensing/HurtBySensor.java) with Mojang official mappings, stored verbatim on the site.