Search in sources :

Example 1 with WrappedCommandException

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;
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) CommandUsageException(net.citizensnpcs.api.command.exception.CommandUsageException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) CommandSender(org.bukkit.command.CommandSender) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException)

Example 2 with WrappedCommandException

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());
    }
}
Also used : WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) Method(java.lang.reflect.Method) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) CommandUsageException(net.citizensnpcs.api.command.exception.CommandUsageException) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException)

Aggregations

CommandException (net.citizensnpcs.api.command.exception.CommandException)2 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)2 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)2 UnhandledCommandException (net.citizensnpcs.api.command.exception.UnhandledCommandException)2 WrappedCommandException (net.citizensnpcs.api.command.exception.WrappedCommandException)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)1 NPC (net.citizensnpcs.api.npc.NPC)1 CommandSender (org.bukkit.command.CommandSender)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1