use of net.citizensnpcs.api.command.exception.WrappedCommandException 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.WrappedCommandException 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());
}
}
Aggregations