Allow straight line navigator to track entity exactly

This commit is contained in:
fullwall 2020-12-16 17:59:20 +08:00
parent 556c58a7f6
commit 1266298442
3 changed files with 19 additions and 3 deletions

View File

@ -409,8 +409,9 @@ public class CitizensNavigator implements Navigator, Runnable {
stopNavigating(CancelReason.STUCK);
return true;
}
} else
} else {
stationaryTicks = 0;
}
lastX = current.getBlockX();
lastY = current.getBlockY();
lastZ = current.getBlockZ();

View File

@ -226,7 +226,9 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
if (parameters.straightLineTargetingDistance() > 0) {
double distance = npc.getStoredLocation().distance(location);
if (distance < parameters.straightLineTargetingDistance()) {
active = new StraightLineNavigationStrategy(npc, location, parameters);
if (active == null) {
active = new StraightLineNavigationStrategy(npc, target, parameters);
}
return;
}
}

View File

@ -4,6 +4,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.util.Vector;
@ -17,9 +18,18 @@ import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
public class StraightLineNavigationStrategy extends AbstractPathStrategy {
private final Location destination;
private Location destination;
private final NPC npc;
private final NavigatorParameters params;
private Entity target;
public StraightLineNavigationStrategy(NPC npc, Entity target, NavigatorParameters params) {
super(TargetType.LOCATION);
this.params = params;
this.target = target;
this.npc = npc;
this.destination = params.entityTargetLocationMapper().apply(target);
}
public StraightLineNavigationStrategy(NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION);
@ -51,6 +61,9 @@ public class StraightLineNavigationStrategy extends AbstractPathStrategy {
if (currLoc.distance(destination) <= params.distanceMargin()) {
return true;
}
if (target != null) {
destination = params.entityTargetLocationMapper().apply(target);
}
Vector destVector = npc.getStoredLocation().toVector()
.add(destination.toVector().subtract(npc.getStoredLocation().toVector()).normalize());
Location point = destVector.toLocation(destination.getWorld());