RangedBowAttackGoal .java

net.minecraft.world.entity.ai.goal.RangedBowAttackGoal (class): members: setMinAttackInterval, strafingBackwards, strafingClockwise, strafingTime, seeTime.

net.minecraft.world.entity.ai.goalclass140 linescommonextends Monster3.9 KB

API Surface (8)

void setMinAttackInterval(final int ticks)
boolean canUse()
boolean isHoldingBow()
boolean canContinueToUse()
void start()
void stop()
boolean requiresUpdateEveryTick()
void tick()

Fields & Constants (9)

mob
speedModifier
attackIntervalMin
attackRadiusSqr
attackTime
seeTime
strafingClockwise
strafingBackwards
strafingTime

Code Graph: Dependencies (8)

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

Full Source

RangedBowAttackGoal.java140 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import java.util.EnumSet;
4
import net.minecraft.world.entity.LivingEntity;
5
import net.minecraft.world.entity.Mob;
6
import net.minecraft.world.entity.monster.Monster;
7
import net.minecraft.world.entity.monster.RangedAttackMob;
8
import net.minecraft.world.entity.projectile.ProjectileUtil;
9
import net.minecraft.world.item.BowItem;
10
import net.minecraft.world.item.Items;
11
12
public class RangedBowAttackGoal<T extends Monster & RangedAttackMob> extends Goal {
13
	private final T mob;
14
	private final double speedModifier;
15
	private int attackIntervalMin;
16
	private final float attackRadiusSqr;
17
	private int attackTime = -1;
18
	private int seeTime;
19
	private boolean strafingClockwise;
20
	private boolean strafingBackwards;
21
	private int strafingTime = -1;
22
23
	public RangedBowAttackGoal(final T mob, final double speedModifier, final int attackIntervalMin, final float attackRadius) {
24
		this.mob = mob;
25
		this.speedModifier = speedModifier;
26
		this.attackIntervalMin = attackIntervalMin;
27
		this.attackRadiusSqr = attackRadius * attackRadius;
28
		this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
29
	}
30
31
	public void setMinAttackInterval(final int ticks) {
32
		this.attackIntervalMin = ticks;
33
	}
34
35
	@Override
36
	public boolean canUse() {
37
		return this.mob.getTarget() == null ? false : this.isHoldingBow();
38
	}
39
40
	protected boolean isHoldingBow() {
41
		return this.mob.isHolding(Items.BOW);
42
	}
43
44
	@Override
45
	public boolean canContinueToUse() {
46
		return (this.canUse() || !this.mob.getNavigation().isDone()) && this.isHoldingBow();
47
	}
48
49
	@Override
50
	public void start() {
51
		super.start();
52
		this.mob.setAggressive(true);
53
	}
54
55
	@Override
56
	public void stop() {
57
		super.stop();
58
		this.mob.setAggressive(false);
59
		this.seeTime = 0;
60
		this.attackTime = -1;
61
		this.mob.stopUsingItem();
62
	}
63
64
	@Override
65
	public boolean requiresUpdateEveryTick() {
66
		return true;
67
	}
68
69
	@Override
70
	public void tick() {
71
		LivingEntity target = this.mob.getTarget();
72
		if (target != null) {
73
			double targetDistSqr = this.mob.distanceToSqr(target.getX(), target.getY(), target.getZ());
74
			boolean hasLineOfSight = this.mob.getSensing().hasLineOfSight(target);
75
			boolean hadLineOfSight = this.seeTime > 0;
76
			if (hasLineOfSight != hadLineOfSight) {
77
				this.seeTime = 0;
78
			}
79
80
			if (hasLineOfSight) {
81
				this.seeTime++;
82
			} else {
83
				this.seeTime--;
84
			}
85
86
			if (!(targetDistSqr > this.attackRadiusSqr) && this.seeTime >= 20) {
87
				this.mob.getNavigation().stop();
88
				this.strafingTime++;
89
			} else {
90
				this.mob.getNavigation().moveTo(target, this.speedModifier);
91
				this.strafingTime = -1;
92
			}
93
94
			if (this.strafingTime >= 20) {
95
				if (this.mob.getRandom().nextFloat() < 0.3) {
96
					this.strafingClockwise = !this.strafingClockwise;
97
				}
98
99
				if (this.mob.getRandom().nextFloat() < 0.3) {
100
					this.strafingBackwards = !this.strafingBackwards;
101
				}
102
103
				this.strafingTime = 0;
104
			}
105
106
			if (this.strafingTime > -1) {
107
				if (targetDistSqr > this.attackRadiusSqr * 0.75F) {
108
					this.strafingBackwards = false;
109
				} else if (targetDistSqr < this.attackRadiusSqr * 0.25F) {
110
					this.strafingBackwards = true;
111
				}
112
113
				this.mob.getMoveControl().strafe(this.strafingBackwards ? -0.5F : 0.5F, this.strafingClockwise ? 0.5F : -0.5F);
114
				if (this.mob.getControlledVehicle() instanceof Mob vehicle) {
115
					vehicle.lookAt(target, 30.0F, 30.0F);
116
				}
117
118
				this.mob.lookAt(target, 30.0F, 30.0F);
119
			} else {
120
				this.mob.getLookControl().setLookAt(target, 30.0F, 30.0F);
121
			}
122
123
			if (this.mob.isUsingItem()) {
124
				if (!hasLineOfSight && this.seeTime < -60) {
125
					this.mob.stopUsingItem();
126
				} else if (hasLineOfSight) {
127
					int pullTime = this.mob.getTicksUsingItem();
128
					if (pullTime >= 20) {
129
						this.mob.stopUsingItem();
130
						this.mob.performRangedAttack(target, BowItem.getPowerForTime(pullTime));
131
						this.attackTime = this.attackIntervalMin;
132
					}
133
				}
134
			} else if (--this.attackTime <= 0 && this.seeTime >= -60) {
135
				this.mob.startUsingItem(ProjectileUtil.getWeaponHoldingHand(this.mob, Items.BOW));
136
			}
137
		}
138
	}
139
}
140

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