Fixed an issue with waypoints

This commit is contained in:
fullwall 2012-05-12 10:01:27 +08:00
parent 09776024e0
commit 8a07f129b9
5 changed files with 23 additions and 11 deletions

View File

@ -15,12 +15,12 @@ import org.bukkit.entity.Player;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
public class MCNavigationStrategy implements PathStrategy { public class MCNavigationStrategy implements PathStrategy {
private EntityHumanNPC entity = null; private final EntityLiving entity;
private final Navigation navigation; private final Navigation navigation;
MCNavigationStrategy(CitizensNPC npc, Location dest) { MCNavigationStrategy(CitizensNPC npc, Location dest) {
entity = npc.getHandle();
if (npc.getBukkitEntity() instanceof Player) { if (npc.getBukkitEntity() instanceof Player) {
entity = (EntityHumanNPC) npc.getHandle();
entity.onGround = true; entity.onGround = true;
// not sure of a better way around this - if onGround is false, then // not sure of a better way around this - if onGround is false, then
// navigation won't execute, and calling entity.move doesn't // navigation won't execute, and calling entity.move doesn't
@ -32,8 +32,8 @@ public class MCNavigationStrategy implements PathStrategy {
} }
MCNavigationStrategy(EntityLiving entity, EntityLiving target) { MCNavigationStrategy(EntityLiving entity, EntityLiving target) {
this.entity = entity;
if (entity instanceof EntityHumanNPC) { if (entity instanceof EntityHumanNPC) {
this.entity = (EntityHumanNPC) entity;
entity.onGround = true; // see above entity.onGround = true; // see above
} }
navigation = entity.al(); navigation = entity.al();
@ -60,9 +60,9 @@ public class MCNavigationStrategy implements PathStrategy {
@Override @Override
public boolean update() { public boolean update() {
if (entity != null) { if (entity instanceof EntityHumanNPC) {
navigation.d(); navigation.d();
entity.moveOnCurrentHeading(); ((EntityHumanNPC) entity).moveOnCurrentHeading();
} }
return navigation.e(); return navigation.e();
} }

View File

@ -44,7 +44,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHandle {
@Override @Override
public void F_() { public void F_() {
super.F_(); super.F_();
if (motX != 0 || motZ != 0 || motY != 0) { if (!npc.getAI().hasDestination() && (motX != 0 || motZ != 0 || motY != 0)) {
a(0, 0); a(0, 0);
} }
if (noDamageTicks > 0) if (noDamageTicks > 0)

View File

@ -40,9 +40,11 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
@EventHandler @EventHandler
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.getPlayer().equals(player)) if (!event.getPlayer().equals(player) || event.getAction() == Action.PHYSICAL)
return; return;
if (event.getAction() == Action.LEFT_CLICK_BLOCK) { if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
if (event.getClickedBlock() == null)
return;
Location at = event.getClickedBlock().getLocation(); Location at = event.getClickedBlock().getLocation();
waypoints.add(new Waypoint(at)); waypoints.add(new Waypoint(at));
Messaging.send(player, String.format("<e>Added<a> a waypoint at (<e>%d<a>, <e>%d<a>, <e>%d<a>).", Messaging.send(player, String.format("<e>Added<a> a waypoint at (<e>%d<a>, <e>%d<a>, <e>%d<a>).",
@ -67,6 +69,11 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
return waypoints.iterator(); return waypoints.iterator();
} }
@Override
public void onAttach() {
callback.onProviderChanged();
}
@Override @Override
public void load(DataKey key) { public void load(DataKey key) {
for (DataKey root : key.getRelative("points").getIntegerSubKeys()) { for (DataKey root : key.getRelative("points").getIntegerSubKeys()) {
@ -75,7 +82,6 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
.getDouble("y"), root.getDouble("z"), (float) root.getDouble("yaw", 0), (float) root.getDouble( .getDouble("y"), root.getDouble("z"), (float) root.getDouble("yaw", 0), (float) root.getDouble(
"pitch", 0)))); "pitch", 0))));
} }
callback.onProviderChanged();
} }
@Override @Override

View File

@ -41,4 +41,6 @@ public interface WaypointProvider {
* The key to save to * The key to save to
*/ */
public void save(DataKey key); public void save(DataKey key);
public void onAttach();
} }

View File

@ -15,7 +15,7 @@ import org.bukkit.entity.Player;
public class Waypoints extends Trait { public class Waypoints extends Trait {
private final NPC npc; private final NPC npc;
private WaypointProvider provider = new LinearWaypointProvider(); private WaypointProvider provider = new LinearWaypointProvider();
private String providerName; private String providerName = "linear";
public Waypoints(NPC npc) { public Waypoints(NPC npc) {
this.npc = npc; this.npc = npc;
@ -32,6 +32,11 @@ public class Waypoints extends Trait {
} }
} }
@Override
public void onNPCSpawn() {
npc.getAI().registerNavigationCallback(provider.getCallback());
}
public Editor getEditor(Player player) { public Editor getEditor(Player player) {
return provider.createEditor(player); return provider.createEditor(player);
} }
@ -49,7 +54,6 @@ public class Waypoints extends Trait {
if (provider == null) if (provider == null)
return; return;
provider.load(key.getRelative(providerName)); provider.load(key.getRelative(providerName));
npc.getAI().registerNavigationCallback(provider.getCallback());
} }
@Override @Override