From 27eb290eb3384eebc5cc04777d721c2274ac6516 Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 15 Jul 2015 15:51:38 +0800 Subject: [PATCH] Fix changing an NPC's speed mid-path --- .../citizensnpcs/commands/AdminCommands.java | 4 ++-- .../npc/ai/MCNavigationStrategy.java | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/citizensnpcs/commands/AdminCommands.java b/src/main/java/net/citizensnpcs/commands/AdminCommands.java index f3bcdf4da..0c77d068b 100644 --- a/src/main/java/net/citizensnpcs/commands/AdminCommands.java +++ b/src/main/java/net/citizensnpcs/commands/AdminCommands.java @@ -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; diff --git a/src/main/java/net/citizensnpcs/npc/ai/MCNavigationStrategy.java b/src/main/java/net/citizensnpcs/npc/ai/MCNavigationStrategy.java index b3863890c..add45a887 100644 --- a/src/main/java/net/citizensnpcs/npc/ai/MCNavigationStrategy.java +++ b/src/main/java/net/citizensnpcs/npc/ai/MCNavigationStrategy.java @@ -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); }