-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
MCNavigationStrategy.java
84 lines (68 loc) · 2.18 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
76
77
78
79
80
81
82
83
84
package net.citizensnpcs.npc.ai;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import net.citizensnpcs.api.ai.AbstractPathStrategy;
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;
public class MCNavigationStrategy extends AbstractPathStrategy {
private final Entity handle;
private final MCNavigator navigator;
private final NavigatorParameters parameters;
private final Location target;
MCNavigationStrategy(final NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION);
this.target = dest;
this.parameters = params;
handle = npc.getEntity();
this.navigator = NMS.getTargetNavigator(npc.getEntity(), dest, params);
}
private double distanceSquared() {
return handle.getLocation(HANDLE_LOCATION).distanceSquared(target);
}
@Override
public Iterable<Vector> getPath() {
return navigator.getPath();
}
@Override
public Location getTargetAsLocation() {
return target;
}
@Override
public TargetType getTargetType() {
return TargetType.LOCATION;
}
@Override
public void stop() {
navigator.stop();
}
@Override
public String toString() {
return "MCNavigationStrategy [target=" + target + "]";
}
@Override
public boolean update() {
if (navigator.getCancelReason() != null) {
setCancelReason(navigator.getCancelReason());
}
if (getCancelReason() != null)
return true;
boolean wasFinished = navigator.update();
parameters.run();
if (distanceSquared() < parameters.distanceMargin()) {
stop();
return true;
}
return wasFinished;
}
public static interface MCNavigator {
CancelReason getCancelReason();
Iterable<Vector> getPath();
void stop();
boolean update();
}
private static final Location HANDLE_LOCATION = new Location(null, 0, 0, 0);
}