LongJumpToRandomPos .java

net.minecraft.world.entity.ai.behavior.LongJumpToRandomPos (class): members: MIN_PATHFIND_DISTANCE_TO_VALID_JUMP, defaultAcceptableLandingSpot, chosenJump.

net.minecraft.world.entity.ai.behaviorclass209 linescommonextends Mob>8.3 KB

API Surface (8)

<E extends Mob> boolean defaultAcceptableLandingSpot(final E body, final BlockPos targetPos)
boolean checkExtraStartConditions(final ServerLevel level, final Mob body)
boolean canStillUse(final ServerLevel level, final Mob body, final long timestamp)
void start(final ServerLevel level, final E body, final long timestamp)
void tick(final ServerLevel level, final E body, final long timestamp)
void pickCandidate(final ServerLevel level, final E body, final long timestamp)
Optional<LongJumpToRandomPos.PossibleJump> getJumpCandidate(final ServerLevel level)
record PossibleJump(BlockPos targetPos, int weight)

Fields & Constants (16)

FIND_JUMP_TRIES
PREPARE_JUMP_DURATION
MIN_PATHFIND_DISTANCE_TO_VALID_JUMP
TIME_OUT_DURATION
ALLOWED_ANGLES
timeBetweenLongJumps
maxLongJumpHeight
maxLongJumpWidth
maxJumpVelocityMultiplier
jumpCandidates
initialPosition
chosenJump
findJumpTries
prepareJumpStart
getJumpSound
acceptableLandingSpot

Full Source

LongJumpToRandomPos.java209 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.Lists;
5
import java.util.Collections;
6
import java.util.List;
7
import java.util.Optional;
8
import java.util.function.BiPredicate;
9
import java.util.function.Function;
10
import java.util.stream.Collectors;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.sounds.SoundEvent;
14
import net.minecraft.sounds.SoundSource;
15
import net.minecraft.util.Mth;
16
import net.minecraft.util.random.WeightedRandom;
17
import net.minecraft.util.valueproviders.UniformInt;
18
import net.minecraft.world.entity.Mob;
19
import net.minecraft.world.entity.ai.attributes.Attributes;
20
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
21
import net.minecraft.world.entity.ai.memory.MemoryStatus;
22
import net.minecraft.world.entity.ai.navigation.PathNavigation;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.block.Blocks;
25
import net.minecraft.world.level.pathfinder.Path;
26
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
27
import net.minecraft.world.phys.Vec3;
28
import org.jspecify.annotations.Nullable;
29
30
public class LongJumpToRandomPos<E extends Mob> extends Behavior<E> {
31
	protected static final int FIND_JUMP_TRIES = 20;
32
	private static final int PREPARE_JUMP_DURATION = 40;
33
	protected static final int MIN_PATHFIND_DISTANCE_TO_VALID_JUMP = 8;
34
	private static final int TIME_OUT_DURATION = 200;
35
	private static final List<Integer> ALLOWED_ANGLES = Lists.newArrayList(65, 70, 75, 80);
36
	private final UniformInt timeBetweenLongJumps;
37
	protected final int maxLongJumpHeight;
38
	protected final int maxLongJumpWidth;
39
	protected final float maxJumpVelocityMultiplier;
40
	protected List<LongJumpToRandomPos.PossibleJump> jumpCandidates = Lists.newArrayList();
41
	protected Optional<Vec3> initialPosition = Optional.empty();
42
	protected @Nullable Vec3 chosenJump;
43
	protected int findJumpTries;
44
	protected long prepareJumpStart;
45
	private final Function<E, SoundEvent> getJumpSound;
46
	private final BiPredicate<E, BlockPos> acceptableLandingSpot;
47
48
	public LongJumpToRandomPos(
49
		final UniformInt timeBetweenLongJumps,
50
		final int maxLongJumpHeight,
51
		final int maxLongJumpWidth,
52
		final float maxJumpVelocityMultiplier,
53
		final Function<E, SoundEvent> getJumpSound
54
	) {
55
		this(timeBetweenLongJumps, maxLongJumpHeight, maxLongJumpWidth, maxJumpVelocityMultiplier, getJumpSound, LongJumpToRandomPos::defaultAcceptableLandingSpot);
56
	}
57
58
	public static <E extends Mob> boolean defaultAcceptableLandingSpot(final E body, final BlockPos targetPos) {
59
		Level level = body.level();
60
		BlockPos below = targetPos.below();
61
		return level.getBlockState(below).isSolidRender() && body.getPathfindingMalus(WalkNodeEvaluator.getPathTypeStatic(body, targetPos)) == 0.0F;
62
	}
63
64
	public LongJumpToRandomPos(
65
		final UniformInt timeBetweenLongJumps,
66
		final int maxLongJumpHeight,
67
		final int maxLongJumpWidth,
68
		final float maxJumpVelocityMultiplier,
69
		final Function<E, SoundEvent> getJumpSound,
70
		final BiPredicate<E, BlockPos> acceptableLandingSpot
71
	) {
72
		super(
73
			ImmutableMap.of(
74
				MemoryModuleType.LOOK_TARGET,
75
				MemoryStatus.REGISTERED,
76
				MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS,
77
				MemoryStatus.VALUE_ABSENT,
78
				MemoryModuleType.LONG_JUMP_MID_JUMP,
79
				MemoryStatus.VALUE_ABSENT
80
			),
81
			200
82
		);
83
		this.timeBetweenLongJumps = timeBetweenLongJumps;
84
		this.maxLongJumpHeight = maxLongJumpHeight;
85
		this.maxLongJumpWidth = maxLongJumpWidth;
86
		this.maxJumpVelocityMultiplier = maxJumpVelocityMultiplier;
87
		this.getJumpSound = getJumpSound;
88
		this.acceptableLandingSpot = acceptableLandingSpot;
89
	}
90
91
	protected boolean checkExtraStartConditions(final ServerLevel level, final Mob body) {
92
		boolean canStart = body.onGround() && !body.isInWater() && !body.isInLava() && !level.getBlockState(body.blockPosition()).is(Blocks.HONEY_BLOCK);
93
		if (!canStart) {
94
			body.getBrain().setMemory(MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS, this.timeBetweenLongJumps.sample(level.getRandom()) / 2);
95
		}
96
97
		return canStart;
98
	}
99
100
	protected boolean canStillUse(final ServerLevel level, final Mob body, final long timestamp) {
101
		boolean isValid = this.initialPosition.isPresent()
102
			&& this.initialPosition.get().equals(body.position())
103
			&& this.findJumpTries > 0
104
			&& !body.isInWater()
105
			&& (this.chosenJump != null || !this.jumpCandidates.isEmpty());
106
		if (!isValid && body.getBrain().getMemory(MemoryModuleType.LONG_JUMP_MID_JUMP).isEmpty()) {
107
			body.getBrain().setMemory(MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS, this.timeBetweenLongJumps.sample(level.getRandom()) / 2);
108
			body.getBrain().eraseMemory(MemoryModuleType.LOOK_TARGET);
109
		}
110
111
		return isValid;
112
	}
113
114
	protected void start(final ServerLevel level, final E body, final long timestamp) {
115
		this.chosenJump = null;
116
		this.findJumpTries = 20;
117
		this.initialPosition = Optional.of(body.position());
118
		BlockPos mobPos = body.blockPosition();
119
		int mobX = mobPos.getX();
120
		int mobY = mobPos.getY();
121
		int mobZ = mobPos.getZ();
122
		this.jumpCandidates = BlockPos.betweenClosedStream(
123
				mobX - this.maxLongJumpWidth,
124
				mobY - this.maxLongJumpHeight,
125
				mobZ - this.maxLongJumpWidth,
126
				mobX + this.maxLongJumpWidth,
127
				mobY + this.maxLongJumpHeight,
128
				mobZ + this.maxLongJumpWidth
129
			)
130
			.filter(pos -> !pos.equals(mobPos))
131
			.map(pos -> new LongJumpToRandomPos.PossibleJump(pos.immutable(), Mth.ceil(mobPos.distSqr(pos))))
132
			.collect(Collectors.toCollection(Lists::newArrayList));
133
	}
134
135
	protected void tick(final ServerLevel level, final E body, final long timestamp) {
136
		if (this.chosenJump != null) {
137
			if (timestamp - this.prepareJumpStart >= 40L) {
138
				body.setYRot(body.yBodyRot);
139
				body.setDiscardFriction(true);
140
				double orgLength = this.chosenJump.length();
141
				double lengthWithJumpBoost = orgLength + body.getJumpBoostPower();
142
				body.setDeltaMovement(this.chosenJump.scale(lengthWithJumpBoost / orgLength));
143
				body.getBrain().setMemory(MemoryModuleType.LONG_JUMP_MID_JUMP, true);
144
				level.playSound(null, body, this.getJumpSound.apply(body), SoundSource.NEUTRAL, 1.0F, 1.0F);
145
			}
146
		} else {
147
			this.findJumpTries--;
148
			this.pickCandidate(level, body, timestamp);
149
		}
150
	}
151
152
	protected void pickCandidate(final ServerLevel level, final E body, final long timestamp) {
153
		while (!this.jumpCandidates.isEmpty()) {
154
			Optional<LongJumpToRandomPos.PossibleJump> optionalPosition = this.getJumpCandidate(level);
155
			if (!optionalPosition.isEmpty()) {
156
				LongJumpToRandomPos.PossibleJump position = optionalPosition.get();
157
				BlockPos targetPos = position.targetPos();
158
				if (this.isAcceptableLandingPosition(level, body, targetPos)) {
159
					Vec3 targetPosition = Vec3.atCenterOf(targetPos);
160
					Vec3 jumpVector = this.calculateOptimalJumpVector(body, targetPosition);
161
					if (jumpVector != null) {
162
						body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(targetPos));
163
						PathNavigation navigation = body.getNavigation();
164
						Path path = navigation.createPath(targetPos, 0, 8);
165
						if (path == null || !path.canReach()) {
166
							this.chosenJump = jumpVector;
167
							this.prepareJumpStart = timestamp;
168
							return;
169
						}
170
					}
171
				}
172
			}
173
		}
174
	}
175
176
	protected Optional<LongJumpToRandomPos.PossibleJump> getJumpCandidate(final ServerLevel level) {
177
		Optional<LongJumpToRandomPos.PossibleJump> randomItem = WeightedRandom.getRandomItem(
178
			level.getRandom(), this.jumpCandidates, LongJumpToRandomPos.PossibleJump::weight
179
		);
180
		randomItem.ifPresent(this.jumpCandidates::remove);
181
		return randomItem;
182
	}
183
184
	private boolean isAcceptableLandingPosition(final ServerLevel level, final E body, final BlockPos targetPos) {
185
		BlockPos bodyPos = body.blockPosition();
186
		int mobX = bodyPos.getX();
187
		int mobZ = bodyPos.getZ();
188
		return mobX == targetPos.getX() && mobZ == targetPos.getZ() ? false : this.acceptableLandingSpot.test(body, targetPos);
189
	}
190
191
	protected @Nullable Vec3 calculateOptimalJumpVector(final Mob body, final Vec3 targetPos) {
192
		List<Integer> allowedAngles = Lists.newArrayList(ALLOWED_ANGLES);
193
		Collections.shuffle(allowedAngles);
194
		float maxJumpVelocity = (float)(body.getAttributeValue(Attributes.JUMP_STRENGTH) * this.maxJumpVelocityMultiplier);
195
196
		for (int angle : allowedAngles) {
197
			Optional<Vec3> velocityVector = LongJumpUtil.calculateJumpVectorForAngle(body, targetPos, maxJumpVelocity, angle, true);
198
			if (velocityVector.isPresent()) {
199
				return velocityVector.get();
200
			}
201
		}
202
203
		return null;
204
	}
205
206
	public record PossibleJump(BlockPos targetPos, int weight) {
207
	}
208
}
209

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