Implement WaypointTriggers

This commit is contained in:
fullwall 2012-10-26 21:49:30 +08:00
parent 4ce0843315
commit 35844ac6c5
24 changed files with 503 additions and 94 deletions

View File

@ -103,10 +103,10 @@ public class CommandManager {
CommandContext context = new CommandContext(args);
if (context.argsLength() < cmd.min())
throw new CommandUsageException("Too few arguments.", getUsage(args, cmd));
throw new CommandUsageException(Messages.COMMAND_TOO_FEW_ARGUMENTS, getUsage(args, cmd));
if (cmd.max() != -1 && context.argsLength() > cmd.max())
throw new CommandUsageException("Too many arguments.", getUsage(args, cmd));
throw new CommandUsageException(Messages.COMMAND_TOO_MANY_ARGUMENTS, getUsage(args, cmd));
if (!cmd.flags().contains("*")) {
for (char flag : context.getFlags())

View File

@ -576,7 +576,7 @@ public class NPCCommands {
double z = Double.parseDouble(parts[2]);
World world = parts.length == 4 ? Bukkit.getWorld(parts[3]) : current.getWorld();
if (world == null)
throw new CommandException(Messages.MOVETO_WORLD_NOT_FOUND);
throw new CommandException(Messages.WORLD_NOT_FOUND);
to = new Location(world, x, y, z, current.getYaw(), current.getPitch());
} else {
to = current.clone();
@ -593,7 +593,7 @@ public class NPCCommands {
if (args.hasValueFlag("world")) {
World world = Bukkit.getWorld(args.getFlag("world"));
if (world == null)
throw new CommandException(Messages.MOVETO_WORLD_NOT_FOUND);
throw new CommandException(Messages.WORLD_NOT_FOUND);
to.setWorld(world);
}
}

View File

@ -1,8 +1,10 @@
package net.citizensnpcs.command.exception;
import net.citizensnpcs.util.Messages;
public class NoPermissionsException extends CommandException {
public NoPermissionsException() {
super("You don't have permission to execute that command.");
super(Messages.COMMAND_NO_PERMISSION);
}
private static final long serialVersionUID = -602374621030168291L;

View File

@ -40,14 +40,6 @@ public abstract class CitizensNPC extends AbstractNPC {
protected abstract EntityLiving createHandle(Location loc);
@Override
public void removeTrait(Class<? extends Trait> clazz) {
Trait present = traits.get(clazz);
if (present != null)
removedTraits.add(present.getName());
super.removeTrait(clazz);
}
@Override
public boolean despawn() {
if (!isSpawned())
@ -127,6 +119,21 @@ public abstract class CitizensNPC extends AbstractNPC {
navigator.load(root.getRelative("navigator"));
}
@Override
public void removeTrait(Class<? extends Trait> clazz) {
Trait present = traits.get(clazz);
if (present != null)
removedTraits.add(present.getName());
super.removeTrait(clazz);
}
private void removeTraitData(DataKey root) {
for (String name : removedTraits) {
root.removeKey("traits." + name);
}
removedTraits.clear();
}
public void save(DataKey root) {
root.setString("name", getFullName());
@ -143,13 +150,6 @@ public abstract class CitizensNPC extends AbstractNPC {
removeTraitData(root);
}
private void removeTraitData(DataKey root) {
for (String name : removedTraits) {
root.removeKey("traits." + name);
}
removedTraits.clear();
}
@Override
public boolean spawn(Location loc) {
Validate.notNull(loc, "location cannot be null");

View File

@ -15,6 +15,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.trait.waypoint.triggers.TriggerEditPrompt;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.NMS;
@ -43,7 +44,7 @@ public class LinearWaypointProvider implements WaypointProvider {
private final List<Waypoint> waypoints = Lists.newArrayList();
@Override
public Editor createEditor(Player player) {
public WaypointEditor createEditor(Player player) {
return new LinearWaypointEditor(player);
}
@ -85,7 +86,7 @@ public class LinearWaypointProvider implements WaypointProvider {
currentGoal.setPaused(paused);
}
private final class LinearWaypointEditor extends Editor {
private final class LinearWaypointEditor extends WaypointEditor {
boolean editing = true;
int editingSlot = waypoints.size() - 1;
private final Player player;
@ -136,6 +137,14 @@ public class LinearWaypointProvider implements WaypointProvider {
location.getBlockZ());
}
@Override
public Waypoint getCurrentWaypoint() {
if (waypoints.size() == 0 || !editing)
return null;
normaliseEditingSlot();
return waypoints.get(editingSlot);
}
private Location getPreviousWaypoint(int fromSlot) {
if (waypoints.size() <= 1)
return null;
@ -145,6 +154,10 @@ public class LinearWaypointProvider implements WaypointProvider {
return waypoints.get(fromSlot).getLocation();
}
private void normaliseEditingSlot() {
editingSlot = Math.max(0, Math.min(waypoints.size() - 1, editingSlot));
}
@EventHandler
public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getNPC().equals(npc))
@ -161,6 +174,20 @@ public class LinearWaypointProvider implements WaypointProvider {
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (!event.getPlayer().equals(player))
return;
if (event.getMessage().equalsIgnoreCase("triggers")) {
event.setCancelled(true);
if (!player.hasPermission("citizens.waypoints.triggers")) {
Messaging.sendErrorTr(player, Messages.COMMAND_NO_PERMISSION);
return;
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
TriggerEditPrompt.start(player, LinearWaypointEditor.this);
}
}, 1);
return;
}
if (!event.getMessage().equalsIgnoreCase("toggle path"))
return;
event.setCancelled(true);
@ -197,7 +224,8 @@ public class LinearWaypointProvider implements WaypointProvider {
}
Waypoint element = new Waypoint(at);
waypoints.add(Math.max(0, editingSlot), element);
normaliseEditingSlot();
waypoints.add(editingSlot, element);
if (showPath)
createWaypointMarker(editingSlot, element);
editingSlot = Math.min(editingSlot + 1, waypoints.size());
@ -205,7 +233,7 @@ public class LinearWaypointProvider implements WaypointProvider {
editingSlot + 1, waypoints.size());
} else if (waypoints.size() > 0) {
event.setCancelled(true);
editingSlot = Math.min(0, Math.max(waypoints.size() - 1, editingSlot));
normaliseEditingSlot();
Waypoint waypoint = waypoints.remove(editingSlot);
if (showPath)
removeWaypointMarker(waypoint);
@ -298,14 +326,15 @@ public class LinearWaypointProvider implements WaypointProvider {
public void onNavigationComplete(NavigationCompleteEvent event) {
if (selector == null || !event.getNavigator().equals(getNavigator()))
return;
Waypoint from = currentDestination;
selector.finish();
Location finished = event.getNavigator().getTargetAsLocation();
if (finished == null || currentDestination == null)
if (finished == null || from == null)
return;
if (finished.getWorld() != currentDestination.getLocation().getWorld())
if (finished.getWorld() != from.getLocation().getWorld())
return;
if (finished.equals(currentDestination.getLocation()))
currentDestination.onReach(npc);
if (finished.equals(from.getLocation()))
from.onReach(npc);
}
public void onProviderChanged() {

View File

@ -1,17 +0,0 @@
package net.citizensnpcs.trait.waypoint;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import org.bukkit.Location;
public class TeleportWaypointTrigger implements WaypointTrigger {
@Persist
private Location location;
@Override
public void onWaypointReached(NPC npc, Location waypoint) {
if (location != null)
npc.getBukkitEntity().teleport(waypoint);
}
}

View File

@ -8,7 +8,6 @@ import net.citizensnpcs.api.ai.GoalSelector;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -23,8 +22,8 @@ public class WanderWaypointProvider implements WaypointProvider {
private int xrange, yrange;
@Override
public Editor createEditor(Player player) {
return new Editor() {
public WaypointEditor createEditor(Player player) {
return new WaypointEditor() {
@Override
public void begin() {
// TODO Auto-generated method stub

View File

@ -1,13 +1,18 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Collections;
import java.util.List;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTrigger;
import net.citizensnpcs.trait.waypoint.triggers.WaypointTriggerRegistry;
import org.bukkit.Location;
import com.google.common.collect.Lists;
public class Waypoint {
@Persist(required = true)
private Location location;
@ -21,10 +26,21 @@ public class Waypoint {
location = at;
}
public void addTrigger(WaypointTrigger trigger) {
if (triggers == null)
triggers = Lists.newArrayList();
triggers.add(trigger);
}
public Location getLocation() {
return location;
}
@SuppressWarnings("unchecked")
public List<WaypointTrigger> getTriggers() {
return triggers == null ? Collections.EMPTY_LIST : triggers;
}
public void onReach(NPC npc) {
if (triggers == null)
return;

View File

@ -0,0 +1,9 @@
package net.citizensnpcs.trait.waypoint;
import net.citizensnpcs.editor.Editor;
public abstract class WaypointEditor extends Editor {
public Waypoint getCurrentWaypoint() {
return null;
}
}

View File

@ -2,20 +2,19 @@ package net.citizensnpcs.trait.waypoint;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import org.bukkit.entity.Player;
public interface WaypointProvider {
/**
* Creates an {@link Editor} with the given {@link Player}.
* Creates an {@link WaypointEditor} with the given {@link Player}.
*
* @param player
* The player to link the editor with
* @return The editor
*/
public Editor createEditor(Player player);
public WaypointEditor createEditor(Player player);
/**
* Returns whether this provider has paused execution of waypoints.

View File

@ -1,29 +0,0 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Map;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.persistence.Persister;
import net.citizensnpcs.api.util.DataKey;
import com.google.common.collect.Maps;
public class WaypointTriggerRegistry implements Persister {
@Override
public Object create(DataKey root) {
String type = root.getString("type");
Class<? extends WaypointTrigger> clazz = triggers.get(type);
return clazz == null ? null : PersistenceLoader.load(clazz, root);
}
@Override
public void save(Object instance, DataKey root) {
PersistenceLoader.save(instance, root);
}
private static final Map<String, Class<? extends WaypointTrigger>> triggers = Maps.newHashMap();
static {
triggers.put("teleport", TeleportWaypointTrigger.class);
triggers.put("delay", DelayWaypointTrigger.class);
}
}

View File

@ -1,16 +1,30 @@
package net.citizensnpcs.trait.waypoint;
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.trait.waypoint.WaypointProvider;
import net.citizensnpcs.trait.waypoint.Waypoints;
import org.bukkit.Bukkit;
import org.bukkit.Location;
public class DelayWaypointTrigger implements WaypointTrigger {
public class DelayTrigger implements WaypointTrigger {
@Persist
private int delay = 0;
public DelayTrigger() {
}
public DelayTrigger(int delay) {
this.delay = delay;
}
@Override
public String description() {
return String.format("Delay for %d ticks", delay);
}
@Override
public void onWaypointReached(NPC npc, Location waypoint) {
if (delay > 0)
@ -26,8 +40,4 @@ public class DelayWaypointTrigger implements WaypointTrigger {
}
}, delay);
}
public void setDelay(int newDelay) {
delay = newDelay;
}
}

View File

@ -0,0 +1,22 @@
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.NumericPrompt;
import org.bukkit.conversations.Prompt;
public class DelayTriggerPrompt extends NumericPrompt implements WaypointTriggerPrompt {
@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
int delay = Math.max(input.intValue(), 0);
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new DelayTrigger(delay));
return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY);
}
@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.DELAY_TRIGGER_PROMPT);
}
}

View File

@ -0,0 +1,30 @@
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import org.bukkit.Location;
public class TeleportTrigger implements WaypointTrigger {
@Persist(required = true)
private Location location;
public TeleportTrigger() {
}
public TeleportTrigger(Location location) {
this.location = location;
}
@Override
public String description() {
return String.format("Teleport to [%s, %d, %d, %d]", location.getWorld().getName(),
location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
@Override
public void onWaypointReached(NPC npc, Location waypoint) {
if (location != null)
npc.getBukkitEntity().teleport(waypoint);
}
}

View File

@ -0,0 +1,58 @@
package net.citizensnpcs.trait.waypoint.triggers;
import java.util.regex.Pattern;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.RegexPrompt;
import org.bukkit.entity.Player;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
public class TeleportTriggerPrompt extends RegexPrompt implements WaypointTriggerPrompt {
public TeleportTriggerPrompt() {
super(PATTERN);
}
@Override
protected Prompt acceptValidatedInput(ConversationContext context, String input) {
input = input.trim();
if (input.equalsIgnoreCase("back"))
return (Prompt) context.getSessionData("previous");
if (input.equalsIgnoreCase("here")) {
Player player = (Player) context.getForWhom();
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY,
new TeleportTrigger(player.getLocation()));
return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY);
}
String[] parts = Iterables.toArray(Splitter.on(':').split(input), String.class);
String worldName = parts[0];
World world = Bukkit.getWorld(worldName);
if (world == null) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(), Messages.WORLD_NOT_FOUND);
return this;
}
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new Location(world, x, y, z));
return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY);
}
@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.WAYPOINT_TRIGGER_TELEPORT_PROMPT);
}
private static final Pattern PATTERN = Pattern.compile("here|back|[\\p{L}]+?:[0-9]+?:[0-9]+?:[0-9]+?",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}

View File

@ -0,0 +1,51 @@
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.trait.waypoint.WaypointEditor;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
public class TriggerAddPrompt extends StringPrompt {
private final WaypointEditor editor;
public TriggerAddPrompt(WaypointEditor editor) {
this.editor = editor;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
input = input.toLowerCase().trim();
if (input.equalsIgnoreCase("back"))
return (Prompt) context.getSessionData("previous");
Prompt prompt = WaypointTriggerRegistry.getTriggerPromptFrom(input);
if (prompt == null) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_EDITOR_INVALID_TRIGGER, input);
return this;
}
return prompt;
}
@Override
public String getPromptText(ConversationContext context) {
WaypointTrigger returned = (WaypointTrigger) context
.getSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY);
if (returned != null) {
if (editor.getCurrentWaypoint() != null) {
editor.getCurrentWaypoint().addTrigger(returned);
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, null);
Messaging.sendTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_ADDED_SUCCESSFULLY, returned.description());
} else
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_EDITOR_INACTIVE);
}
context.setSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY, this);
return Messaging.tr(Messages.WAYPOINT_TRIGGER_ADD_PROMPT,
WaypointTriggerRegistry.describeValidTriggerNames());
}
}

View File

@ -0,0 +1,56 @@
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.trait.waypoint.Waypoint;
import net.citizensnpcs.trait.waypoint.WaypointEditor;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
public class TriggerEditPrompt extends StringPrompt {
private final WaypointEditor editor;
public TriggerEditPrompt(WaypointEditor editor) {
this.editor = editor;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
input = input.toLowerCase().trim();
if (input.contains("add"))
return new TriggerAddPrompt(editor);
if (input.contains("remove"))
return new TriggerRemovePrompt(editor);
return this;
}
@Override
public String getPromptText(ConversationContext context) {
context.setSessionData("previous", this);
context.setSessionData("exit", false);
if (editor.getCurrentWaypoint() == null)
return Messaging.tr(Messages.WAYPOINT_TRIGGER_EDITOR_INACTIVE);
String base = Messaging.tr(Messages.WAYPOINT_TRIGGER_EDITOR_PROMPT);
Waypoint waypoint = editor.getCurrentWaypoint();
for (WaypointTrigger trigger : waypoint.getTriggers()) {
base += "\n - " + trigger.description();
}
Messaging.send((CommandSender) context.getForWhom(), base);
return "";
}
public static void start(Player player, WaypointEditor editor) {
final Conversation conversation = new ConversationFactory(CitizensAPI.getPlugin())
.withLocalEcho(false).withEscapeSequence("exit").withModality(false)
.withFirstPrompt(new TriggerEditPrompt(editor)).buildConversation(player);
conversation.begin();
}
}

View File

@ -0,0 +1,66 @@
package net.citizensnpcs.trait.waypoint.triggers;
import java.util.List;
import net.citizensnpcs.trait.waypoint.WaypointEditor;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
public class TriggerRemovePrompt extends StringPrompt {
private final WaypointEditor editor;
public TriggerRemovePrompt(WaypointEditor editor) {
this.editor = editor;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase("back"))
return (Prompt) context.getSessionData("previous");
if (editor.getCurrentWaypoint() == null) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_EDITOR_INACTIVE);
return this;
}
int index = 0;
try {
index = Math.max(0, Integer.parseInt(input) - 1);
} catch (NumberFormatException e) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_REMOVE_INVALID_NUMBER);
return this;
}
List<WaypointTrigger> triggers = editor.getCurrentWaypoint().getTriggers();
if (index < triggers.size() || index >= triggers.size()) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_REMOVE_INDEX_OUT_OF_RANGE, triggers.size());
} else {
triggers.remove(index);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_REMOVE_REMOVED,
index + 1);
}
return this;
}
@Override
public String getPromptText(ConversationContext context) {
if (editor.getCurrentWaypoint() == null) {
Messaging.sendErrorTr((CommandSender) context.getForWhom(),
Messages.WAYPOINT_TRIGGER_EDITOR_INACTIVE);
return "";
}
String root = Messaging.tr(Messages.WAYPOINT_TRIGGER_REMOVE_PROMPT);
int i = 1;
for (WaypointTrigger trigger : editor.getCurrentWaypoint().getTriggers()) {
root += String.format("<br> %d. " + trigger.description(), i++);
}
Messaging
.sendTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_REMOVE_PROMPT + root);
return "";
}
}

View File

@ -1,9 +1,11 @@
package net.citizensnpcs.trait.waypoint;
package net.citizensnpcs.trait.waypoint.triggers;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Location;
public interface WaypointTrigger {
String description();
void onWaypointReached(NPC npc, Location waypoint);
}

View File

@ -0,0 +1,19 @@
package net.citizensnpcs.trait.waypoint.triggers;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
/**
* Marker interface for waypoint trigger prompts.
*
* Prompts are expected to return to the prompt specified under the
* {@link #RETURN_PROMPT_KEY} key in the {@link ConversationContext} and to set
* the specified trigger under {@link #CREATED_TRIGGER_KEY} prior to returning.
*
* If the returned trigger is <code>null</code> then the prompt is assumed to
* have failed and an error message will be displayed.
*/
public interface WaypointTriggerPrompt extends Prompt {
static final String CREATED_TRIGGER_KEY = "created-trigger";
static final String RETURN_PROMPT_KEY = "return-to";
}

View File

@ -0,0 +1,55 @@
package net.citizensnpcs.trait.waypoint.triggers;
import java.util.Map;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.persistence.Persister;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.conversations.Prompt;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
public class WaypointTriggerRegistry implements Persister {
@Override
public Object create(DataKey root) {
String type = root.getString("type");
Class<? extends WaypointTrigger> clazz = triggers.get(type);
return clazz == null ? null : PersistenceLoader.load(clazz, root);
}
@Override
public void save(Object 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);
triggerPrompts.put(name, promptClass);
}
public static String describeValidTriggerNames() {
return Joiner.on(", ").join(triggerPrompts.keySet());
}
public static Prompt getTriggerPromptFrom(String input) {
Class<? extends Prompt> promptClass = triggerPrompts.get(input);
if (promptClass == null)
return null;
try {
return promptClass.newInstance();
} catch (Exception e) {
return null;
}
}
static {
addTrigger("teleport", TeleportTrigger.class, TeleportTriggerPrompt.class);
addTrigger("delay", DelayTrigger.class, DelayTriggerPrompt.class);
}
}

View File

@ -37,14 +37,18 @@ public class Messages {
public static final String COMMAND_MUST_BE_INGAME = "citizens.commands.requirements.must-be-ingame";
public static final String COMMAND_MUST_BE_OWNER = "citizens.commands.requirements.must-be-owner";
public static final String COMMAND_MUST_HAVE_SELECTED = "citizens.commands.requirements.must-have-selected";
public static final String COMMAND_NO_PERMISSION = "citizens.commands.requirements.missing-permission";
public static final String COMMAND_PAGE_MISSING = "citizens.commands.page-missing";
public static final String COMMAND_REPORT_ERROR = "citizens.commands.console-error";
public static final String COMMAND_REQUIREMENTS_INVALID_MOB_TYPE = "citizens.commands.requirements.disallowed-mobtype";
public static final String COMMAND_SAVE_HELP = "citizens.commands.citizens.save.help";
public static final String COMMAND_TOO_FEW_ARGUMENTS = "citizens.commands.requirements.too-few-arguments";
public static final String COMMAND_TOO_MANY_ARGUMENTS = "citizens.commands.requirements.too-many-arguments";
public static final String CONTROLLABLE_REMOVED = "citizens.commands.npc.controllable.removed";
public static final String CONTROLLABLE_SET = "citizens.commands.npc.controllable.set";
public static final String CURRENT_WAYPOINT_PROVIDER = "citizens.waypoints.current-provider";
public static final String DATABASE_CONNECTION_FAILED = "citizens.notifications.database-connection-failed";
public static final String DELAY_TRIGGER_PROMPT = "citizens.editors.waypoints.triggers.delay.prompt";
public static final String EQUIPMENT_EDITOR_ALL_ITEMS_REMOVED = "citizens.editors.equipment.all-items-removed";
public static final String EQUIPMENT_EDITOR_BEGIN = "citizens.editors.equipment.begin";
public static final String EQUIPMENT_EDITOR_END = "citizens.editors.equipment.end";
@ -74,6 +78,7 @@ public class Messages {
public static final String INVALID_POSE_NAME = "citizens.commands.npc.pose.invalid-name";
public static final String INVALID_PROFESSION = "citizens.commands.npc.profession.invalid-profession";
public static final String INVALID_SPAWN_LOCATION = "citizens.commands.npc.create.invalid-location";
public static final String INVALID_TRIGGER_TELEPORT_FORMAT = "citizens.editors.waypoints.triggers.teleport.invalid-format";
public static final String LINEAR_WAYPOINT_EDITOR_ADDED_WAYPOINT = "citizens.editors.waypoints.linear.added-waypoint";
public static final String LINEAR_WAYPOINT_EDITOR_BEGIN = "citizens.editors.waypoints.linear.begin";
public static final String LINEAR_WAYPOINT_EDITOR_EDIT_SLOT_SET = "citizens.editors.waypoints.linear.edit-slot-set";
@ -97,7 +102,6 @@ public class Messages {
public static final String MONEY_WITHDRAWN = "citizens.economy.money-withdrawn";
public static final String MOVETO_FORMAT = "citizens.commands.npc.moveto.format";
public static final String MOVETO_TELEPORTED = "citizens.commands.npc.moveto.teleported";
public static final String MOVETO_WORLD_NOT_FOUND = "citizens.commands.npc.moveto.missing-world";
public static final String NO_NPC_WITH_ID_FOUND = "citizens.commands.npc.spawn.missing-npc-id";
public static final String NO_STORED_SPAWN_LOCATION = "citizens.commands.npc.spawn.no-location";
public static final String NOT_LIVING_MOBTYPE = "citizens.commands.npc.create.not-living-mobtype";
@ -182,5 +186,16 @@ public class Messages {
public static final String VULNERABLE_STOPPED = "citizens.commands.npc.vulnerable.stopped";
public static final String WAYPOINT_PROVIDER_SET = "citizens.waypoints.set-provider";
public static final String WAYPOINT_TELEPORTING_DISABLED = "citizens.commands.waypoints.disableteleporting.disabled";
public static final String WAYPOINT_TRIGGER_ADD_PROMPT = "citizens.editors.waypoints.triggers.add.prompt";
public static final String WAYPOINT_TRIGGER_ADDED_SUCCESSFULLY = "citizens.editors.waypoints.triggers.add.added";
public static final String WAYPOINT_TRIGGER_EDITOR_INACTIVE = "citizens.editors.waypoints.triggers.main.missing-waypoint";
public static final String WAYPOINT_TRIGGER_EDITOR_INVALID_TRIGGER = "citizens.editors.waypoints.triggers.add.invalid-trigger";
public static final String WAYPOINT_TRIGGER_EDITOR_PROMPT = "citizens.editors.waypoints.triggers.main.prompt";
public static final String WAYPOINT_TRIGGER_REMOVE_INDEX_OUT_OF_RANGE = "citizens.editors.waypoints.triggers.remove.index-out-of-range";
public static final String WAYPOINT_TRIGGER_REMOVE_INVALID_NUMBER = "citizens.editors.waypoints.triggers.remove.not-a-number";
public static final String WAYPOINT_TRIGGER_REMOVE_PROMPT = "citizens.editors.waypoints.triggers.remove.prompt";
public static final String WAYPOINT_TRIGGER_REMOVE_REMOVED = "citizens.editors.waypoints.triggers.remove.removed";
public static final String WAYPOINT_TRIGGER_TELEPORT_PROMPT = "citizens.editors.waypoints.triggers.teleport.prompt";
public static final String WORLD_NOT_FOUND = "citizens.commands.errors.missing-world";
public static final String WRITING_DEFAULT_SETTING = "citizens.settings.writing-default";
}

View File

@ -48,6 +48,8 @@ public class NMS {
private static Field THREAD_STOPPER;
public static void addOrRemoveFromPlayerList(LivingEntity bukkitEntity, boolean remove) {
if (bukkitEntity == null)
return;
EntityLiving handle = ((CraftLivingEntity) bukkitEntity).getHandle();
if (handle.world == null)
return;
@ -137,6 +139,10 @@ public class NMS {
}
}
public static boolean inWater(EntityLiving mcEntity) {
return mcEntity.I() || mcEntity.J();
}
public static void look(ControllerLook controllerLook, EntityLiving handle, EntityLiving target) {
controllerLook.a(target, 10.0F, handle.bf());
}
@ -276,8 +282,4 @@ public class NMS {
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
}
}
public static boolean inWater(EntityLiving mcEntity) {
return mcEntity.I() || mcEntity.J();
}
}

View File

@ -41,7 +41,7 @@ citizens.commands.npc.lookclose.set=[[{0}]] will now rotate when players are nea
citizens.commands.npc.lookclose.stopped=[[{0}]] will no longer rotate when players are nearby.
citizens.commands.npc.mount.failed=Couldn''t mount [[{0}]].
citizens.commands.npc.moveto.format=Format is x:y:z(:world) or x y z( world).
citizens.commands.npc.moveto.missing-world=World not found.
citizens.commands.errors.missing-world=World not found.
citizens.commands.npc.moveto.teleported=[[{0}]] teleported to {1}.
citizens.commands.npc.owner.already-owner={0} is already the owner of {1}.
citizens.commands.npc.owner.owner=[[{0}]]''s owner is [[{1}]].
@ -77,6 +77,9 @@ citizens.commands.npc.tphere.teleported=[[{0}]] was teleported to your location.
citizens.commands.npc.vulnerable.set=[[{0}]] is now vulnerable.
citizens.commands.npc.vulnerable.stopped=[[{0}]] is no longer vulnerable.
citizens.commands.page-missing=The page [[{0}]] does not exist.
citizens.commands.requirements.too-few-arguments=Too few arguments.
citizens.commands.requirements.too-many-arguments=Too many arguments.
citizens.commands.requirements.missing-permission=You don't have permission to execute that command.
citizens.commands.requirements.disallowed-mobtype=The NPC cannot be the mob type {0} to use that command.
citizens.commands.requirements.missing-required-trait=Missing required trait {0}.
citizens.commands.requirements.must-be-ingame=You must be ingame to use that command.
@ -131,8 +134,20 @@ citizens.editors.text.realistic-looking-set=[[Realistic looking]] set to [[{0}]]
citizens.editors.text.remove-prompt=Enter the index of the entry you wish to remove or [[page]] to view more pages.
citizens.editors.text.removed-entry=[[Removed]] entry at index [[{0}]].
citizens.editors.text.start-prompt=Type [[add]] to add an entry, [[edit]] to edit entries, [[remove]] to remove entries, [[close]] to toggle the NPC as a close talker, and [[random]] to toggle the NPC as a random talker. Type [[help]] to show this again.
citizens.editors.waypoints.triggers.remove.not-a-number=Index must be a number.
citizens.editors.waypoints.triggers.remove.removed=Successfully removed trigger {0}.
citizens.editors.waypoints.triggers.remove.index-out-of-range=Index must be in the range [[1-{0}]].
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.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.editors.waypoints.triggers.teleport.invalid-format=Invalid location given. Format is [[world]]:[[x]]:[[y]]:[[z]].
citizens.editors.waypoints.triggers.delay.prompt=Enter the delay in [[server ticks]] to use. (20 ticks = 1 second)
citizens.editors.waypoints.triggers.main.prompt=<b>Entered the waypoint trigger editor.<br> Type [[add]] to begin adding triggers and [[remove]] to remove triggers.<br><br> Current triggers are:
citizens.editors.waypoints.triggers.main.missing-waypoint=Not editing a waypoint.
citizens.editors.waypoints.triggers.add.added=<b>[[Added]] waypoint trigger successfully ({0}).
citizens.editors.waypoints.triggers.add.invalid-trigger=Couldn''t create a trigger by the name [[{0}]].
citizens.editors.waypoints.triggers.add.prompt=Enter in a trigger name to add or type [[back]] to return to the edit prompt. Valid trigger names are {0}.
citizens.editors.waypoints.linear.added-waypoint=[[Added]] a waypoint at ({0}) ([[{1}]], [[{2}]])
citizens.editors.waypoints.linear.begin=<b>Entered the linear waypoint editor!<br>[[Left click]] to add a waypoint, [[right click]] to remove.<br>Type [[toggle path]] to toggle showing entities at waypoints.
citizens.editors.waypoints.linear.begin=<b>Entered the linear waypoint editor!<br>[[Left click]] to add a waypoint, [[right click]] to remove.<br>Type [[toggle path]] to toggle showing entities at waypoints. Type [[triggers]] to begin editing waypoint triggers.
citizens.editors.waypoints.linear.edit-slot-set=Editing slot set to [[{0}]] ({1}).
citizens.editors.waypoints.linear.end=Exited the linear waypoint editor.
citizens.editors.waypoints.linear.not-showing-markers=[[Stopped]] showing waypoint markers.