AI Goals and Riding
AI goals
EntityDefinition.builder(texture)
.goal(4, SeekGlowBlockGoal::new) // a new goal object for each mob
.build();
SeekGlowBlockGoal.java
final class SeekGlowBlockGoal implements AiGoal { // canStart, shouldContinue, start, tick, stop
private BlockPos target;
@Override
public boolean canStart(MobView mob) {
target = find(mob.world(), mob.blockPos()); // find a glowing block nearby
return target != null;
}
@Override
public boolean shouldContinue(MobView mob) {
return mob.isNavigating();
}
@Override
public void start(MobView mob) {
mob.navigateTo(target.x() + 0.5, target.y(), target.z() + 0.5, 1.0);
}
@Override
public void tick(MobView mob) {
mob.lookAt(target.x() + 0.5, target.y() + 0.5, target.z() + 0.5);
}
@Override
public void stop(MobView mob) {
mob.stopNavigating();
}
}
- The game's goal scheduler runs it on the server thread.
- A lower number means higher priority. Goals that use the same control (
GoalControl:MOVE,LOOK,JUMP,TARGET) do not run at the same time. Overridecontrols()to set the controls a goal uses. - The priorities of the default goals are 0 (floating), 1 (fleeing), 2 (attacking), 3 (tempting), 5 (wandering), and 6–7 (looking around).
targetGoal(priority, factory)is a goal that picks the attack target.- The factory is called once per mob, so a goal object can hold state.
MobView
| Method | Meaning |
|---|---|
navigateTo(x, y, z, speed) | Start pathfinding |
isNavigating(), stopNavigating() | Pathfinding state, and stopping it |
lookAt(x, y, z) | Gaze |
target(), setTarget(living) | Attack target |
Movement and riding
EntityDefinition.builder(texture)
.rideable(true) // use with an empty hand to mount, steer with movement keys
.noGravity().notPushable();
entity.setVelocity(0, 0.5, 0);
player.startRiding(golem);
Movement-related methods of EntityView:
| Method | Meaning |
|---|---|
velocity(), setVelocity(x, y, z) | Velocity (Vector3). For players, it is sent to the client |
yaw(), pitch(), setRotation(yaw, pitch) | Direction |
hasNoGravity(), setNoGravity(bool) | Gravity |
isOnGround() | Whether it is on the ground |
vehicle(), passengers(), startRiding(vehicle), stopRiding() | Mount and passengers |
- A mob with
rideable(true)is steered like a horse. As with a horse, the riding player's client computes the steering movement. rideable(false)can be ridden but not steered.
Needs human verification
You cannot automate key input, so a person must check mount steering by hand.