Fix changing an NPC's speed mid-path

This commit is contained in:
fullwall 2015-07-15 15:51:38 +08:00
parent c8053f9d68
commit 27eb290eb3
2 changed files with 16 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.commands;
import org.bukkit.command.CommandSender;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
@ -11,8 +13,6 @@ import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.StringHelper;
import org.bukkit.command.CommandSender;
@Requirements
public class AdminCommands {
private final Citizens plugin;

View File

@ -1,5 +1,8 @@
package net.citizensnpcs.npc.ai;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.event.CancelReason;
@ -8,19 +11,18 @@ import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_8_R3.EntityLiving;
import net.minecraft.server.v1_8_R3.NavigationAbstract;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
public class MCNavigationStrategy extends AbstractPathStrategy {
private final EntityLiving handle;
private float lastSpeed;
private final NavigationAbstract navigation;
private final NavigatorParameters parameters;
private final Location target;
private final EntityLiving handle;
MCNavigationStrategy(final NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION);
this.target = dest;
this.parameters = params;
this.lastSpeed = parameters.speed();
handle = ((CraftLivingEntity) npc.getEntity()).getHandle();
handle.onGround = true;
// not sure of a better way around this - if onGround is false, then
@ -33,6 +35,10 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
}
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION).distanceSquared(target);
}
@Override
public Location getTargetAsLocation() {
return target;
@ -57,7 +63,10 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
public boolean update() {
if (getCancelReason() != null)
return true;
navigation.a(parameters.speed());
if (parameters.speed() != lastSpeed) {
navigation.a(target.getX(), target.getY(), target.getZ(), parameters.speed());
lastSpeed = parameters.speed();
}
parameters.run();
if (distanceSquared() < parameters.distanceMargin()) {
stop();
@ -66,9 +75,5 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
return NMS.isNavigationFinished(navigation);
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION).distanceSquared(target);
}
private static final Location HANDLE_LOCATION = new Location(null, 0, 0, 0);
}