-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Path.java
111 lines (92 loc) · 3.09 KB
/
Path.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.citizensnpcs.api.astar.pathfinder;
import java.util.Arrays;
import java.util.List;
import net.citizensnpcs.api.astar.Agent;
import net.citizensnpcs.api.astar.Plan;
import net.citizensnpcs.api.astar.pathfinder.PathPoint.PathCallback;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import com.google.common.collect.Lists;
public class Path implements Plan {
private int index = 0;
private final PathEntry[] path;
Path(Iterable<VectorNode> unfiltered) {
this.path = cull(unfiltered);
}
private PathEntry[] cull(Iterable<VectorNode> unfiltered) {
// possibly expose cullability in an API
List<PathEntry> path = Lists.newArrayList();
for (VectorNode node : unfiltered) {
if (node.callbacks != null)
continue;
Vector vector = node.location;
path.add(new PathEntry(vector, node.callbacks));
}
return path.toArray(new PathEntry[path.size()]);
}
public void debug() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (PathEntry entry : path) {
player.sendBlockChange(entry.vector.toLocation(player.getWorld()), Material.YELLOW_FLOWER, (byte) 0);
}
}
}
public void debugEnd() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (PathEntry entry : path) {
Block block = entry.vector.toLocation(player.getWorld()).getBlock();
player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
}
}
}
public Vector getCurrentVector() {
return path[index].vector;
}
@Override
public boolean isComplete() {
return index >= path.length;
}
public void run(NPC npc) {
path[index].run(npc);
}
@Override
public String toString() {
return Arrays.toString(path);
}
@Override
public void update(Agent agent) {
if (isComplete()) {
return;
}
++index;
}
private static class PathEntry {
final List<PathCallback> callbacks;
final Vector vector;
private PathEntry(Vector vector, List<PathCallback> callbacks) {
this.vector = vector;
this.callbacks = callbacks;
}
private Block getBlockUsingWorld(World world) {
return world.getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
}
public void run(NPC npc) {
if (callbacks != null) {
Block block = getBlockUsingWorld(npc.getEntity().getWorld());
double distance = npc.getStoredLocation().distance(block.getLocation());
for (PathCallback callback : callbacks) {
callback.run(npc, block, distance);
}
}
}
@Override
public String toString() {
return vector.toString();
}
}
}