-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
MCNavigationStrategy.java
75 lines (64 loc) · 2.33 KB
/
MCNavigationStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package net.citizensnpcs.npc.ai;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_8_R1.EntityLiving;
import net.minecraft.server.v1_8_R1.NavigationAbstract;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftLivingEntity;
public class MCNavigationStrategy extends AbstractPathStrategy {
private final NavigationAbstract navigation;
private final NavigatorParameters parameters;
private final Location target;
private final EntityLiving handle;
MCNavigationStrategy(final NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION);
this.target = dest;
this.parameters = params;
handle = ((CraftLivingEntity) npc.getEntity()).getHandle();
handle.onGround = true;
// not sure of a better way around this - if onGround is false, then
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
navigation = NMS.getNavigation(handle);
navigation.a(dest.getX(), dest.getY(), dest.getZ(), parameters.speed());
if (NMS.isNavigationFinished(navigation)) {
setCancelReason(CancelReason.STUCK);
}
}
@Override
public Location getTargetAsLocation() {
return target;
}
@Override
public TargetType getTargetType() {
return TargetType.LOCATION;
}
@Override
public void stop() {
NMS.stopNavigation(navigation);
}
@Override
public String toString() {
return "MCNavigationStrategy [target=" + target + "]";
}
@Override
public boolean update() {
if (getCancelReason() != null)
return true;
navigation.a(parameters.speed());
parameters.run();
if (distanceSquared() < parameters.distanceMargin()) {
stop();
return true;
}
return NMS.isNavigationFinished(navigation);
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION)
.distanceSquared(target);
}
private static final Location HANDLE_LOCATION = new Location(null, 0, 0, 0);
}