Add debug for navigation, make sure that destination is in range before calling waypoint triggers

This commit is contained in:
fullwall 2013-05-15 11:58:09 +08:00
parent 3b061f9c1f
commit fdde0bf7f8
5 changed files with 30 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import net.citizensnpcs.api.ai.event.NavigatorCallback;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
@ -190,6 +191,8 @@ public class CitizensNavigator implements Navigator, Runnable {
itr.next().onCompletion(reason);
itr.remove();
}
if (Messaging.isDebugging())
Messaging.debug(npc.getId(), "cancelling with reason", reason);
if (reason == null) {
stopNavigating();
return;
@ -211,6 +214,8 @@ public class CitizensNavigator implements Navigator, Runnable {
}
private void switchStrategyTo(PathStrategy newStrategy) {
if (Messaging.isDebugging())
Messaging.debug(npc.getId(), "changing to new PathStrategy", newStrategy);
if (executing != null)
Bukkit.getPluginManager().callEvent(new NavigationReplaceEvent(this));
executing = newStrategy;

View File

@ -46,6 +46,11 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
navigation.g();
}
@Override
public String toString() {
return "MCNavigationStrategy [target=" + target + "]";
}
@Override
public boolean update() {
if (getCancelReason() != null)

View File

@ -94,6 +94,11 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
navigation.g();
}
@Override
public String toString() {
return "MCTargetStrategy [target=" + target + "]";
}
@Override
public boolean update() {
if (target == null || !target.getBukkitEntity().isValid()) {
@ -155,6 +160,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private static final double ATTACK_DISTANCE = 1.75 * 1.75;
private static final Location HANDLE_LOCATION = new Location(null, 0, 0, 0);
private static Field NAV_E, NAV_J, NAV_M;
private static final Location TARGET_LOCATION = new Location(null, 0, 0, 0);
static {

View File

@ -419,9 +419,13 @@ public class LinearWaypointProvider implements WaypointProvider {
getNavigator().getLocalParameters().addSingleUseCallback(new NavigatorCallback() {
@Override
public void onCompletion(@Nullable CancelReason cancelReason) {
selector.finish();
if (currentDestination != null)
if (npc.isSpawned()
&& currentDestination != null
&& Util.locationWithinRange(npc.getBukkitEntity().getLocation(),
currentDestination.getLocation(), 4)) {
currentDestination.onReach(npc);
}
selector.finish();
}
});
return true;

View File

@ -87,6 +87,14 @@ public class Util {
return location.getWorld().isChunkLoaded(chunkX, chunkZ);
}
public static boolean locationWithinRange(Location current, Location target, double range) {
if (current == null || target == null)
return false;
if (current.getWorld() != target.getWorld())
return false;
return current.distanceSquared(target) < Math.pow(range, 2);
}
public static EntityType matchEntityType(String toMatch) {
EntityType type = EntityType.fromName(toMatch);
if (type != null)