Skip to content

Commit

Permalink
Add speed waypoint trigger, stop pathfinding if y < -5
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 7, 2014
1 parent 7e782f7 commit abe3b4b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java
Expand Up @@ -255,6 +255,10 @@ private boolean updateStationaryStatus() {
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);
Expand Down
@@ -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);
}
}
@@ -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);
}
}
Expand Up @@ -24,9 +24,6 @@ public void save(WaypointTrigger instance, DataKey root) {
PersistenceLoader.save(instance, root);
}

private static final Map<String, Class<? extends Prompt>> triggerPrompts = Maps.newHashMap();
private static final Map<String, Class<? extends WaypointTrigger>> triggers = Maps.newHashMap();

public static void addTrigger(String name, Class<? extends WaypointTrigger> triggerClass,
Class<? extends WaypointTriggerPrompt> promptClass) {
triggers.put(name, triggerClass);
Expand All @@ -48,11 +45,15 @@ public static Prompt getTriggerPromptFrom(String input) {
}
}

private static final Map<String, Class<? extends Prompt>> triggerPrompts = Maps.newHashMap();
private static final Map<String, Class<? extends WaypointTrigger>> 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);
}
}
1 change: 1 addition & 0 deletions src/main/java/net/citizensnpcs/util/Messages.java
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_en.properties
Expand Up @@ -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}.
Expand Down

0 comments on commit abe3b4b

Please sign in to comment.