-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Path.java
156 lines (135 loc) · 4.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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.ListIterator;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
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;
import net.citizensnpcs.api.util.SpigotUtil;
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 void debug() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (PathEntry entry : path) {
if (SpigotUtil.isUsing1_13API()) {
player.sendBlockChange(entry.vector.toLocation(player.getWorld()), YELLOW_FLOWER.createBlockData());
} else {
player.sendBlockChange(entry.vector.toLocation(player.getWorld()), YELLOW_FLOWER, (byte) 0);
}
}
}
}
public void debugEnd() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (PathEntry entry : path) {
Block block = entry.vector.toLocation(player.getWorld()).getBlock();
if (SpigotUtil.isUsing1_13API()) {
player.sendBlockChange(block.getLocation(), block.getBlockData());
} else {
player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
}
}
}
}
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;
}
++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 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());
}
});
}
ListIterator<Block> vec = blockList.listIterator();
if (index > 0) {
while (index != vec.nextIndex()) {
vec.next();
}
}
callback.run(npc, current, vec);
}
}
@Override
public String toString() {
return vector.toString();
}
}
private static Material YELLOW_FLOWER = SpigotUtil.isUsing1_13API() ? Material.SUNFLOWER
: Material.valueOf("YELLOW_FLOWER");
}