Mutiple args to one arg

Discussion in 'Plugin Development' started by lbjdaking23, Aug 13, 2011.

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

    lbjdaking23

    Is it possible to have multiple args to one?
    Like if someone put a song like
    Lil wayne Feat Drake - She Will
    Then that would be one arg but the thing is songs would be different.
    Also this is for a request system for music.
     
  2. Is that really so hard to figure out yourself?

    Hints (open)

    Loop through the arguments
    Another hint (open)

    Append every argument to a String
    Yet another one (open)

    Seperate the args with spaces



    Try to think about it by yourself next time.
     
    wwsean08 likes this.
  3. Offline

    Kaikz

    PHP:
    string str;
    foreach (
    string arg in args) {
        
    str str arg " ";
    }
     
  4. Offline

    Shamebot

    Without having put this in an IDE, I think you must initialize the string with "" when defining it.
     
  5. Offline

    feildmaster

    that's a PHP code. :D

    Code:
            String str = "";
            for (String arg : args) {
                str += arg + " ";
            }
     
  6. Offline

    Kaikz

    The PHP tags highlight Java code.
    eh, I must be thinking C# too much.
     
  7. Offline

    feildmaster

    @Kaikz ah right, when i think about it... PHP doesn't have syntax like that either. :p

    also, if you want java syntax use this:
    Code:
    [syntax=java]code here[/syntax]
     
  8. Offline

    cholo71796

    Maybe add a str = str.trim() at the end- or just remove the last character
     
  9. Offline

    desht

    That's really inefficient. Use a StringBuilder if you're repeatedly appending to a String.
     
  10. Offline

    feildmaster

    And how is a StringBuilder any more efficient? It's basically the same thing, just using commands.

    Just use which ever code you want. I was considering using StringBuilder as the example, but sometimes it over complicates things. So use the basic code as an example.
     
  11. Offline

    desht

    It is nothing like the same thing. String is immutable, StringBuilder is not. Every String + operation creates a new String.

    StringBuilder is orders of magnitude more efficient, and it's in no way more complex.
     
  12. Offline

    feildmaster

    @desht
    Depending on how you use it, some people might wonder how it works. (i've made ridiculous appends with it sometimes... which really was the best way. :D)

    Either way, it doesn't really matter. I showed him the very basic.
     
  13. In onCommand, those few nano seconds really don't matter. But a StringBuilder is more efficient in any way, even though you have longer code.
    Look at notch's/mojang's code. They use StringBuilders for every freaking concat operation.

    Example like you can find it anywhere:
    Code:java
    1. throw new RuntimeException((new StringBuilder()).append("The working directory could not be created: ").append(file).toString());

    But yeah ... you don't really need that, but it saves processing time and RAM (the latter is probably the more important one)
     
  14. Offline

    desht

    Yeah, it's just a few nanoseconds, but it all adds up. The original example was creating two new String objects for every iteration of the loop, and each new creation was copying more data than the last. It's an O(n^2) algorithm, which is crazy when a StringBuilder approach is O(n). And it really isn't any harder.

    Sure, it doesn't get called that often, but like I said, it adds up. Imagine a busy server,with hundreds of people, and dozens of different plugins, all contending for CPU resources. Imagine even, if someone created a plugin which used that algorithm in an onPlayerMove() event?

    PHP:
    StringBuilder sb = new StringBuilder();
    for (
    String arg args) {
      
    sb.append(arg).append(" ");
    }
    String result sb.toString();
    Seriously, how hard was that?
     
Thread Status:
Not open for further replies.

Share This Page