use of net.citizensnpcs.api.command.exception.ServerCommandException in project Citizens2 by CitizensDev.
the class NPCCommands method anchor.
@Command(aliases = { "npc" }, usage = "anchor (--save [name]|--assume [name]|--remove [name]) (-a)(-c)", desc = "Changes/Saves/Lists NPC's location anchor(s)", flags = "ac", modifiers = { "anchor" }, min = 1, max = 3, permission = "citizens.npc.anchor")
public void anchor(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Anchors trait = npc.getTrait(Anchors.class);
if (args.hasValueFlag("save")) {
if (args.getFlag("save").isEmpty())
throw new CommandException(Messages.INVALID_ANCHOR_NAME);
if (args.getSenderLocation() == null)
throw new ServerCommandException();
if (args.hasFlag('c')) {
if (trait.addAnchor(args.getFlag("save"), args.getSenderTargetBlockLocation())) {
Messaging.sendTr(sender, Messages.ANCHOR_ADDED);
} else
throw new CommandException(Messages.ANCHOR_ALREADY_EXISTS, args.getFlag("save"));
} else {
if (trait.addAnchor(args.getFlag("save"), args.getSenderLocation())) {
Messaging.sendTr(sender, Messages.ANCHOR_ADDED);
} else
throw new CommandException(Messages.ANCHOR_ALREADY_EXISTS, args.getFlag("save"));
}
} else if (args.hasValueFlag("assume")) {
if (args.getFlag("assume").isEmpty())
throw new CommandException(Messages.INVALID_ANCHOR_NAME);
Anchor anchor = trait.getAnchor(args.getFlag("assume"));
if (anchor == null)
throw new CommandException(Messages.ANCHOR_MISSING, args.getFlag("assume"));
npc.teleport(anchor.getLocation(), TeleportCause.COMMAND);
} else if (args.hasValueFlag("remove")) {
if (args.getFlag("remove").isEmpty())
throw new CommandException(Messages.INVALID_ANCHOR_NAME);
if (trait.removeAnchor(trait.getAnchor(args.getFlag("remove"))))
Messaging.sendTr(sender, Messages.ANCHOR_REMOVED);
else
throw new CommandException(Messages.ANCHOR_MISSING, args.getFlag("remove"));
} else if (!args.hasFlag('a')) {
Paginator paginator = new Paginator().header("Anchors");
paginator.addLine("<e>Key: <a>ID <b>Name <c>World <d>Location (X,Y,Z)");
for (int i = 0; i < trait.getAnchors().size(); i++) {
if (trait.getAnchors().get(i).isLoaded()) {
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> " + trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> " + trait.getAnchors().get(i).getLocation().getBlockX() + ", " + trait.getAnchors().get(i).getLocation().getBlockY() + ", " + trait.getAnchors().get(i).getLocation().getBlockZ();
paginator.addLine(line);
} else {
String[] parts = trait.getAnchors().get(i).getUnloadedValue();
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> " + parts[0] + "<d> " + parts[1] + ", " + parts[2] + ", " + parts[3] + " <f>(unloaded)";
paginator.addLine(line);
}
}
int page = args.getInteger(1, 1);
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
// Assume Player's position
if (!args.hasFlag('a'))
return;
if (sender instanceof ConsoleCommandSender)
throw new ServerCommandException();
npc.teleport(args.getSenderLocation(), TeleportCause.COMMAND);
}
use of net.citizensnpcs.api.command.exception.ServerCommandException in project Citizens2 by CitizensDev.
the class NPCCommands method select.
@Command(aliases = { "npc" }, usage = "select|sel [id|name] (--r range)", desc = "Select a NPC with the given ID or name", modifiers = { "select", "sel" }, min = 1, max = 2, permission = "citizens.npc.select")
@Requirements
public void select(CommandContext args, final CommandSender sender, final NPC npc) throws CommandException {
NPCCommandSelector.Callback callback = new NPCCommandSelector.Callback() {
@Override
public void run(NPC toSelect) throws CommandException {
if (toSelect == null)
throw new CommandException(Messages.NPC_NOT_FOUND);
if (npc != null && toSelect.getId() == npc.getId())
throw new CommandException(Messages.NPC_ALREADY_SELECTED);
selector.select(sender, toSelect);
Messaging.sendWithNPC(sender, Setting.SELECTION_MESSAGE.asString(), toSelect);
}
};
if (args.argsLength() <= 1) {
if (!(sender instanceof Player))
throw new ServerCommandException();
double range = Math.abs(args.getFlagDouble("r", 10));
Entity player = (Player) sender;
final Location location = args.getSenderLocation();
List<Entity> search = player.getNearbyEntities(range, range, range);
Collections.sort(search, new Comparator<Entity>() {
@Override
public int compare(Entity o1, Entity o2) {
double d = o1.getLocation().distanceSquared(location) - o2.getLocation().distanceSquared(location);
return d > 0 ? 1 : d < 0 ? -1 : 0;
}
});
for (Entity possibleNPC : search) {
NPC test = npcRegistry.getNPC(possibleNPC);
if (test == null)
continue;
callback.run(test);
break;
}
} else {
NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1));
}
}
use of net.citizensnpcs.api.command.exception.ServerCommandException in project Citizens2 by CitizensDev.
the class NPCCommandSelector method acceptValidatedInput.
@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
boolean found = false;
for (NPC npc : choices) {
if (input.intValue() == npc.getId()) {
found = true;
break;
}
}
CommandSender sender = (CommandSender) context.getForWhom();
if (!found) {
Messaging.sendErrorTr(sender, Messages.SELECTION_PROMPT_INVALID_CHOICE, input);
return this;
}
NPC toSelect = CitizensAPI.getNPCRegistry().getById(input.intValue());
try {
callback.run(toSelect);
} catch (ServerCommandException ex) {
Messaging.sendTr(sender, CommandMessages.MUST_BE_INGAME);
} catch (CommandUsageException ex) {
Messaging.sendError(sender, ex.getMessage());
Messaging.sendError(sender, ex.getUsage());
} catch (UnhandledCommandException ex) {
ex.printStackTrace();
} catch (WrappedCommandException ex) {
ex.getCause().printStackTrace();
} catch (CommandException ex) {
Messaging.sendError(sender, ex.getMessage());
} catch (NumberFormatException ex) {
Messaging.sendErrorTr(sender, CommandMessages.INVALID_NUMBER);
}
return null;
}
use of net.citizensnpcs.api.command.exception.ServerCommandException in project CitizensAPI by CitizensDev.
the class CommandManager method executeMethod.
// Attempt to execute a command.
private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException {
String cmdName = args[0].toLowerCase();
String modifier = args.length > 1 ? args[1] : "";
boolean help = modifier.toLowerCase().equals("help");
Method method = commands.get(cmdName + " " + modifier.toLowerCase());
if (method == null && !help) {
method = commands.get(cmdName + " *");
}
if (method == null && help) {
executeHelp(args, sender);
return;
}
if (method == null)
throw new UnhandledCommandException();
if (!serverCommands.contains(method) && sender instanceof ConsoleCommandSender)
throw new ServerCommandException();
if (!hasPermission(method, sender))
throw new NoPermissionsException();
Command cmd = method.getAnnotation(Command.class);
CommandContext context = new CommandContext(sender, args);
if (cmd.requiresFlags() && !context.hasAnyFlags())
throw new CommandUsageException("", getUsage(args, cmd));
if (context.argsLength() < cmd.min())
throw new CommandUsageException(CommandMessages.TOO_FEW_ARGUMENTS, getUsage(args, cmd));
if (cmd.max() != -1 && context.argsLength() > cmd.max())
throw new CommandUsageException(CommandMessages.TOO_MANY_ARGUMENTS, getUsage(args, cmd));
if (!cmd.flags().contains("*")) {
for (char flag : context.getFlags()) if (cmd.flags().indexOf(String.valueOf(flag)) == -1)
throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, cmd));
}
methodArgs[0] = context;
for (Annotation annotation : registeredAnnotations.get(method)) {
CommandAnnotationProcessor processor = annotationProcessors.get(annotation.annotationType());
processor.process(sender, context, annotation, methodArgs);
}
Object instance = instances.get(method);
try {
method.invoke(instance, methodArgs);
} catch (IllegalArgumentException e) {
logger.log(Level.SEVERE, "Failed to execute command", e);
} catch (IllegalAccessException e) {
logger.log(Level.SEVERE, "Failed to execute command", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof CommandException) {
if (e.getCause() instanceof CommandUsageException && ((CommandUsageException) e.getCause()).getUsage() == null) {
((CommandUsageException) e.getCause()).setUsage(getUsage(args, cmd));
}
throw (CommandException) e.getCause();
}
throw new WrappedCommandException(e.getCause());
}
}
use of net.citizensnpcs.api.command.exception.ServerCommandException in project Citizens2 by CitizensDev.
the class NPCCommands method pose.
@Command(aliases = { "npc" }, usage = "pose (--save [name]|--assume [name]|--remove [name]) (-a)", desc = "Changes/Saves/Lists NPC's head pose(s)", flags = "a", modifiers = { "pose" }, min = 1, max = 2, permission = "citizens.npc.pose")
public void pose(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Poses trait = npc.getTrait(Poses.class);
if (args.hasValueFlag("save")) {
if (args.getFlag("save").isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
if (args.getSenderLocation() == null)
throw new ServerCommandException();
if (trait.addPose(args.getFlag("save"), args.getSenderLocation())) {
Messaging.sendTr(sender, Messages.POSE_ADDED);
} else
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("save"));
} else if (args.hasValueFlag("assume")) {
String pose = args.getFlag("assume");
if (pose.isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
if (!trait.hasPose(pose))
throw new CommandException(Messages.POSE_MISSING, pose);
trait.assumePose(pose);
} else if (args.hasValueFlag("remove")) {
if (args.getFlag("remove").isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
if (trait.removePose(args.getFlag("remove"))) {
Messaging.sendTr(sender, Messages.POSE_REMOVED);
} else
throw new CommandException(Messages.POSE_MISSING, args.getFlag("remove"));
} else if (!args.hasFlag('a')) {
trait.describe(sender, args.getInteger(1, 1));
}
// Assume Player's pose
if (!args.hasFlag('a'))
return;
if (args.getSenderLocation() == null)
throw new ServerCommandException();
Location location = args.getSenderLocation();
trait.assumePose(location);
}
Aggregations