Search in sources :

Example 1 with PathStrategy

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);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) PathStrategy(net.citizensnpcs.api.ai.PathStrategy)

Example 2 with PathStrategy

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);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) PathStrategy(net.citizensnpcs.api.ai.PathStrategy)

Example 3 with PathStrategy

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);
}
Also used : PathStrategy(net.citizensnpcs.api.ai.PathStrategy)

Example 4 with PathStrategy

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();
    }
}
Also used : NavigationStuckEvent(net.citizensnpcs.api.ai.event.NavigationStuckEvent) PathStrategy(net.citizensnpcs.api.ai.PathStrategy) TeleportStuckAction(net.citizensnpcs.api.ai.TeleportStuckAction) StuckAction(net.citizensnpcs.api.ai.StuckAction) NavigationCancelEvent(net.citizensnpcs.api.ai.event.NavigationCancelEvent) NavigatorCallback(net.citizensnpcs.api.ai.event.NavigatorCallback)

Example 5 with PathStrategy

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);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) PathStrategy(net.citizensnpcs.api.ai.PathStrategy) NavigationCompleteEvent(net.citizensnpcs.api.ai.event.NavigationCompleteEvent) Location(org.bukkit.Location)

Aggregations

PathStrategy (net.citizensnpcs.api.ai.PathStrategy)5 LivingEntity (org.bukkit.entity.LivingEntity)3 StuckAction (net.citizensnpcs.api.ai.StuckAction)1 TeleportStuckAction (net.citizensnpcs.api.ai.TeleportStuckAction)1 NavigationCancelEvent (net.citizensnpcs.api.ai.event.NavigationCancelEvent)1 NavigationCompleteEvent (net.citizensnpcs.api.ai.event.NavigationCompleteEvent)1 NavigationStuckEvent (net.citizensnpcs.api.ai.event.NavigationStuckEvent)1 NavigatorCallback (net.citizensnpcs.api.ai.event.NavigatorCallback)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1