Solved Problem turning multiple args into one string

Discussion in 'Spigot Plugin Development' started by jckswk, May 30, 2016.

  1. So, I'm making a /request plugin that people can /request something like an item and then a trade or something can be made in game. The command is /request add <message>. The only problem is I'm having trouble turing all args after "add" into a string. I want to make it into a string so I can save it to the config.

    COMMAND CODE
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("request")){
                int al = args.length;
                if(al >= 2){
                    if(args[0].equals("add")){

                        String string = null;
                       
                        for (string = string + " " + args[al]; al <= 0; al = al-1) {

                            Bukkit.getLogger().info(string);
                            return true;

                            }
                       
                       
                    }


                }
            }
            return false;
        }
    ERROR
    Code (Text):
    [00:12:35 WARN]: Unexpected exception while parsing console command "request add testing this out"
    org.bukkit.command.CommandException: Unhandled exception executing command 'request' in plugin jckswkKitpvp v0.01
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchServerCommand(CraftServer.java:632) [craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at net.minecraft.server.v1_9_R2.DedicatedServer.aL(DedicatedServer.java:437) [craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:400) [craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
        at me.jckswk.requests.Main.onCommand(Main.java:41) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-8a048fe-d8637df]
        ... 8 more
    >
     
    Thanks!
     
  2. Java is zero based indexing, you'll always be grabbing data one outside of the array. Setting the 'string' variable to null isn't a good idea, set it to a blank String instead.
     
  3. I must say that was a very creative approach to the problem. I enjoyed reading your attempted solution, I've never seen this approach before :)

    I would start the loop at the beginning though and to keep a bit of stress off the garbage collector we can use a StringBuilder.

    Initialize a string builder with the first word
    Code (Text):
    StringBuilder sb = new StringBuilder(args[1]);
    Now we can just loop over the rest adding a space followed by the next word. This for loop looks a bit more common then your backwards almost recursive looking concatenation :p
    Code (Text):
    for (int i = 2; i < al; i++) {
        sb.append(' ').append(args[i]);
    }
    Then simply build your string
    Code (Text):
    sb.toString(); //This is the constructed string
     
  4. Rewrite your plugin.
    Code (Java):
    String string = "";
    for(int i = 0; i < args.length; i++) {
       string += args[i] + " ";
    }
    // or this
    Code (Java):
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < args.length; i++) {
       builder.append(args[i] + " ");
    }
    String msg = builder.toString();
     
  5. You're starting with the argument that is in the list length index (which won't work since Java starts at 0) and then looping while "al" is less than or equal to 0 which will infinitely loop.

    Also, use a StringBuilder instead.

    Edit: sniped like 3 times lol
     
  6. you can either use StringBuilder or just a string the user will decide what's he going to use.
     
  7. Just for future reference if you are going to use a StringBuilder go all out :p
    Code (Text):
    builder.append(args[i]).append(" ");
     
    • Friendly Friendly x 1
  8. Just want to say thanks for you kind words, and this worked