From dc0fc270f1aa7a34bc82d7f1df1b9657dead386d Mon Sep 17 00:00:00 2001 From: fullwall Date: Fri, 7 Mar 2014 23:21:54 +0800 Subject: [PATCH] Add speed waypoint trigger, stop pathfinding if y < -5 --- .../npc/ai/CitizensNavigator.java | 4 +++ .../trait/waypoint/triggers/SpeedTrigger.java | 32 +++++++++++++++++++ .../waypoint/triggers/SpeedTriggerPrompt.java | 22 +++++++++++++ .../triggers/WaypointTriggerRegistry.java | 7 ++-- .../java/net/citizensnpcs/util/Messages.java | 1 + src/main/resources/messages_en.properties | 1 + 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTrigger.java create mode 100644 src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTriggerPrompt.java diff --git a/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java b/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java index bb83af13d..7ab5e2f7a 100644 --- a/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java +++ b/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java @@ -255,6 +255,10 @@ public class CitizensNavigator implements Navigator, Runnable { if (localParams.stationaryTicks() < 0) return false; Location current = npc.getEntity().getLocation(STATIONARY_LOCATION); + if (current.getY() < -5) { + stopNavigating(CancelReason.STUCK); + return true; + } if (lastX == current.getBlockX() && lastY == current.getBlockY() && lastZ == current.getBlockZ()) { if (++stationaryTicks >= localParams.stationaryTicks()) { stopNavigating(CancelReason.STUCK); diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTrigger.java b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTrigger.java new file mode 100644 index 000000000..aa4987882 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTrigger.java @@ -0,0 +1,32 @@ +package net.citizensnpcs.trait.waypoint.triggers; + +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.persistence.Persist; + +import org.bukkit.Location; + +public class SpeedTrigger implements WaypointTrigger { + @Persist + private float speed = 1F; + + public SpeedTrigger() { + } + + public SpeedTrigger(float speed) { + this.speed = speed; + } + + @Override + public String description() { + return String.format("Speed change to %f", speed); + } + + public float getSpeed() { + return speed; + } + + @Override + public void onWaypointReached(NPC npc, Location waypoint) { + npc.getNavigator().getDefaultParameters().speedModifier(speed); + } +} diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTriggerPrompt.java b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTriggerPrompt.java new file mode 100644 index 000000000..8fc339b4a --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/SpeedTriggerPrompt.java @@ -0,0 +1,22 @@ +package net.citizensnpcs.trait.waypoint.triggers; + +import net.citizensnpcs.api.util.Messaging; +import net.citizensnpcs.util.Messages; + +import org.bukkit.conversations.ConversationContext; +import org.bukkit.conversations.NumericPrompt; +import org.bukkit.conversations.Prompt; + +public class SpeedTriggerPrompt extends NumericPrompt implements WaypointTriggerPrompt { + @Override + protected Prompt acceptValidatedInput(ConversationContext context, Number input) { + float speed = (float) Math.max(input.doubleValue(), 0); + context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new SpeedTrigger(speed)); + return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY); + } + + @Override + public String getPromptText(ConversationContext context) { + return Messaging.tr(Messages.SPEED_TRIGGER_PROMPT); + } +} diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/triggers/WaypointTriggerRegistry.java b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/WaypointTriggerRegistry.java index 898e3f09a..e9c164046 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/triggers/WaypointTriggerRegistry.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/triggers/WaypointTriggerRegistry.java @@ -24,9 +24,6 @@ public class WaypointTriggerRegistry implements Persister { PersistenceLoader.save(instance, root); } - private static final Map> triggerPrompts = Maps.newHashMap(); - private static final Map> triggers = Maps.newHashMap(); - public static void addTrigger(String name, Class triggerClass, Class promptClass) { triggers.put(name, triggerClass); @@ -48,11 +45,15 @@ public class WaypointTriggerRegistry implements Persister { } } + private static final Map> triggerPrompts = Maps.newHashMap(); + private static final Map> triggers = Maps.newHashMap(); + static { addTrigger("animation", AnimationTrigger.class, AnimationTriggerPrompt.class); addTrigger("chat", ChatTrigger.class, ChatTriggerPrompt.class); addTrigger("delay", DelayTrigger.class, DelayTriggerPrompt.class); addTrigger("teleport", TeleportTrigger.class, TeleportTriggerPrompt.class); + addTrigger("speed", SpeedTrigger.class, SpeedTriggerPrompt.class); // addTrigger("pose", PoseTrigger.class, PoseTriggerPrompt.class); } } diff --git a/src/main/java/net/citizensnpcs/util/Messages.java b/src/main/java/net/citizensnpcs/util/Messages.java index 5284c3f82..ce90beeb4 100644 --- a/src/main/java/net/citizensnpcs/util/Messages.java +++ b/src/main/java/net/citizensnpcs/util/Messages.java @@ -191,6 +191,7 @@ public class Messages { public static final String SPAWN_NUMERIC_ID_ONLY = "citizens.commands.npc.spawn.numeric-id-only"; public static final String SPEED_MODIFIER_ABOVE_LIMIT = "citizens.commands.npc.speed.modifier-above-limit"; public static final String SPEED_MODIFIER_SET = "citizens.commands.npc.speed.set"; + public static final String SPEED_TRIGGER_PROMPT = "citizens.editors.waypoints.triggers.speed.prompt"; public static final String SWIMMING_SET = "citizens.commands.npc.swim.set"; public static final String SWIMMING_UNSET = "citizens.commands.npc.swim.unset"; public static final String TARGETABLE_SET = "citizens.commands.npc.targetable.set"; diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 5d7f2708a..e8fbe0435 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -215,6 +215,7 @@ citizens.editors.waypoints.triggers.remove.index-out-of-range=Index must be in t citizens.editors.waypoints.triggers.remove.not-a-number=Index must be a number. citizens.editors.waypoints.triggers.remove.prompt=Enter in the index of the trigger to delete or [[back]] to return to the edit prompt. Current triggers are: citizens.editors.waypoints.triggers.remove.removed=Successfully removed trigger {0}. +citizens.editors.waypoints.triggers.speed.prompt=Enter the speed modifier as a [[percentage]] of its base speed. citizens.editors.waypoints.triggers.teleport.invalid-format=Invalid location given. Format is [[world]]:[[x]]:[[y]]:[[z]]. citizens.editors.waypoints.triggers.teleport.prompt=Enter the destination in the format world:x:y:z. Type [[here]] to use your current location. Type [[back]] to return to the edit prompt. citizens.limits.over-npc-limit=Over the NPC limit of {0}.