-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
GroupNPCFlock.java
47 lines (39 loc) · 1.38 KB
/
GroupNPCFlock.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
package net.citizensnpcs.api.ai.flocking;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.citizensnpcs.api.npc.NPC;
/**
* Defines a static flock of NPCs with an optional radius. If the radius is positive then NPCs will only be considered
* part of the flock if they are within the base NPC's radius currently.
*/
public class GroupNPCFlock implements NPCFlock {
private final List<NPC> npcs;
private final double radius;
public GroupNPCFlock(Iterable<NPC> npcs, double radius) {
this.npcs = new ArrayList<NPC>();
this.radius = radius;
}
@Override
public Collection<NPC> getNearby(final NPC npc) {
if (radius < 0)
return npcs;
return npcs.stream().filter(new Predicate<NPC>() {
@Override
public boolean test(NPC input) {
return input.getStoredLocation().distance(npc.getStoredLocation()) < radius;
}
}).collect(Collectors.<NPC> toList());
}
public List<NPC> getNPCs() {
return npcs;
}
public static GroupNPCFlock create(Iterable<NPC> npcs) {
return new GroupNPCFlock(npcs, -1);
}
public static GroupNPCFlock createWithRadius(Iterable<NPC> npcs, double radius) {
return new GroupNPCFlock(npcs, radius);
}
}