Allow linear waypoint commands to be used from console

This commit is contained in:
fullwall 2014-05-07 16:41:53 +08:00
parent 4835101af4
commit 66099d36af
4 changed files with 39 additions and 21 deletions

View File

@ -27,6 +27,7 @@ import net.citizensnpcs.util.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -50,7 +51,12 @@ public class GuidedWaypointProvider implements WaypointProvider {
private PRTree<Region3D<Waypoint>> tree = PRTree.create(new Region3D.Converter<Waypoint>(), 30); private PRTree<Region3D<Waypoint>> tree = PRTree.create(new Region3D.Converter<Waypoint>(), 30);
@Override @Override
public WaypointEditor createEditor(final Player player, CommandContext args) { public WaypointEditor createEditor(final CommandSender sender, CommandContext args) {
if (!(sender instanceof Player)) {
Messaging.sendErrorTr(sender, Messages.COMMAND_MUST_BE_INGAME);
return null;
}
final Player player = (Player) sender;
return new WaypointEditor() { return new WaypointEditor() {
private final WaypointMarkers markers = new WaypointMarkers(player.getWorld()); private final WaypointMarkers markers = new WaypointMarkers(player.getWorld());
private boolean showPath; private boolean showPath;
@ -188,13 +194,13 @@ public class GuidedWaypointProvider implements WaypointProvider {
tree = PRTree.create(new Region3D.Converter<Waypoint>(), 30); tree = PRTree.create(new Region3D.Converter<Waypoint>(), 30);
tree.load(Lists.newArrayList(Iterables.transform(Iterables.<Waypoint> concat(available, helpers), tree.load(Lists.newArrayList(Iterables.transform(Iterables.<Waypoint> concat(available, helpers),
new Function<Waypoint, Region3D<Waypoint>>() { new Function<Waypoint, Region3D<Waypoint>>() {
@Override @Override
public Region3D<Waypoint> apply(Waypoint arg0) { public Region3D<Waypoint> apply(Waypoint arg0) {
Location loc = arg0.getLocation(); Location loc = arg0.getLocation();
Vector root = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); Vector root = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
return new Region3D<Waypoint>(root, root, arg0); return new Region3D<Waypoint>(root, root, arg0);
} }
}))); })));
} }
@Override @Override

View File

@ -27,6 +27,7 @@ import net.citizensnpcs.util.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation; import org.bukkit.conversations.Conversation;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -44,16 +45,24 @@ public class LinearWaypointProvider implements WaypointProvider {
private final List<Waypoint> waypoints = Lists.newArrayList(); private final List<Waypoint> waypoints = Lists.newArrayList();
@Override @Override
public WaypointEditor createEditor(Player player, CommandContext args) { public WaypointEditor createEditor(CommandSender sender, CommandContext args) {
if (args.hasFlag('h')) { if (args.hasFlag('h')) {
waypoints.add(new Waypoint(player.getLocation())); try {
if (args.getSenderLocation() != null) {
waypoints.add(new Waypoint(args.getSenderLocation()));
}
} catch (CommandException e) {
Messaging.sendError(sender, e.getMessage());
}
return null; return null;
} else if (args.hasValueFlag("at")) { } else if (args.hasValueFlag("at")) {
try { try {
Location location = CommandContext.parseLocation(player.getLocation(), args.getFlag("at")); Location location = CommandContext.parseLocation(args.getSenderLocation(), args.getFlag("at"));
waypoints.add(new Waypoint(location)); if (location != null) {
waypoints.add(new Waypoint(location));
}
} catch (CommandException e) { } catch (CommandException e) {
Messaging.sendError(player, e.getMessage()); Messaging.sendError(sender, e.getMessage());
} }
return null; return null;
} else if (args.hasFlag('c')) { } else if (args.hasFlag('c')) {
@ -67,8 +76,11 @@ public class LinearWaypointProvider implements WaypointProvider {
} else if (args.hasFlag('p')) { } else if (args.hasFlag('p')) {
setPaused(!isPaused()); setPaused(!isPaused());
return null; return null;
} else if (!(sender instanceof CommandSender)) {
Messaging.sendErrorTr(sender, Messages.COMMAND_MUST_BE_INGAME);
return null;
} }
return new LinearWaypointEditor(player); return new LinearWaypointEditor((Player) sender);
} }
@Override @Override
@ -408,7 +420,7 @@ public class LinearWaypointProvider implements WaypointProvider {
Location npcLoc = npc.getEntity().getLocation(cachedLocation); Location npcLoc = npc.getEntity().getLocation(cachedLocation);
if (npcLoc.getWorld() != next.getLocation().getWorld() if (npcLoc.getWorld() != next.getLocation().getWorld()
|| npcLoc.distanceSquared(next.getLocation()) < npc.getNavigator().getLocalParameters() || npcLoc.distanceSquared(next.getLocation()) < npc.getNavigator().getLocalParameters()
.distanceMargin()) { .distanceMargin()) {
return false; return false;
} }
currentDestination = next; currentDestination = next;

View File

@ -8,7 +8,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Player; import org.bukkit.command.CommandSender;
public class WanderWaypointProvider implements WaypointProvider { public class WanderWaypointProvider implements WaypointProvider {
private Goal currentGoal; private Goal currentGoal;
@ -20,7 +20,7 @@ public class WanderWaypointProvider implements WaypointProvider {
private final int yrange = DEFAULT_YRANGE; private final int yrange = DEFAULT_YRANGE;
@Override @Override
public WaypointEditor createEditor(Player player, CommandContext args) { public WaypointEditor createEditor(CommandSender sender, CommandContext args) {
return new WaypointEditor() { return new WaypointEditor() {
@Override @Override
public void begin() { public void begin() {

View File

@ -4,18 +4,18 @@ import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persistable; import net.citizensnpcs.api.persistence.Persistable;
import org.bukkit.entity.Player; import org.bukkit.command.CommandSender;
public interface WaypointProvider extends Persistable { public interface WaypointProvider extends Persistable {
/** /**
* Creates an {@link WaypointEditor} with the given {@link Player}. * Creates an {@link WaypointEditor} with the given {@link CommandSender}.
* *
* @param player * @param sender
* The player to link the editor with * The player to link the editor with
* @param args * @param args
* @return The editor * @return The editor
*/ */
public WaypointEditor createEditor(Player player, CommandContext args); public WaypointEditor createEditor(CommandSender sender, CommandContext args);
/** /**
* Returns whether this provider has paused execution of waypoints. * Returns whether this provider has paused execution of waypoints.