-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Path.java
136 lines (116 loc) · 4.17 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package net.citizensnpcs.api.astar.pathfinder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.util.Vector;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
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;
public class Path implements Plan {
private List<Block> blockList;
private int index = 0;
private final PathEntry[] path;
public Path(Collection<Vector> vector) {
this.path = Iterables.toArray(Iterables.transform(vector, new Function<Vector, PathEntry>() {
@Override
public PathEntry apply(Vector input) {
return new PathEntry(input, Collections.<PathCallback> emptyList());
}
}), PathEntry.class);
}
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) {
for (Vector vector : node.getPathVectors()) {
path.add(new PathEntry(vector, node.callbacks));
}
}
return path.toArray(new PathEntry[path.size()]);
}
public List<Block> getBlocks(World world) {
return Arrays.asList(path).stream()
.map(p -> world.getBlockAt(p.vector.getBlockX(), p.vector.getBlockY(), p.vector.getBlockZ()))
.collect(Collectors.toList());
}
public Vector getCurrentVector() {
return path[index].vector;
}
public Iterable<Vector> getPath() {
return Iterables.transform(Arrays.asList(path), new Function<PathEntry, Vector>() {
@Override
public Vector apply(PathEntry input) {
return input.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;
}
path[index].onComplete((NPC) agent);
++index;
}
private class PathEntry {
final List<PathCallback> callbacks;
final Vector vector;
private PathEntry(Vector vector, List<PathCallback> callbacks) {
this.vector = vector;
this.callbacks = callbacks;
}
public void onComplete(NPC npc) {
if (callbacks == null)
return;
Block current = npc.getEntity().getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(),
vector.getBlockZ());
for (PathCallback callback : callbacks) {
callback.onReached(npc, current);
}
}
public void run(final NPC npc) {
if (callbacks == null)
return;
Block current = npc.getEntity().getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(),
vector.getBlockZ());
for (PathCallback callback : callbacks) {
if (blockList == null) {
blockList = Lists.transform(Arrays.asList(path), new Function<PathEntry, Block>() {
@Override
public Block apply(PathEntry input) {
return npc.getEntity().getWorld().getBlockAt(input.vector.getBlockX(),
input.vector.getBlockY(), input.vector.getBlockZ());
}
});
}
callback.run(npc, current, blockList, index);
}
}
@Override
public String toString() {
return vector.toString();
}
}
}