JumpOnBed .java

net.minecraft.world.entity.ai.behavior.JumpOnBed (class): members: remainingCooldownUntilNextJump, remainingTimeToReachBed, COOLDOWN_BETWEEN_JUMPS, stop.

net.minecraft.world.entity.ai.behaviorclass114 linescommonextends Behavior<Mob>4.0 KB

API Surface (6)

boolean checkExtraStartConditions(final ServerLevel level, final Mob body)
void start(final ServerLevel level, final Mob body, final long timestamp)
void stop(final ServerLevel level, final Mob body, final long timestamp)
boolean canStillUse(final ServerLevel level, final Mob body, final long timestamp)
boolean timedOut(final long timestamp)
void tick(final ServerLevel level, final Mob body, final long timestamp)

Fields & Constants (9)

MAX_TIME_TO_REACH_BED
MIN_JUMPS
MAX_JUMPS
COOLDOWN_BETWEEN_JUMPS
speedModifier
targetBed
remainingTimeToReachBed
remainingJumps
remainingCooldownUntilNextJump

Code Graph: Dependencies (8)

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

Full Source

JumpOnBed.java114 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import java.util.Optional;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.tags.BlockTags;
8
import net.minecraft.world.entity.Mob;
9
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
10
import net.minecraft.world.entity.ai.memory.MemoryStatus;
11
import net.minecraft.world.entity.ai.memory.WalkTarget;
12
import org.jspecify.annotations.Nullable;
13
14
public class JumpOnBed extends Behavior<Mob> {
15
	private static final int MAX_TIME_TO_REACH_BED = 100;
16
	private static final int MIN_JUMPS = 3;
17
	private static final int MAX_JUMPS = 6;
18
	private static final int COOLDOWN_BETWEEN_JUMPS = 5;
19
	private final float speedModifier;
20
	private @Nullable BlockPos targetBed;
21
	private int remainingTimeToReachBed;
22
	private int remainingJumps;
23
	private int remainingCooldownUntilNextJump;
24
25
	public JumpOnBed(final float speedModifier) {
26
		super(ImmutableMap.of(MemoryModuleType.NEAREST_BED, MemoryStatus.VALUE_PRESENT, MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT));
27
		this.speedModifier = speedModifier;
28
	}
29
30
	protected boolean checkExtraStartConditions(final ServerLevel level, final Mob body) {
31
		return body.isBaby() && this.nearBed(level, body);
32
	}
33
34
	protected void start(final ServerLevel level, final Mob body, final long timestamp) {
35
		super.start(level, body, timestamp);
36
		this.getNearestBed(body).ifPresent(targetBed -> {
37
			this.targetBed = targetBed;
38
			this.remainingTimeToReachBed = 100;
39
			this.remainingJumps = 3 + level.getRandom().nextInt(4);
40
			this.remainingCooldownUntilNextJump = 0;
41
			this.startWalkingTowardsBed(body, targetBed);
42
		});
43
	}
44
45
	protected void stop(final ServerLevel level, final Mob body, final long timestamp) {
46
		super.stop(level, body, timestamp);
47
		this.targetBed = null;
48
		this.remainingTimeToReachBed = 0;
49
		this.remainingJumps = 0;
50
		this.remainingCooldownUntilNextJump = 0;
51
	}
52
53
	protected boolean canStillUse(final ServerLevel level, final Mob body, final long timestamp) {
54
		return body.isBaby()
55
			&& this.targetBed != null
56
			&& this.isJumpable(level, this.targetBed)
57
			&& !this.tiredOfWalking(level, body)
58
			&& !this.tiredOfJumping(level, body);
59
	}
60
61
	@Override
62
	protected boolean timedOut(final long timestamp) {
63
		return false;
64
	}
65
66
	protected void tick(final ServerLevel level, final Mob body, final long timestamp) {
67
		if (!this.onOrOverBed(level, body)) {
68
			this.remainingTimeToReachBed--;
69
		} else if (this.remainingCooldownUntilNextJump > 0) {
70
			this.remainingCooldownUntilNextJump--;
71
		} else {
72
			if (this.onBedSurface(level, body)) {
73
				body.getJumpControl().jump();
74
				this.remainingJumps--;
75
				this.remainingCooldownUntilNextJump = 5;
76
			}
77
		}
78
	}
79
80
	private void startWalkingTowardsBed(final Mob body, final BlockPos bedPos) {
81
		body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(bedPos, this.speedModifier, 0));
82
	}
83
84
	private boolean nearBed(final ServerLevel level, final Mob body) {
85
		return this.onOrOverBed(level, body) || this.getNearestBed(body).isPresent();
86
	}
87
88
	private boolean onOrOverBed(final ServerLevel level, final Mob body) {
89
		BlockPos bodyPos = body.blockPosition();
90
		BlockPos oneBelow = bodyPos.below();
91
		return this.isJumpable(level, bodyPos) || this.isJumpable(level, oneBelow);
92
	}
93
94
	private boolean onBedSurface(final ServerLevel level, final Mob body) {
95
		return this.isJumpable(level, body.blockPosition());
96
	}
97
98
	private boolean isJumpable(final ServerLevel level, final BlockPos bodyPos) {
99
		return level.getBlockState(bodyPos).is(BlockTags.VILLAGER_BABIES_CAN_JUMP_ON_BED);
100
	}
101
102
	private Optional<BlockPos> getNearestBed(final Mob body) {
103
		return body.getBrain().getMemory(MemoryModuleType.NEAREST_BED);
104
	}
105
106
	private boolean tiredOfWalking(final ServerLevel level, final Mob body) {
107
		return !this.onOrOverBed(level, body) && this.remainingTimeToReachBed <= 0;
108
	}
109
110
	private boolean tiredOfJumping(final ServerLevel level, final Mob body) {
111
		return this.onOrOverBed(level, body) && this.remainingJumps <= 0;
112
	}
113
}
114

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