BehaviorUtils .java

net.minecraft.world.entity.ai.behavior.BehaviorUtils (class, 168 lines): members: isOtherTargetMuchFurtherAwayThanCurrentAttackTarget, getTargetNearestMe.

net.minecraft.world.entity.ai.behaviorclass168 linescommon8.0 KB

API Surface (17)

void lockGazeAndWalkToEachOther(final LivingEntity entity1, final LivingEntity entity2, final float speedModifier, final int closeEnoughDistance)
boolean entityIsVisible(final Brain<?> brain, final LivingEntity targetEntity)
boolean targetIsValid(final Brain<?> brain, final MemoryModuleType<? extends LivingEntity> memory, final EntityType<?> targetType)
void lookAtEntity(final LivingEntity looker, final LivingEntity targetEntity)
void setWalkAndLookTargetMemories(final LivingEntity walker, final Entity targetEntity, final float speedModifier, final int closeEnoughDistance)
void setWalkAndLookTargetMemories(final LivingEntity walker, final BlockPos targetPos, final float speedModifier, final int closeEnoughDistance)
void setWalkAndLookTargetMemories(final LivingEntity walker, final PositionTracker target, final float speedModifier, final int closeEnoughDistance)
void throwItem(final LivingEntity thrower, final ItemStack item, final Vec3 targetPos)
void throwItem(final LivingEntity thrower, final ItemStack item, final Vec3 targetPos, final Vec3 throwVelocity, final float handYDistanceFromEye)
SectionPos findSectionClosestToVillage(final ServerLevel level, final SectionPos center, final int radius)
boolean isWithinAttackRange(final Mob body, final LivingEntity target, final int projectileAttackRangeMargin)
boolean isOtherTargetMuchFurtherAwayThanCurrentAttackTarget(final LivingEntity body, final LivingEntity otherTarget, final double howMuchFurtherAway)
boolean canSee(final LivingEntity body, final LivingEntity target)
LivingEntity getNearestTarget(final LivingEntity body, final Optional<LivingEntity> target1, final LivingEntity target2)
LivingEntity getTargetNearestMe(final LivingEntity body, final LivingEntity target1, final LivingEntity target2)
Optional<LivingEntity> getLivingEntityFromUUIDMemory(final LivingEntity body, final MemoryModuleType<UUID> memoryType)
boolean isBreeding(final LivingEntity body)

Fields & Constants (4)

memory
speedModifier
throwVelocity
otherTarget

Full Source

BehaviorUtils.java168 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import java.util.Comparator;
4
import java.util.Optional;
5
import java.util.UUID;
6
import java.util.function.Predicate;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.SectionPos;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.world.entity.Entity;
11
import net.minecraft.world.entity.EntityType;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.Mob;
14
import net.minecraft.world.entity.PathfinderMob;
15
import net.minecraft.world.entity.ai.Brain;
16
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
17
import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
18
import net.minecraft.world.entity.ai.memory.WalkTarget;
19
import net.minecraft.world.entity.ai.util.DefaultRandomPos;
20
import net.minecraft.world.entity.item.ItemEntity;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.item.ProjectileWeaponItem;
23
import net.minecraft.world.level.pathfinder.PathComputationType;
24
import net.minecraft.world.phys.Vec3;
25
import org.jspecify.annotations.Nullable;
26
27
public class BehaviorUtils {
28
	private BehaviorUtils() {
29
	}
30
31
	public static void lockGazeAndWalkToEachOther(final LivingEntity entity1, final LivingEntity entity2, final float speedModifier, final int closeEnoughDistance) {
32
		lookAtEachOther(entity1, entity2);
33
		setWalkAndLookTargetMemoriesToEachOther(entity1, entity2, speedModifier, closeEnoughDistance);
34
	}
35
36
	public static boolean entityIsVisible(final Brain<?> brain, final LivingEntity targetEntity) {
37
		Optional<NearestVisibleLivingEntities> visibleEntities = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES);
38
		return visibleEntities.isPresent() && visibleEntities.get().contains(targetEntity);
39
	}
40
41
	public static boolean targetIsValid(final Brain<?> brain, final MemoryModuleType<? extends LivingEntity> memory, final EntityType<?> targetType) {
42
		return targetIsValid(brain, memory, entity -> entity.is(targetType));
43
	}
44
45
	private static boolean targetIsValid(
46
		final Brain<?> brain, final MemoryModuleType<? extends LivingEntity> memory, final Predicate<LivingEntity> targetPredicate
47
	) {
48
		return brain.getMemory(memory).filter(targetPredicate).filter(LivingEntity::isAlive).filter(entity -> entityIsVisible(brain, entity)).isPresent();
49
	}
50
51
	private static void lookAtEachOther(final LivingEntity entity1, final LivingEntity entity2) {
52
		lookAtEntity(entity1, entity2);
53
		lookAtEntity(entity2, entity1);
54
	}
55
56
	public static void lookAtEntity(final LivingEntity looker, final LivingEntity targetEntity) {
57
		looker.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(targetEntity, true));
58
	}
59
60
	private static void setWalkAndLookTargetMemoriesToEachOther(
61
		final LivingEntity entity1, final LivingEntity entity2, final float speedModifier, final int closeEnoughDistance
62
	) {
63
		setWalkAndLookTargetMemories(entity1, entity2, speedModifier, closeEnoughDistance);
64
		setWalkAndLookTargetMemories(entity2, entity1, speedModifier, closeEnoughDistance);
65
	}
66
67
	public static void setWalkAndLookTargetMemories(final LivingEntity walker, final Entity targetEntity, final float speedModifier, final int closeEnoughDistance) {
68
		setWalkAndLookTargetMemories(walker, new EntityTracker(targetEntity, true), speedModifier, closeEnoughDistance);
69
	}
70
71
	public static void setWalkAndLookTargetMemories(final LivingEntity walker, final BlockPos targetPos, final float speedModifier, final int closeEnoughDistance) {
72
		setWalkAndLookTargetMemories(walker, new BlockPosTracker(targetPos), speedModifier, closeEnoughDistance);
73
	}
74
75
	public static void setWalkAndLookTargetMemories(
76
		final LivingEntity walker, final PositionTracker target, final float speedModifier, final int closeEnoughDistance
77
	) {
78
		WalkTarget walkTarget = new WalkTarget(target, speedModifier, closeEnoughDistance);
79
		walker.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, target);
80
		walker.getBrain().setMemory(MemoryModuleType.WALK_TARGET, walkTarget);
81
	}
82
83
	public static void throwItem(final LivingEntity thrower, final ItemStack item, final Vec3 targetPos) {
84
		Vec3 throwVelocity = new Vec3(0.3F, 0.3F, 0.3F);
85
		throwItem(thrower, item, targetPos, throwVelocity, 0.3F);
86
	}
87
88
	public static void throwItem(
89
		final LivingEntity thrower, final ItemStack item, final Vec3 targetPos, final Vec3 throwVelocity, final float handYDistanceFromEye
90
	) {
91
		double yHandPos = thrower.getEyeY() - handYDistanceFromEye;
92
		ItemEntity itemEntity = new ItemEntity(thrower.level(), thrower.getX(), yHandPos, thrower.getZ(), item);
93
		itemEntity.setThrower(thrower);
94
		Vec3 throwVector = targetPos.subtract(thrower.position());
95
		throwVector = throwVector.normalize().multiply(throwVelocity.x, throwVelocity.y, throwVelocity.z);
96
		itemEntity.setDeltaMovement(throwVector);
97
		itemEntity.setDefaultPickUpDelay();
98
		thrower.level().addFreshEntity(itemEntity);
99
	}
100
101
	public static SectionPos findSectionClosestToVillage(final ServerLevel level, final SectionPos center, final int radius) {
102
		int distToVillage = level.sectionsToVillage(center);
103
		return SectionPos.cube(center, radius)
104
			.filter(s -> level.sectionsToVillage(s) < distToVillage)
105
			.min(Comparator.comparingInt(level::sectionsToVillage))
106
			.orElse(center);
107
	}
108
109
	public static boolean isWithinAttackRange(final Mob body, final LivingEntity target, final int projectileAttackRangeMargin) {
110
		if (body.getMainHandItem().getItem() instanceof ProjectileWeaponItem weapon && body.canUseNonMeleeWeapon(body.getMainHandItem())) {
111
			int maxAllowedDistance = weapon.getDefaultProjectileRange() - projectileAttackRangeMargin;
112
			return body.closerThan(target, maxAllowedDistance);
113
		} else {
114
			return body.isWithinMeleeAttackRange(target);
115
		}
116
	}
117
118
	public static boolean isOtherTargetMuchFurtherAwayThanCurrentAttackTarget(
119
		final LivingEntity body, final LivingEntity otherTarget, final double howMuchFurtherAway
120
	) {
121
		Optional<LivingEntity> currentTarget = body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET);
122
		if (currentTarget.isEmpty()) {
123
			return false;
124
		}
125
126
		double distSqrToCurrentTarget = body.distanceToSqr(currentTarget.get().position());
127
		double distSqrToOtherTarget = body.distanceToSqr(otherTarget.position());
128
		return distSqrToOtherTarget > distSqrToCurrentTarget + howMuchFurtherAway * howMuchFurtherAway;
129
	}
130
131
	public static boolean canSee(final LivingEntity body, final LivingEntity target) {
132
		Brain<?> brain = body.getBrain();
133
		return !brain.hasMemoryValue(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
134
			? false
135
			: brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES).get().contains(target);
136
	}
137
138
	public static LivingEntity getNearestTarget(final LivingEntity body, final Optional<LivingEntity> target1, final LivingEntity target2) {
139
		return target1.isEmpty() ? target2 : getTargetNearestMe(body, target1.get(), target2);
140
	}
141
142
	public static LivingEntity getTargetNearestMe(final LivingEntity body, final LivingEntity target1, final LivingEntity target2) {
143
		Vec3 pos1 = target1.position();
144
		Vec3 pos2 = target2.position();
145
		return body.distanceToSqr(pos1) < body.distanceToSqr(pos2) ? target1 : target2;
146
	}
147
148
	public static Optional<LivingEntity> getLivingEntityFromUUIDMemory(final LivingEntity body, final MemoryModuleType<UUID> memoryType) {
149
		Optional<UUID> uuidMemory = body.getBrain().getMemory(memoryType);
150
		return uuidMemory.<Entity>map(uuid -> body.level().getEntity(uuid)).map(entity -> entity instanceof LivingEntity livingEntity ? livingEntity : null);
151
	}
152
153
	public static @Nullable Vec3 getRandomSwimmablePos(final PathfinderMob body, final int maxHorizontalDistance, final int maxVerticalDistance) {
154
		Vec3 targetPos = DefaultRandomPos.getPos(body, maxHorizontalDistance, maxVerticalDistance);
155
		int count = 0;
156
157
		while (targetPos != null && !body.level().getBlockState(BlockPos.containing(targetPos)).isPathfindable(PathComputationType.WATER) && count++ < 10) {
158
			targetPos = DefaultRandomPos.getPos(body, maxHorizontalDistance, maxVerticalDistance);
159
		}
160
161
		return targetPos;
162
	}
163
164
	public static boolean isBreeding(final LivingEntity body) {
165
		return body.getBrain().hasMemoryValue(MemoryModuleType.BREED_TARGET);
166
	}
167
}
168

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