GroundPathNavigation .java

net.minecraft.world.entity.ai.navigation.GroundPathNavigation (class): members: setCanPathToTargetsBelowSurface, canPathToTargetsBelowSurface, setAvoidSun.

net.minecraft.world.entity.ai.navigationclass150 linescommonextends PathNavigation4.3 KB

API Surface (10)

PathFinder createPathFinder(final int maxVisitedNodes)
boolean canUpdatePath()
Vec3 getTempMobPos()
Path createPath(BlockPos pos, final int reachRange)
Path createPath(final Entity target, final int reachRange)
void trimPath()
boolean canNavigateGround()
void setAvoidSun(final boolean avoidSun)
void setCanWalkOverFences(final boolean canWalkOverFences)
void setCanPathToTargetsBelowSurface(final boolean canPathToTargetsBelowSurface)

Fields & Constants (2)

avoidSun
canPathToTargetsBelowSurface

Full Source

GroundPathNavigation.java150 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.navigation;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.core.SectionPos;
6
import net.minecraft.tags.FluidTags;
7
import net.minecraft.util.Mth;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.Mob;
10
import net.minecraft.world.level.Level;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.level.chunk.LevelChunk;
13
import net.minecraft.world.level.pathfinder.Node;
14
import net.minecraft.world.level.pathfinder.Path;
15
import net.minecraft.world.level.pathfinder.PathFinder;
16
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
17
import net.minecraft.world.phys.Vec3;
18
19
public class GroundPathNavigation extends PathNavigation {
20
	private boolean avoidSun;
21
	private boolean canPathToTargetsBelowSurface;
22
23
	public GroundPathNavigation(final Mob mob, final Level level) {
24
		super(mob, level);
25
	}
26
27
	@Override
28
	protected PathFinder createPathFinder(final int maxVisitedNodes) {
29
		this.nodeEvaluator = new WalkNodeEvaluator();
30
		return new PathFinder(this.nodeEvaluator, maxVisitedNodes);
31
	}
32
33
	@Override
34
	protected boolean canUpdatePath() {
35
		return this.mob.onGround() || this.mob.isInLiquid() || this.mob.isPassenger();
36
	}
37
38
	@Override
39
	protected Vec3 getTempMobPos() {
40
		return new Vec3(this.mob.getX(), this.getSurfaceY(), this.mob.getZ());
41
	}
42
43
	@Override
44
	public Path createPath(BlockPos pos, final int reachRange) {
45
		LevelChunk chunk = this.level.getChunkSource().getChunkNow(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()));
46
		if (chunk == null) {
47
			return null;
48
		}
49
50
		if (!this.canPathToTargetsBelowSurface) {
51
			pos = this.findSurfacePosition(chunk, pos, reachRange);
52
		}
53
54
		return super.createPath(pos, reachRange);
55
	}
56
57
	private BlockPos findSurfacePosition(final LevelChunk chunk, BlockPos pos, final int reachRange) {
58
		if (chunk.getBlockState(pos).isAir()) {
59
			BlockPos.MutableBlockPos columnPos = pos.mutable().move(Direction.DOWN);
60
61
			while (columnPos.getY() >= this.level.getMinY() && chunk.getBlockState(columnPos).isAir()) {
62
				columnPos.move(Direction.DOWN);
63
			}
64
65
			if (columnPos.getY() >= this.level.getMinY()) {
66
				return columnPos.above();
67
			}
68
69
			columnPos.setY(pos.getY() + 1);
70
71
			while (columnPos.getY() <= this.level.getMaxY() && chunk.getBlockState(columnPos).isAir()) {
72
				columnPos.move(Direction.UP);
73
			}
74
75
			pos = columnPos;
76
		}
77
78
		if (!chunk.getBlockState(pos).isSolid()) {
79
			return pos;
80
		}
81
82
		BlockPos.MutableBlockPos columnPos = pos.mutable().move(Direction.UP);
83
84
		while (columnPos.getY() <= this.level.getMaxY() && chunk.getBlockState(columnPos).isSolid()) {
85
			columnPos.move(Direction.UP);
86
		}
87
88
		return columnPos.immutable();
89
	}
90
91
	@Override
92
	public Path createPath(final Entity target, final int reachRange) {
93
		return this.createPath(target.blockPosition(), reachRange);
94
	}
95
96
	private int getSurfaceY() {
97
		if (this.mob.isInFloatableFluid() && this.canFloat()) {
98
			int surface = this.mob.getBlockY();
99
			BlockState state = this.level.getBlockState(BlockPos.containing(this.mob.getX(), surface, this.mob.getZ()));
100
			int steps = 0;
101
102
			while (state.getFluidState().is(FluidTags.ENTITY_FLOATABLE)) {
103
				state = this.level.getBlockState(BlockPos.containing(this.mob.getX(), ++surface, this.mob.getZ()));
104
				if (++steps > 16) {
105
					return this.mob.getBlockY();
106
				}
107
			}
108
109
			return surface;
110
		} else {
111
			return Mth.floor(this.mob.getY() + 0.5);
112
		}
113
	}
114
115
	@Override
116
	protected void trimPath() {
117
		super.trimPath();
118
		if (this.avoidSun) {
119
			if (this.level.canSeeSky(BlockPos.containing(this.mob.getX(), this.mob.getY() + 0.5, this.mob.getZ()))) {
120
				return;
121
			}
122
123
			for (int i = 0; i < this.path.getNodeCount(); i++) {
124
				Node node = this.path.getNode(i);
125
				if (this.level.canSeeSky(new BlockPos(node.x, node.y, node.z))) {
126
					this.path.truncateNodes(i);
127
					return;
128
				}
129
			}
130
		}
131
	}
132
133
	@Override
134
	public boolean canNavigateGround() {
135
		return true;
136
	}
137
138
	public void setAvoidSun(final boolean avoidSun) {
139
		this.avoidSun = avoidSun;
140
	}
141
142
	public void setCanWalkOverFences(final boolean canWalkOverFences) {
143
		this.nodeEvaluator.setCanWalkOverFences(canWalkOverFences);
144
	}
145
146
	public void setCanPathToTargetsBelowSurface(final boolean canPathToTargetsBelowSurface) {
147
		this.canPathToTargetsBelowSurface = canPathToTargetsBelowSurface;
148
	}
149
}
150

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