use of net.citizensnpcs.api.ai.PathStrategy in project Citizens2 by CitizensDev.
the class CitizensNavigator method setTarget.
@Override
public void setTarget(Location target) {
if (!npc.isSpawned())
throw new IllegalStateException("npc is not spawned");
if (target == null) {
cancelNavigation();
return;
}
switchParams();
updatePathfindingRange();
PathStrategy newStrategy;
if (npc.isFlyable()) {
newStrategy = new FlyingAStarNavigationStrategy(npc, target, localParams);
} else if (localParams.useNewPathfinder() || !(npc.getEntity() instanceof LivingEntity)) {
newStrategy = new AStarNavigationStrategy(npc, target, localParams);
} else {
newStrategy = new MCNavigationStrategy(npc, target, localParams);
}
switchStrategyTo(newStrategy);
}
use of net.citizensnpcs.api.ai.PathStrategy in project Citizens2 by CitizensDev.
the class CitizensNavigator method setTarget.
@Override
public void setTarget(Iterable<Vector> path) {
if (!npc.isSpawned())
throw new IllegalStateException("npc is not spawned");
if (path == null || Iterables.size(path) == 0) {
cancelNavigation();
return;
}
switchParams();
updatePathfindingRange();
PathStrategy newStrategy;
if (npc.isFlyable()) {
newStrategy = new FlyingAStarNavigationStrategy(npc, path, localParams);
} else if (localParams.useNewPathfinder() || !(npc.getEntity() instanceof LivingEntity)) {
newStrategy = new AStarNavigationStrategy(npc, path, localParams);
} else {
newStrategy = new MCNavigationStrategy(npc, path, localParams);
}
switchStrategyTo(newStrategy);
}
use of net.citizensnpcs.api.ai.PathStrategy in project Citizens2 by CitizensDev.
the class CitizensNavigator method setTarget.
@Override
public void setTarget(Entity target, boolean aggressive) {
if (!npc.isSpawned())
throw new IllegalStateException("npc is not spawned");
if (target == null) {
cancelNavigation();
return;
}
switchParams();
updatePathfindingRange();
PathStrategy newStrategy = new MCTargetStrategy(npc, target, aggressive, localParams);
switchStrategyTo(newStrategy);
}
use of net.citizensnpcs.api.ai.PathStrategy in project Citizens2 by CitizensDev.
the class CitizensNavigator method stopNavigating.
private void stopNavigating(CancelReason reason) {
if (!isNavigating())
return;
Iterator<NavigatorCallback> itr = localParams.callbacks().iterator();
while (itr.hasNext()) {
itr.next().onCompletion(reason);
itr.remove();
}
if (reason == null) {
stopNavigating();
return;
}
if (reason == CancelReason.STUCK) {
StuckAction action = localParams.stuckAction();
NavigationStuckEvent event = new NavigationStuckEvent(this, action);
Bukkit.getPluginManager().callEvent(event);
action = event.getAction();
boolean shouldContinue = action != null ? action.run(npc, this) : false;
if (shouldContinue) {
stationaryTicks = 0;
executing.clearCancelReason();
return;
}
}
NavigationCancelEvent event = new NavigationCancelEvent(this, reason);
PathStrategy old = executing;
Bukkit.getPluginManager().callEvent(event);
if (old == executing) {
stopNavigating();
}
}
use of net.citizensnpcs.api.ai.PathStrategy in project Citizens2 by CitizensDev.
the class CitizensNavigator method run.
@Override
public void run() {
updateMountedStatus();
if (!isNavigating() || !npc.isSpawned() || paused)
return;
if (!npc.getStoredLocation().getWorld().equals(getTargetAsLocation().getWorld()) || Math.pow(localParams.range(), 2) < npc.getStoredLocation().distanceSquared(getTargetAsLocation())) {
stopNavigating(CancelReason.STUCK);
return;
}
if (updateStationaryStatus())
return;
updatePathfindingRange();
boolean finished = executing.update();
if (localParams.lookAtFunction() != null) {
Util.faceLocation(npc.getEntity(), localParams.lookAtFunction().apply(this), true, true);
Entity entity = npc.getEntity().getPassenger();
Location npcLoc = npc.getEntity().getLocation();
while (entity != null) {
Location loc = entity.getLocation(STATIONARY_LOCATION);
loc.setYaw(npcLoc.getYaw());
entity.teleport(loc);
entity = entity.getPassenger();
}
}
if (!finished) {
return;
}
if (executing.getCancelReason() != null) {
stopNavigating(executing.getCancelReason());
} else {
NavigationCompleteEvent event = new NavigationCompleteEvent(this);
PathStrategy old = executing;
Bukkit.getPluginManager().callEvent(event);
if (old == executing) {
stopNavigating(null);
}
}
}
Aggregations