onCommand arguments

Discussion in 'Plugin Development' started by dvargas135, Oct 21, 2015.

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

    dvargas135

    Hi,

    I'm trying to notify all players when someone does "/notify <message>". I've tried using args.toString() but it just shows [​IMG]
     
  2. Offline

    RoboticPlayer

    I believe you need a string builder for this, but I'm not positive.
     
  3. Offline

    q8minecraft

  4. Offline

    Scimiguy

  5. Offline

    dvargas135

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
         
            if (cmd.getName().equalsIgnoreCase("galaxyalert")){
                if(args.length > 0){
                    for(String s : args)
                    for (Player p : Bukkit.getOnlinePlayers()){
                        if (p.hasPermission("galaxy.staff")){
                            p.sendMessage(s);
                        }
                    }
                }
            }
          return true;
        }
    With this code, when I do "/galaxyalert hi how are you" it doesn't show it all in one line
     
  6. Offline

    q8minecraft

    @dvargas135
    As @Scimiguy already said, you need a string builder since you're sending it privately. There's many ways to do it rather than string builder. I suggest looking it up on google :p
     
  7. Offline

    boomboompower

  8. Offline

    Scimiguy

    @q8minecraft
    String builder is probably the best way though.

    If you're thinking about just doing args[0] + args[1], etc... Then you're just using super inefficient string builders.
    Programming is about solving problems, and learning. This kind of task is exactly what stringbuilders are for, should learn how to use em
     
  9. Offline

    DoggyCode™

    This is easy dude.. Need the code? Haha... Whatever, here you go:

    Code:
    //creating the String
    String msg = "";
    //getting all the arguments
    for(int i = 0; i < args.length; i++){
         //modifying 'msg' String to contain the args
         msg = "" + args[i];
         }
    //gets online players 
    for(Player players : Bukkit.getServer().getOnlinePlayers()){
          //check for the online players who has a specific permission
          if(players.hasPermission("pluginName.permission.subpermissions..."){
             //send the message
             players.sendMessage(msg);
             }
          }
    }
    
    */
    DoggyCode
    21.10.2015
    /*
     
    dvargas135 likes this.
  10. Offline

    mcdorli

    (This is just an extension to your post)
    The problem here isn't that it is slow. The problem is the memory costs.

    Basically, when you create a new String, wich never existed before, then it gets stired in a so called "string pool". The problem with this one, is that it never gets deleted from there, not matter how much memory left. Basically, if the user types in a 10 words long message, ans you use the args[0] + args[1]... method, then it will store 10 different words in the memory. If the server never stops, then it can cause a memory leak pretty easily.
     
    dvargas135 likes this.
  11. Offline

    dvargas135

    Thanks! Will test

    Thanks for the info +1 follower

    This just gets the last argument. I type "/alert omg hai" it just says "hai"

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Oct 23, 2015
  12. Offline

    Scimiguy

    @dvargas135
    That's because he's setting the message to "" every time before he adds to it.

    You should still be using a stringbuilder though
     
  13. Offline

    DoggyCode™

    Try adding a space as so:
    msg = "" + args + " ";

    That should split all the args and make it a complete string
     
  14. Offline

    Scimiguy

    @DoggyCode™
    You're still setting it to "" before doing anything with it each time
     
  15. Offline

    dvargas135

    Still
     
  16. Offline

    Scimiguy

    @dvargas135
    Code:
    StringBuilder sb = new StringBuilder();
    for (int i =0; i < args.length; i++) {
        sb.append(args[i]);
        sb.append(" ");
    }
    sb.toString();
     
  17. Offline

    mcdorli

    Try msg += args + ˙" ", if you want to stick with this answer, but I recommend, what @Scimiguy said.
     
  18. Offline

    Xerox262

    Don't forget to trim to remove those pesky extra spaces.
     
  19. Offline

    mythbusterma

    Errr no. You're right that you might be creating excess Objects (instances of String), but it is certainly not true that these are never GC'd. The JVM will GC out of the String Pool if it needs to. You're not going to run into any issues here.

    -1 for misinformation.
     
    Mrs. bwfctower likes this.
  20. Offline

    dvargas135

    +1
     
  21. Offline

    boomboompower

  22. Offline

    mcdorli

    I'm currently talking about java 6, the version that craftbukkit is written. The string pool only was moves from the permgen into the heap space in java 1.7, before that the garbage collector couldn't delete them. Everybody should use java 1.6 with bukkit, so there wouldn't be so much compatibility issue everywhere.
     
  23. Offline

    Scimiguy

  24. Offline

    mcdorli

    What section is not undestandable?
     
  25. Offline

    Scimiguy

    @mcdorli
    Your solution to out of date technology is to use it over new technology?

    Everyone should be building with 1.7 at the minimum, or even 1.8 with back-support for 1.7 if necessary
     
  26. Offline

    mcdorli

    Not every host supports 1.8, not even 1.7 sometimes.
     
  27. Offline

    mythbusterma

    @mcdorli

    1.6 is obsolete, that sticky is from a long time ago.

    Also, I'm fairly certain 1.6 would garbage collect Strings.
     
  28. Offline

    mcdorli

    Sometimes...It depends on the VM (for example most VM types for windows did not).
     
  29. Offline

    Scimiguy

    @mcdorli
    I could also argue with that saying "some hosts might still use 1.5, we should use that instead"

    Java 1.6 is very much out of date, and shouldn't be used anymore. If a host still uses it, and it causes a problem for a user, then that user can request that the plugin we backdated for them
     
  30. Offline

    mcdorli

    The second argument is totally right. But there is only a slight problem with the "a hist can use java 1.5 too" sentence. No, it's inpossible, that a host uses java 1.5, because bukkit is written in java 1.6, so it won't run on it. But maybe we should get back to the thread.
     
Thread Status:
Not open for further replies.

Share This Page