Not sure how to format command

Discussion in 'Plugin Development' started by Blir, Aug 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Blir

    I'm working on a cross-server chat plugin called ConvoSync: http://dev.bukkit.org/bukkit-plugins/convosync/

    I included a command in its most recent build for executing commands cross-server, /ccmd.
    However, I quickly realized that it wouldn't work with server's that have spaces in their names, e.g.:

    /ccmd FTB Unhinged say hi

    Because "FTB" and "Unhinged" would be separated as two separate arguments. I was wondering what the best solution to this would be. I've thought of a couple, and I can't decide between them.

    Using flags: /ccmd FTB Unhinged -c say hi
    Or using quotes: /ccmd "FTB Unhinged" say hi

    Could anyone tell me which solution they think would be better, or even suggest another solution?

    The command usage currently:
    /ccmd <server on which to execute command> <command> [any other arguments]
     
  2. Offline

    Robotia

    Blir
    I have an easy solution for you! ;)
    To do spaces, you could have underscores (_)
    So if the world was FTB_server_main_overworld, it would enter args[0]
    You'll need this code:
    args[0] = args[0].replaceAll("_", " ");
     
  3. Offline

    afistofirony

    An alternate method for that is to use quotes like WorldEdit does. This way you don't run into issues with worlds with underscores (world_nether) as you would with Robotia's method.

    An example command would be /ccmd "FTB Unhinged" say hi, just as you suggested.

    Try something like this:

    Code:
    public ArrayList<String> processQuotes (String[] args) {
            ArrayList<String> result = new ArrayList<String>();
            for (int i = 0; i < args.length; ++i) {
                    String arg = args[i];
                    switch (arg.charAt(0)) {
                        case '\'':
                        case '"':
                            String result = "";
                            char closer = arg.charAt(0);
                            int j, lastWord = i;
                            for (j = i; j < args.length; ++j) {
                                String part = args[j];
                                if (part.length() == 0) {
                                    args[lastWord] += " ";
                                    continue;
                                }
     
                                if (part.charAt(part.length() - 1) == closer && part.length() > 1)
                                    break;
     
                                lastWord = j;
                            }
     
                            if (j < args.length) {
                                for (int k = i; k < j; ++k) {
                                    result += args[k];
                                }
     
                                arg = result.substring(1, result.length() - 1);
                            }
                    }
     
                    result.add(arg);
        }
     
        return result;
    }
    Of course, you'd need to use something like
    Code:
    ArrayList<String> args = processQuotes(array);
    args.get(0);
     
Thread Status:
Not open for further replies.

Share This Page