VillagerPanicTrigger .java

net.minecraft.world.entity.ai.behavior.VillagerPanicTrigger (class): members: hasHostile, isHurt, canStillUse, start, tick. imports: Villager, Activity.

net.minecraft.world.entity.ai.behaviorclass49 linescommonextends Behavior<Villager>1.6 KB

API Surface (5)

boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp)
void start(final ServerLevel level, final Villager body, final long timestamp)
void tick(final ServerLevel level, final Villager body, final long timestamp)
boolean hasHostile(final LivingEntity myBody)
boolean isHurt(final LivingEntity myBody)

Code Graph: Dependencies (7)

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

Full Source

VillagerPanicTrigger.java49 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import net.minecraft.server.level.ServerLevel;
5
import net.minecraft.world.entity.LivingEntity;
6
import net.minecraft.world.entity.ai.Brain;
7
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
8
import net.minecraft.world.entity.npc.villager.Villager;
9
import net.minecraft.world.entity.schedule.Activity;
10
11
public class VillagerPanicTrigger extends Behavior<Villager> {
12
	public VillagerPanicTrigger() {
13
		super(ImmutableMap.of());
14
	}
15
16
	protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
17
		return isHurt(body) || hasHostile(body);
18
	}
19
20
	protected void start(final ServerLevel level, final Villager body, final long timestamp) {
21
		if (isHurt(body) || hasHostile(body)) {
22
			Brain<?> brain = body.getBrain();
23
			if (!brain.isActive(Activity.PANIC)) {
24
				brain.eraseMemory(MemoryModuleType.PATH);
25
				brain.eraseMemory(MemoryModuleType.WALK_TARGET);
26
				brain.eraseMemory(MemoryModuleType.LOOK_TARGET);
27
				brain.eraseMemory(MemoryModuleType.BREED_TARGET);
28
				brain.eraseMemory(MemoryModuleType.INTERACTION_TARGET);
29
			}
30
31
			brain.setActiveActivityIfPossible(Activity.PANIC);
32
		}
33
	}
34
35
	protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
36
		if (timestamp % 100L == 0L) {
37
			body.spawnGolemIfNeeded(level, timestamp, 3);
38
		}
39
	}
40
41
	public static boolean hasHostile(final LivingEntity myBody) {
42
		return myBody.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_HOSTILE);
43
	}
44
45
	public static boolean isHurt(final LivingEntity myBody) {
46
		return myBody.getBrain().hasMemoryValue(MemoryModuleType.HURT_BY);
47
	}
48
}
49

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