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.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
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);
@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() {
private final WaypointMarkers markers = new WaypointMarkers(player.getWorld());
private boolean showPath;
@ -188,13 +194,13 @@ public class GuidedWaypointProvider implements WaypointProvider {
tree = PRTree.create(new Region3D.Converter<Waypoint>(), 30);
tree.load(Lists.newArrayList(Iterables.transform(Iterables.<Waypoint> concat(available, helpers),
new Function<Waypoint, Region3D<Waypoint>>() {
@Override
public Region3D<Waypoint> apply(Waypoint arg0) {
Location loc = arg0.getLocation();
Vector root = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
return new Region3D<Waypoint>(root, root, arg0);
}
})));
@Override
public Region3D<Waypoint> apply(Waypoint arg0) {
Location loc = arg0.getLocation();
Vector root = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
return new Region3D<Waypoint>(root, root, arg0);
}
})));
}
@Override

View File

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

View File

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