-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
WanderGoal.java
185 lines (163 loc) · 6.47 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package net.citizensnpcs.api.ai.goals;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import ch.ethz.globis.phtree.PhTreeSolid;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.ai.tree.Behavior;
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;
/**
* A sample {@link Goal}/{@link Behavior} that will wander within a certain radius or {@link QuadTree}.
*/
public class WanderGoal extends BehaviorGoalAdapter implements Listener {
private int delay;
private int delayedTicks;
private final Function<NPC, Location> fallback;
private boolean forceFinish;
private final NPC npc;
private boolean paused;
private final Supplier<PhTreeSolid<Boolean>> tree;
private Object worldguardRegion;
private int xrange;
private int yrange;
private WanderGoal(NPC npc, int xrange, int yrange, Supplier<PhTreeSolid<Boolean>> tree,
Function<NPC, Location> fallback, Object worldguardRegion) {
this.npc = npc;
this.worldguardRegion = worldguardRegion;
this.xrange = xrange;
this.yrange = yrange;
this.tree = tree;
this.fallback = fallback;
}
private Location findRandomPosition() {
Location found = MinecraftBlockExaminer.findRandomValidLocation(npc.getEntity().getLocation(NPC_LOCATION),
xrange, yrange, new Function<Block, Boolean>() {
@Override
public Boolean apply(Block block) {
if ((MinecraftBlockExaminer.isLiquidOrInLiquid(block.getRelative(BlockFace.UP))
|| MinecraftBlockExaminer.isLiquidOrInLiquid(block.getRelative(0, 2, 0)))
&& npc.getNavigator().getDefaultParameters().avoidWater()) {
return false;
}
if (worldguardRegion != null) {
try {
if (!((ProtectedRegion) worldguardRegion)
.contains(BukkitAdapter.asBlockVector(block.getLocation())))
return false;
} catch (Throwable t) {
t.printStackTrace();
}
}
if (tree != null) {
long[] pt = { block.getX(), block.getY(), block.getZ() };
if (tree.get() != null && !tree.get().queryIntersect(pt, pt).hasNext()) {
return false;
}
}
return true;
}
}, RANDOM);
if (found == null && fallback != null) {
return fallback.apply(npc);
}
return found;
}
@EventHandler
public void onFinish(NavigationCompleteEvent event) {
forceFinish = true;
}
public void pause() {
this.paused = true;
}
@Override
public void reset() {
delayedTicks = delay;
forceFinish = false;
HandlerList.unregisterAll(this);
}
@Override
public BehaviorStatus run() {
if (!npc.getNavigator().isNavigating() || forceFinish) {
return BehaviorStatus.SUCCESS;
}
return BehaviorStatus.RUNNING;
}
public void setDelay(int delay) {
this.delay = delay;
this.delayedTicks = delay;
}
public void setWorldGuardRegion(Object region) {
this.worldguardRegion = region;
}
public void setXYRange(int xrange, int yrange) {
this.xrange = xrange;
this.yrange = yrange;
}
@Override
public boolean shouldExecute() {
if (!npc.isSpawned() || npc.getNavigator().isNavigating() || paused)
return false;
if (delayedTicks-- > 0) {
return false;
}
Location dest = findRandomPosition();
if (dest == null)
return false;
npc.getNavigator().setTarget(dest);
CitizensAPI.registerEvents(this);
return true;
}
public void unpause() {
this.paused = false;
}
public static WanderGoal createWithNPC(NPC npc) {
return createWithNPCAndRange(npc, 10, 2);
}
public static WanderGoal createWithNPCAndRange(NPC npc, int xrange, int yrange) {
return createWithNPCAndRangeAndTree(npc, xrange, yrange, null);
}
public static WanderGoal createWithNPCAndRangeAndTree(NPC npc, int xrange, int yrange,
Supplier<PhTreeSolid<Boolean>> tree) {
return createWithNPCAndRangeAndTreeAndFallback(npc, xrange, yrange, tree, null);
}
public static WanderGoal createWithNPCAndRangeAndTreeAndFallback(NPC npc, int xrange, int yrange,
Supplier<PhTreeSolid<Boolean>> tree, Function<NPC, Location> fallback) {
return new WanderGoal(npc, xrange, yrange, tree, fallback, null);
}
/**
* The full builder method.
*
* @param npc
* the NPC to wander
* @param xrange
* x/z range, in blocks
* @param yrange
* y range, in blocks
* @param tree
* an optional {@link PhTreeSolid} supplier to allow only wandering within a certain {@link PhTreeSolid}
* @param fallback
* an optional fallback location
* @param worldguardRegion
* the optional region
* @return the built goal
*/
public static WanderGoal createWithNPCAndRangeAndTreeAndFallbackAndRegion(NPC npc, int xrange, int yrange,
Supplier<PhTreeSolid<Boolean>> tree, Function<NPC, Location> fallback, Object worldguardRegion) {
return new WanderGoal(npc, xrange, yrange, tree, fallback, worldguardRegion);
}
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Random RANDOM = new Random();
}