-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
WanderGoal.java
79 lines (67 loc) · 2.37 KB
/
WanderGoal.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
package net.citizensnpcs.api.ai.goals;
import java.util.Random;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
public class WanderGoal extends BehaviorGoalAdapter {
private boolean forceFinish;
private final NPC npc;
private final Random random = new Random();
private final int xrange;
private final int yrange;
private WanderGoal(NPC npc, int xrange, int yrange) {
this.npc = npc;
this.xrange = xrange;
this.yrange = yrange;
}
private Location findRandomPosition() {
Location base = npc.getEntity().getLocation();
Location found = null;
for (int i = 0; i < 10; i++) {
int x = base.getBlockX() + random.nextInt(2 * xrange) - xrange;
int y = base.getBlockY() + random.nextInt(2 * yrange) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * xrange) - xrange;
Block block = base.getWorld().getBlockAt(x, y - 2, z);
if (MinecraftBlockExaminer.canStandOn(block)) {
found = block.getLocation().add(0, 1, 0);
break;
}
}
return found;
}
@EventHandler
public void onFinish(NavigationCompleteEvent event) {
forceFinish = true;
}
@Override
public void reset() {
forceFinish = false;
}
@Override
public BehaviorStatus run() {
if (!npc.getNavigator().isNavigating() || forceFinish)
return BehaviorStatus.SUCCESS;
return BehaviorStatus.RUNNING;
}
@Override
public boolean shouldExecute() {
if (!npc.isSpawned() || npc.getNavigator().isNavigating())
return false;
Location dest = findRandomPosition();
if (dest == null)
return false;
npc.getNavigator().setTarget(dest);
return true;
}
public static WanderGoal createWithNPC(NPC npc) {
return createWithNPCAndRange(npc, 10, 2);
}
public static WanderGoal createWithNPCAndRange(NPC npc, int xrange, int yrange) {
return new WanderGoal(npc, xrange, yrange);
}
}